1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Factories; 21 22use Fisharebest\Webtrees\Date\FrenchDate; 23use Fisharebest\Webtrees\Date\GregorianDate; 24use Fisharebest\Webtrees\Date\HijriDate; 25use Fisharebest\Webtrees\Date\JalaliDate; 26use Fisharebest\Webtrees\Date\JewishDate; 27use Fisharebest\Webtrees\Date\JulianDate; 28use Fisharebest\Webtrees\Date\RomanDate; 29use Fisharebest\Webtrees\TestCase; 30use PHPUnit\Framework\Attributes\CoversClass; 31 32 33#[CoversClass(CalendarDateFactory::class)] 34class CalendarDateFactoryTest extends TestCase 35{ 36 public function testEmptyDate(): void 37 { 38 $factory = new CalendarDateFactory(); 39 40 $date = $factory->make(''); 41 42 static::assertSame(GregorianDate::ESCAPE, $date->format('%@')); 43 static::assertSame(0, $date->year); 44 static::assertSame(0, $date->month); 45 static::assertSame(0, $date->day); 46 } 47 48 public function testValidCalendarEscape(): void 49 { 50 $factory = new CalendarDateFactory(); 51 52 $calendar_escapes = [ 53 FrenchDate::ESCAPE, 54 GregorianDate::ESCAPE, 55 HijriDate::ESCAPE, 56 JalaliDate::ESCAPE, 57 JewishDate::ESCAPE, 58 JulianDate::ESCAPE, 59 RomanDate::ESCAPE, 60 ]; 61 62 foreach ($calendar_escapes as $calendar_escape) { 63 $date = $factory->make($calendar_escape); 64 static::assertSame($calendar_escape, $date->format('%@')); 65 static::assertSame(0, $date->year); 66 static::assertSame(0, $date->month); 67 static::assertSame(0, $date->day); 68 } 69 } 70 71 public function testInvalidCalendarEscapeIgnored(): void 72 { 73 $factory = new CalendarDateFactory(); 74 75 $date = $factory->make('@#DSTARDATE@'); 76 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 77 static::assertSame(0, $date->year); 78 static::assertSame(0, $date->month); 79 static::assertSame(0, $date->day); 80 } 81 82 public function testDayMonthAndYear(): void 83 { 84 $factory = new CalendarDateFactory(); 85 86 $date = $factory->make('01 JAN 1970'); 87 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 88 static::assertSame(1970, $date->year); 89 static::assertSame(1, $date->month); 90 static::assertSame(1, $date->day); 91 } 92 93 public function testMonthAndYear(): void 94 { 95 $factory = new CalendarDateFactory(); 96 97 $date = $factory->make('JAN 1970'); 98 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 99 static::assertSame(1970, $date->year); 100 static::assertSame(1, $date->month); 101 static::assertSame(0, $date->day); 102 } 103 104 public function testYear(): void 105 { 106 $factory = new CalendarDateFactory(); 107 108 $date = $factory->make('1970'); 109 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 110 static::assertSame(1970, $date->year); 111 static::assertSame(0, $date->month); 112 static::assertSame(0, $date->day); 113 } 114 115 public function testExtractedYear(): void 116 { 117 $factory = new CalendarDateFactory(); 118 119 $date = $factory->make('THE MID 1960S'); 120 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 121 static::assertSame(1960, $date->year); 122 static::assertSame(0, $date->month); 123 static::assertSame(0, $date->day); 124 } 125 126 public function testExtractedMonthAndYear(): void 127 { 128 $factory = new CalendarDateFactory(); 129 130 $date = $factory->make('PERHAPS FEB OR MAR IN 1960 or 1961'); 131 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 132 static::assertSame(1960, $date->year); 133 static::assertSame(2, $date->month); 134 static::assertSame(0, $date->day); 135 } 136 137 public function testExtractedDayMonthAndYear(): void 138 { 139 $factory = new CalendarDateFactory(); 140 141 $date = $factory->make('PERHAPS 11 OR 12 FEB OR MAR IN 1960 or 1961'); 142 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 143 static::assertSame(1960, $date->year); 144 static::assertSame(2, $date->month); 145 static::assertSame(11, $date->day); 146 } 147 148 public function testExtractedMonth(): void 149 { 150 $factory = new CalendarDateFactory(); 151 152 $date = $factory->make('PERHAPS FEB OR MAR'); 153 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 154 static::assertSame(0, $date->year); 155 static::assertSame(2, $date->month); 156 static::assertSame(0, $date->day); 157 } 158 159 public function testExtractedDayAndMonth(): void 160 { 161 $factory = new CalendarDateFactory(); 162 163 $date = $factory->make('PERHAPS 11 OR 12 FEB OR MAR'); 164 static::assertSame('@#DGREGORIAN@', $date->format('%@')); 165 static::assertSame(0, $date->year); 166 static::assertSame(2, $date->month); 167 static::assertSame(11, $date->day); 168 } 169 170 public function testUnambiguousOverrideWithHebrewMonth(): void 171 { 172 $factory = new CalendarDateFactory(); 173 174 $date = $factory->make('@#DGREGORIAN@ 10 NSN 5432'); 175 static::assertSame('@#DHEBREW@', $date->format('%@')); 176 static::assertSame(5432, $date->year); 177 static::assertSame(8, $date->month); 178 static::assertSame(10, $date->day); 179 } 180 181 public function testUnambiguousOverrideWithFrenchMonth(): void 182 { 183 $factory = new CalendarDateFactory(); 184 185 $date = $factory->make('@#DGREGORIAN@ 10 PLUV 11'); 186 static::assertSame('@#DFRENCH R@', $date->format('%@')); 187 static::assertSame(11, $date->year); 188 static::assertSame(5, $date->month); 189 static::assertSame(10, $date->day); 190 } 191 192 public function testUnambiguousOverrideWithHijriMonth(): void 193 { 194 $factory = new CalendarDateFactory(); 195 196 $date = $factory->make('@#DGREGORIAN@ 10 SHAAB 1234'); 197 static::assertSame('@#DHIJRI@', $date->format('%@')); 198 static::assertSame(1234, $date->year); 199 static::assertSame(8, $date->month); 200 static::assertSame(10, $date->day); 201 } 202 203 public function testUnambiguousOverrideWithJalaliMonth(): void 204 { 205 $factory = new CalendarDateFactory(); 206 207 $date = $factory->make('@#DGREGORIAN@ 10 BAHMA 1234'); 208 static::assertSame('@#DJALALI@', $date->format('%@')); 209 static::assertSame(1234, $date->year); 210 static::assertSame(11, $date->month); 211 static::assertSame(10, $date->day); 212 } 213 214 public function testUnambiguousOverrideWithJulianBCYear(): void 215 { 216 $factory = new CalendarDateFactory(); 217 218 $date = $factory->make('@#DGREGORIAN@ 10 AUG 44 B.C.'); 219 static::assertSame('@#DJULIAN@', $date->format('%@')); 220 static::assertSame(-44, $date->year); 221 static::assertSame(8, $date->month); 222 static::assertSame(10, $date->day); 223 } 224 225 public function testUnambiguousYearWithNoCalendar(): void 226 { 227 $factory = new CalendarDateFactory(); 228 229 $date = $factory->make('3456'); 230 static::assertSame('@#DHEBREW@', $date->format('%@')); 231 static::assertSame(3456, $date->year); 232 static::assertSame(0, $date->month); 233 static::assertSame(0, $date->day); 234 } 235 236 public function testSupportedCalendars(): void 237 { 238 $factory = new CalendarDateFactory(); 239 240 $calendars = $factory->supportedCalendars(); 241 242 static::assertNotEmpty($calendars); 243 } 244} 245