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