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