xref: /webtrees/tests/app/AgeTest.php (revision 0e2e57bd5c8dad261d043770bf646c2285b4eaca)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use function view;
23
24/**
25 * Test the Age functions
26 */
27class AgeTest extends TestCase
28{
29    /**
30     * @covers \Fisharebest\Webtrees\Age::__construct
31     * @covers \Fisharebest\Webtrees\Age::ageDays
32     * @covers \Fisharebest\Webtrees\Age::ageYears
33     * @covers \Fisharebest\Webtrees\Age::ageYearsString
34     * @covers \Fisharebest\Webtrees\Age::ageString
35     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
36     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
37     *
38     * @return void
39     */
40    public function testSameDayMonthYear(): void
41    {
42        $x = new Date('27 APR 2019');
43        $y = new Date('27 APR 2019');
44        $age = new Age($x, $y);
45
46        self::assertSame(0, $age->ageDays());
47        self::assertSame(0, $age->ageYears());
48        self::assertSame('0', $age->ageYearsString());
49        self::assertSame('0 days', $age->ageString());
50        self::assertSame('(aged 0 days)', $age->ageAtEvent(false));
51        self::assertSame('(age 0 days)', $age->ageAtEvent(true));
52        self::assertSame('(on the date of death)', $age->timeAfterDeath());
53    }
54
55    /**
56     * @covers \Fisharebest\Webtrees\Age::__construct
57     * @covers \Fisharebest\Webtrees\Age::ageDays
58     * @covers \Fisharebest\Webtrees\Age::ageYears
59     * @covers \Fisharebest\Webtrees\Age::ageYearsString
60     * @covers \Fisharebest\Webtrees\Age::ageString
61     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
62     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
63     *
64     * @return void
65     */
66    public function testSameMonthYear(): void
67    {
68        $x = new Date('APR 2019');
69        $y = new Date('APR 2019');
70        $age = new Age($x, $y);
71
72        self::assertSame(0, $age->ageDays());
73        self::assertSame(0, $age->ageYears());
74        self::assertSame('0', $age->ageYearsString());
75        self::assertSame('0', $age->ageString());
76        self::assertSame('(aged 0)', $age->ageAtEvent(false));
77        self::assertSame('(age 0)', $age->ageAtEvent(true));
78        self::assertSame('', $age->timeAfterDeath());
79    }
80
81    /**
82     * @covers \Fisharebest\Webtrees\Age::__construct
83     * @covers \Fisharebest\Webtrees\Age::ageDays
84     * @covers \Fisharebest\Webtrees\Age::ageYears
85     * @covers \Fisharebest\Webtrees\Age::ageYearsString
86     * @covers \Fisharebest\Webtrees\Age::ageString
87     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
88     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
89     *
90     * @return void
91     */
92    public function testSameYear(): void
93    {
94        $x = new Date('2019');
95        $y = new Date('2019');
96        $age = new Age($x, $y);
97
98        self::assertSame(0, $age->ageDays());
99        self::assertSame(0, $age->ageYears());
100        self::assertSame('0', $age->ageYearsString());
101        self::assertSame('0', $age->ageString());
102        self::assertSame('(aged 0)', $age->ageAtEvent(false));
103        self::assertSame('(age 0)', $age->ageAtEvent(true));
104        self::assertSame('', $age->timeAfterDeath());
105    }
106
107    /**
108     * @covers \Fisharebest\Webtrees\Age::__construct
109     * @covers \Fisharebest\Webtrees\Age::ageDays
110     * @covers \Fisharebest\Webtrees\Age::ageYears
111     * @covers \Fisharebest\Webtrees\Age::ageYearsString
112     * @covers \Fisharebest\Webtrees\Age::ageString
113     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
114     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
115     *
116     * @return void
117     */
118    public function testReversed(): void
119    {
120        $x = new Date('13 FEB 2019');
121        $y = new Date('07 JAN 2019');
122        $age = new Age($x, $y);
123
124        self::assertSame(-37, $age->ageDays());
125        self::assertSame(-1, $age->ageYears());
126        self::assertSame(view('icons/warning'), $age->ageYearsString());
127        self::assertSame(view('icons/warning'), $age->ageString());
128        self::assertSame('(aged ' . view('icons/warning') . ')', $age->ageAtEvent(false));
129        self::assertSame('(age ' . view('icons/warning') . ')', $age->ageAtEvent(true));
130        self::assertSame('(' . view('icons/warning') . ' after death)', $age->timeAfterDeath());
131    }
132
133    /**
134     * @covers \Fisharebest\Webtrees\Age::__construct
135     * @covers \Fisharebest\Webtrees\Age::ageDays
136     * @covers \Fisharebest\Webtrees\Age::ageYears
137     * @covers \Fisharebest\Webtrees\Age::ageYearsString
138     * @covers \Fisharebest\Webtrees\Age::ageString
139     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
140     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
141     *
142     * @return void
143     */
144    public function testStartDateInvalid(): void
145    {
146        $x = new Date('');
147        $y = new Date('07 JAN 2019');
148        $age = new Age($x, $y);
149
150        self::assertSame(-1, $age->ageDays());
151        self::assertSame(-1, $age->ageYears());
152        self::assertSame('', $age->ageYearsString());
153        self::assertSame('', $age->ageString());
154        self::assertSame('', $age->ageAtEvent(false));
155        self::assertSame('', $age->ageAtEvent(true));
156        self::assertSame('', $age->timeAfterDeath());
157    }
158
159    /**
160     * @covers \Fisharebest\Webtrees\Age::__construct
161     * @covers \Fisharebest\Webtrees\Age::ageDays
162     * @covers \Fisharebest\Webtrees\Age::ageYears
163     * @covers \Fisharebest\Webtrees\Age::ageYearsString
164     * @covers \Fisharebest\Webtrees\Age::ageString
165     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
166     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
167     *
168     * @return void
169     */
170    public function testEndDateInvalid(): void
171    {
172        $x = new Date('07 JAN 2019');
173        $y = new Date('');
174        $age = new Age($x, $y);
175
176        self::assertSame(-1, $age->ageDays());
177        self::assertSame(-1, $age->ageYears());
178        self::assertSame('', $age->ageYearsString());
179        self::assertSame('', $age->ageString());
180        self::assertSame('', $age->ageAtEvent(false));
181        self::assertSame('', $age->ageAtEvent(true));
182        self::assertSame('', $age->timeAfterDeath());
183    }
184
185    /**
186     * @covers \Fisharebest\Webtrees\Age::__construct
187     * @covers \Fisharebest\Webtrees\Age::ageDays
188     * @covers \Fisharebest\Webtrees\Age::ageYears
189     * @covers \Fisharebest\Webtrees\Age::ageYearsString
190     * @covers \Fisharebest\Webtrees\Age::ageString
191     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
192     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
193     *
194     * @return void
195     */
196    public function testOverlappingDates1(): void
197    {
198        $x = new Date('07 JAN 2019');
199        $y = new Date('JAN 2019');
200        $age = new Age($x, $y);
201
202        self::assertSame(-6, $age->ageDays());
203        self::assertSame(0, $age->ageYears());
204        self::assertSame('0', $age->ageYearsString());
205        self::assertSame('0', $age->ageString());
206        self::assertSame('(aged 0)', $age->ageAtEvent(false));
207        self::assertSame('(age 0)', $age->ageAtEvent(true));
208        self::assertSame('', $age->timeAfterDeath());
209    }
210
211    /**
212     * @covers \Fisharebest\Webtrees\Age::__construct
213     * @covers \Fisharebest\Webtrees\Age::ageDays
214     * @covers \Fisharebest\Webtrees\Age::ageYears
215     * @covers \Fisharebest\Webtrees\Age::ageYearsString
216     * @covers \Fisharebest\Webtrees\Age::ageString
217     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
218     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
219     *
220     * @return void
221     */
222    public function testOverlappingDates2(): void
223    {
224        $x = new Date('JAN 2019');
225        $y = new Date('07 JAN 2019');
226        $age = new Age($x, $y);
227
228        self::assertSame(6, $age->ageDays());
229        self::assertSame(0, $age->ageYears());
230        self::assertSame('0', $age->ageYearsString());
231        self::assertSame('0', $age->ageString());
232        self::assertSame('(aged 0)', $age->ageAtEvent(false));
233        self::assertSame('(age 0)', $age->ageAtEvent(true));
234        self::assertSame('', $age->timeAfterDeath());
235    }
236
237    /**
238     * @covers \Fisharebest\Webtrees\Age::__construct
239     * @covers \Fisharebest\Webtrees\Age::ageDays
240     * @covers \Fisharebest\Webtrees\Age::ageYears
241     * @covers \Fisharebest\Webtrees\Age::ageYearsString
242     * @covers \Fisharebest\Webtrees\Age::ageString
243     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
244     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
245     *
246     * @return void
247     */
248    public function testDifferentDay(): void
249    {
250        $x = new Date('13 APR 2019');
251        $y = new Date('27 APR 2019');
252        $age = new Age($x, $y);
253
254        self::assertSame(14, $age->ageDays());
255        self::assertSame(0, $age->ageYears());
256        self::assertSame('0', $age->ageYearsString());
257        self::assertSame('14 days', $age->ageString());
258        self::assertSame('(aged 14 days)', $age->ageAtEvent(false));
259        self::assertSame('(age 14 days)', $age->ageAtEvent(true));
260        self::assertSame('(14 days after death)', $age->timeAfterDeath());
261    }
262
263    /**
264     * @covers \Fisharebest\Webtrees\Age::__construct
265     * @covers \Fisharebest\Webtrees\Age::ageDays
266     * @covers \Fisharebest\Webtrees\Age::ageYears
267     * @covers \Fisharebest\Webtrees\Age::ageYearsString
268     * @covers \Fisharebest\Webtrees\Age::ageString
269     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
270     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
271     *
272     * @return void
273     */
274    public function testDifferentMonth(): void
275    {
276        $x = new Date('13 APR 2019');
277        $y = new Date('27 JUN 2019');
278        $age = new Age($x, $y);
279
280        self::assertSame(75, $age->ageDays());
281        self::assertSame(0, $age->ageYears());
282        self::assertSame('0', $age->ageYearsString());
283        self::assertSame('2 months', $age->ageString());
284        self::assertSame('(aged 2 months)', $age->ageAtEvent(false));
285        self::assertSame('(age 2 months)', $age->ageAtEvent(true));
286        self::assertSame('(2 months after death)', $age->timeAfterDeath());
287    }
288
289    /**
290     * @covers \Fisharebest\Webtrees\Age::__construct
291     * @covers \Fisharebest\Webtrees\Age::ageDays
292     * @covers \Fisharebest\Webtrees\Age::ageYears
293     * @covers \Fisharebest\Webtrees\Age::ageYearsString
294     * @covers \Fisharebest\Webtrees\Age::ageString
295     * @covers \Fisharebest\Webtrees\Age::ageAtEvent
296     * @covers \Fisharebest\Webtrees\Age::timeAfterDeath
297     *
298     * @return void
299     */
300    public function testDifferentYear(): void
301    {
302        $x = new Date('13 APR 2012');
303        $y = new Date('27 JUN 2019');
304        $age = new Age($x, $y);
305
306        self::assertSame(2631, $age->ageDays());
307        self::assertSame(7, $age->ageYears());
308        self::assertSame('7', $age->ageYearsString());
309        self::assertSame('7 years', $age->ageString());
310        self::assertSame('(aged 7 years)', $age->ageAtEvent(false));
311        self::assertSame('(age 7 years)', $age->ageAtEvent(true));
312        self::assertSame('(7 years after death)', $age->timeAfterDeath());
313    }
314}
315