xref: /webtrees/tests/app/Factories/CalendarDateFactoryTest.php (revision 36779af1bd0601de7819554b13a393f6edb92507)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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        $this->assertSame(GregorianDate::ESCAPE, $date->format('%@'));
48        $this->assertSame(0, $date->year);
49        $this->assertSame(0, $date->month);
50        $this->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            $this->assertSame($calendar_escape, $date->format('%@'));
73            $this->assertSame(0, $date->year);
74            $this->assertSame(0, $date->month);
75            $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
88        $this->assertSame(0, $date->year);
89        $this->assertSame(0, $date->month);
90        $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
102        $this->assertSame(1970, $date->year);
103        $this->assertSame(1, $date->month);
104        $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
116        $this->assertSame(1970, $date->year);
117        $this->assertSame(1, $date->month);
118        $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
130        $this->assertSame(1970, $date->year);
131        $this->assertSame(0, $date->month);
132        $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
144        $this->assertSame(1960, $date->year);
145        $this->assertSame(0, $date->month);
146        $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
158        $this->assertSame(1960, $date->year);
159        $this->assertSame(2, $date->month);
160        $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
172        $this->assertSame(1960, $date->year);
173        $this->assertSame(2, $date->month);
174        $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
186        $this->assertSame(0, $date->year);
187        $this->assertSame(2, $date->month);
188        $this->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        $this->assertSame('@#DGREGORIAN@', $date->format('%@'));
200        $this->assertSame(0, $date->year);
201        $this->assertSame(2, $date->month);
202        $this->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        $this->assertSame('@#DHEBREW@', $date->format('%@'));
214        $this->assertSame(5432, $date->year);
215        $this->assertSame(8, $date->month);
216        $this->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        $this->assertSame('@#DFRENCH R@', $date->format('%@'));
228        $this->assertSame(11, $date->year);
229        $this->assertSame(5, $date->month);
230        $this->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        $this->assertSame('@#DHIJRI@', $date->format('%@'));
242        $this->assertSame(1234, $date->year);
243        $this->assertSame(8, $date->month);
244        $this->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        $this->assertSame('@#DJALALI@', $date->format('%@'));
256        $this->assertSame(1234, $date->year);
257        $this->assertSame(11, $date->month);
258        $this->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        $this->assertSame('@#DJULIAN@', $date->format('%@'));
270        $this->assertSame(-44, $date->year);
271        $this->assertSame(8, $date->month);
272        $this->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        $this->assertSame('@#DHEBREW@', $date->format('%@'));
284        $this->assertSame(3456, $date->year);
285        $this->assertSame(0, $date->month);
286        $this->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        $this->assertIsArray($calendars);
299    }
300}
301