xref: /webtrees/tests/app/Factories/CalendarDateFactoryTest.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
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#[CoversClass(CalendarDateFactory::class)]
33class CalendarDateFactoryTest extends TestCase
34{
35    public function testEmptyDate(): void
36    {
37        $factory = new CalendarDateFactory();
38
39        $date = $factory->make('');
40
41        static::assertSame(GregorianDate::ESCAPE, $date->format('%@'));
42        static::assertSame(0, $date->year);
43        static::assertSame(0, $date->month);
44        static::assertSame(0, $date->day);
45    }
46
47    public function testValidCalendarEscape(): void
48    {
49        $factory = new CalendarDateFactory();
50
51        $calendar_escapes = [
52            FrenchDate::ESCAPE,
53            GregorianDate::ESCAPE,
54            HijriDate::ESCAPE,
55            JalaliDate::ESCAPE,
56            JewishDate::ESCAPE,
57            JulianDate::ESCAPE,
58            RomanDate::ESCAPE,
59        ];
60
61        foreach ($calendar_escapes as $calendar_escape) {
62            $date = $factory->make($calendar_escape);
63            static::assertSame($calendar_escape, $date->format('%@'));
64            static::assertSame(0, $date->year);
65            static::assertSame(0, $date->month);
66            static::assertSame(0, $date->day);
67        }
68    }
69
70    public function testInvalidCalendarEscapeIgnored(): void
71    {
72        $factory = new CalendarDateFactory();
73
74        $date = $factory->make('@#DSTARDATE@');
75        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
76        static::assertSame(0, $date->year);
77        static::assertSame(0, $date->month);
78        static::assertSame(0, $date->day);
79    }
80
81    public function testDayMonthAndYear(): void
82    {
83        $factory = new CalendarDateFactory();
84
85        $date = $factory->make('01 JAN 1970');
86        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
87        static::assertSame(1970, $date->year);
88        static::assertSame(1, $date->month);
89        static::assertSame(1, $date->day);
90    }
91
92    public function testMonthAndYear(): void
93    {
94        $factory = new CalendarDateFactory();
95
96        $date = $factory->make('JAN 1970');
97        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
98        static::assertSame(1970, $date->year);
99        static::assertSame(1, $date->month);
100        static::assertSame(0, $date->day);
101    }
102
103    public function testYear(): void
104    {
105        $factory = new CalendarDateFactory();
106
107        $date = $factory->make('1970');
108        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
109        static::assertSame(1970, $date->year);
110        static::assertSame(0, $date->month);
111        static::assertSame(0, $date->day);
112    }
113
114    public function testExtractedYear(): void
115    {
116        $factory = new CalendarDateFactory();
117
118        $date = $factory->make('THE MID 1960S');
119        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
120        static::assertSame(1960, $date->year);
121        static::assertSame(0, $date->month);
122        static::assertSame(0, $date->day);
123    }
124
125    public function testExtractedMonthAndYear(): void
126    {
127        $factory = new CalendarDateFactory();
128
129        $date = $factory->make('PERHAPS FEB OR MAR IN 1960 or 1961');
130        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
131        static::assertSame(1960, $date->year);
132        static::assertSame(2, $date->month);
133        static::assertSame(0, $date->day);
134    }
135
136    public function testExtractedDayMonthAndYear(): void
137    {
138        $factory = new CalendarDateFactory();
139
140        $date = $factory->make('PERHAPS 11 OR 12 FEB OR MAR IN 1960 or 1961');
141        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
142        static::assertSame(1960, $date->year);
143        static::assertSame(2, $date->month);
144        static::assertSame(11, $date->day);
145    }
146
147    public function testExtractedMonth(): void
148    {
149        $factory = new CalendarDateFactory();
150
151        $date = $factory->make('PERHAPS FEB OR MAR');
152        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
153        static::assertSame(0, $date->year);
154        static::assertSame(2, $date->month);
155        static::assertSame(0, $date->day);
156    }
157
158    public function testExtractedDayAndMonth(): void
159    {
160        $factory = new CalendarDateFactory();
161
162        $date = $factory->make('PERHAPS 11 OR 12 FEB OR MAR');
163        static::assertSame('@#DGREGORIAN@', $date->format('%@'));
164        static::assertSame(0, $date->year);
165        static::assertSame(2, $date->month);
166        static::assertSame(11, $date->day);
167    }
168
169    public function testUnambiguousOverrideWithHebrewMonth(): void
170    {
171        $factory = new CalendarDateFactory();
172
173        $date = $factory->make('@#DGREGORIAN@ 10 NSN 5432');
174        static::assertSame('@#DHEBREW@', $date->format('%@'));
175        static::assertSame(5432, $date->year);
176        static::assertSame(8, $date->month);
177        static::assertSame(10, $date->day);
178    }
179
180    public function testUnambiguousOverrideWithFrenchMonth(): void
181    {
182        $factory = new CalendarDateFactory();
183
184        $date = $factory->make('@#DGREGORIAN@ 10 PLUV 11');
185        static::assertSame('@#DFRENCH R@', $date->format('%@'));
186        static::assertSame(11, $date->year);
187        static::assertSame(5, $date->month);
188        static::assertSame(10, $date->day);
189    }
190
191    public function testUnambiguousOverrideWithHijriMonth(): void
192    {
193        $factory = new CalendarDateFactory();
194
195        $date = $factory->make('@#DGREGORIAN@ 10 SHAAB 1234');
196        static::assertSame('@#DHIJRI@', $date->format('%@'));
197        static::assertSame(1234, $date->year);
198        static::assertSame(8, $date->month);
199        static::assertSame(10, $date->day);
200    }
201
202    public function testUnambiguousOverrideWithJalaliMonth(): void
203    {
204        $factory = new CalendarDateFactory();
205
206        $date = $factory->make('@#DGREGORIAN@ 10 BAHMA 1234');
207        static::assertSame('@#DJALALI@', $date->format('%@'));
208        static::assertSame(1234, $date->year);
209        static::assertSame(11, $date->month);
210        static::assertSame(10, $date->day);
211    }
212
213    public function testUnambiguousOverrideWithJulianBCYear(): void
214    {
215        $factory = new CalendarDateFactory();
216
217        $date = $factory->make('@#DGREGORIAN@ 10 AUG 44 B.C.');
218        static::assertSame('@#DJULIAN@', $date->format('%@'));
219        static::assertSame(-44, $date->year);
220        static::assertSame(8, $date->month);
221        static::assertSame(10, $date->day);
222    }
223
224    public function testUnambiguousYearWithNoCalendar(): void
225    {
226        $factory = new CalendarDateFactory();
227
228        $date = $factory->make('3456');
229        static::assertSame('@#DHEBREW@', $date->format('%@'));
230        static::assertSame(3456, $date->year);
231        static::assertSame(0, $date->month);
232        static::assertSame(0, $date->day);
233    }
234
235    public function testSupportedCalendars(): void
236    {
237        $factory = new CalendarDateFactory();
238
239        $calendars = $factory->supportedCalendars();
240
241        static::assertNotEmpty($calendars);
242    }
243}
244