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