xref: /webtrees/tests/app/AgeTest.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
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;
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::__toString
35     */
36    public function testSameDayMonthYear(): void
37    {
38        $x = new Date('27 APR 2019');
39        $y = new Date('27 APR 2019');
40        $age = new Age($x, $y);
41
42        self::assertSame(0, $age->ageDays());
43        self::assertSame(0, $age->ageYears());
44        self::assertSame('0', $age->ageYearsString());
45        self::assertSame('0 days', (string) $age);
46    }
47
48    /**
49     * @covers \Fisharebest\Webtrees\Age::__construct
50     * @covers \Fisharebest\Webtrees\Age::ageDays
51     * @covers \Fisharebest\Webtrees\Age::ageYears
52     * @covers \Fisharebest\Webtrees\Age::ageYearsString
53     * @covers \Fisharebest\Webtrees\Age::__toString
54     */
55    public function testSameMonthYear(): void
56    {
57        $x = new Date('APR 2019');
58        $y = new Date('APR 2019');
59        $age = new Age($x, $y);
60
61        self::assertSame(0, $age->ageDays());
62        self::assertSame(0, $age->ageYears());
63        self::assertSame('0', $age->ageYearsString());
64        self::assertSame('0', (string) $age);
65    }
66
67    /**
68     * @covers \Fisharebest\Webtrees\Age::__construct
69     * @covers \Fisharebest\Webtrees\Age::ageDays
70     * @covers \Fisharebest\Webtrees\Age::ageYears
71     * @covers \Fisharebest\Webtrees\Age::ageYearsString
72     * @covers \Fisharebest\Webtrees\Age::__toString
73     */
74    public function testSameYear(): void
75    {
76        $x = new Date('2019');
77        $y = new Date('2019');
78        $age = new Age($x, $y);
79
80        self::assertSame(0, $age->ageDays());
81        self::assertSame(0, $age->ageYears());
82        self::assertSame('0', $age->ageYearsString());
83        self::assertSame('0', (string) $age);
84    }
85
86    /**
87     * @covers \Fisharebest\Webtrees\Age::__construct
88     * @covers \Fisharebest\Webtrees\Age::ageDays
89     * @covers \Fisharebest\Webtrees\Age::ageYears
90     * @covers \Fisharebest\Webtrees\Age::ageYearsString
91     * @covers \Fisharebest\Webtrees\Age::__toString
92     */
93    public function testReversed(): void
94    {
95        $x = new Date('13 FEB 2019');
96        $y = new Date('07 JAN 2019');
97        $age = new Age($x, $y);
98
99        self::assertSame(-37, $age->ageDays());
100        self::assertSame(-1, $age->ageYears());
101        self::assertSame(view('icons/warning'), $age->ageYearsString());
102        self::assertSame(view('icons/warning'), (string) $age);
103    }
104
105    /**
106     * @covers \Fisharebest\Webtrees\Age::__construct
107     * @covers \Fisharebest\Webtrees\Age::ageDays
108     * @covers \Fisharebest\Webtrees\Age::ageYears
109     * @covers \Fisharebest\Webtrees\Age::ageYearsString
110     * @covers \Fisharebest\Webtrees\Age::__toString
111     */
112    public function testStartDateInvalid(): void
113    {
114        $x = new Date('');
115        $y = new Date('07 JAN 2019');
116        $age = new Age($x, $y);
117
118        self::assertSame(-1, $age->ageDays());
119        self::assertSame(-1, $age->ageYears());
120        self::assertSame('', $age->ageYearsString());
121        self::assertSame('', (string) $age);
122    }
123
124    /**
125     * @covers \Fisharebest\Webtrees\Age::__construct
126     * @covers \Fisharebest\Webtrees\Age::ageDays
127     * @covers \Fisharebest\Webtrees\Age::ageYears
128     * @covers \Fisharebest\Webtrees\Age::ageYearsString
129     * @covers \Fisharebest\Webtrees\Age::__toString
130     */
131    public function testEndDateInvalid(): void
132    {
133        $x = new Date('07 JAN 2019');
134        $y = new Date('');
135        $age = new Age($x, $y);
136
137        self::assertSame(-1, $age->ageDays());
138        self::assertSame(-1, $age->ageYears());
139        self::assertSame('', $age->ageYearsString());
140        self::assertSame('', (string) $age);
141    }
142
143    /**
144     * @covers \Fisharebest\Webtrees\Age::__construct
145     * @covers \Fisharebest\Webtrees\Age::ageDays
146     * @covers \Fisharebest\Webtrees\Age::ageYears
147     * @covers \Fisharebest\Webtrees\Age::ageYearsString
148     * @covers \Fisharebest\Webtrees\Age::__toString
149     */
150    public function testOverlappingDates1(): void
151    {
152        $x = new Date('07 JAN 2019');
153        $y = new Date('JAN 2019');
154        $age = new Age($x, $y);
155
156        self::assertSame(-6, $age->ageDays());
157        self::assertSame(0, $age->ageYears());
158        self::assertSame('0', $age->ageYearsString());
159        self::assertSame('0', (string) $age);
160    }
161
162    /**
163     * @covers \Fisharebest\Webtrees\Age::__construct
164     * @covers \Fisharebest\Webtrees\Age::ageDays
165     * @covers \Fisharebest\Webtrees\Age::ageYears
166     * @covers \Fisharebest\Webtrees\Age::ageYearsString
167     * @covers \Fisharebest\Webtrees\Age::__toString
168     */
169    public function testOverlappingDates2(): void
170    {
171        $x = new Date('JAN 2019');
172        $y = new Date('07 JAN 2019');
173        $age = new Age($x, $y);
174
175        self::assertSame(6, $age->ageDays());
176        self::assertSame(0, $age->ageYears());
177        self::assertSame('0', $age->ageYearsString());
178        self::assertSame('0', (string) $age);
179    }
180
181    /**
182     * @covers \Fisharebest\Webtrees\Age::__construct
183     * @covers \Fisharebest\Webtrees\Age::ageDays
184     * @covers \Fisharebest\Webtrees\Age::ageYears
185     * @covers \Fisharebest\Webtrees\Age::ageYearsString
186     * @covers \Fisharebest\Webtrees\Age::__toString
187     */
188    public function testDifferentDay(): void
189    {
190        $x = new Date('13 APR 2019');
191        $y = new Date('27 APR 2019');
192        $age = new Age($x, $y);
193
194        self::assertSame(14, $age->ageDays());
195        self::assertSame(0, $age->ageYears());
196        self::assertSame('0', $age->ageYearsString());
197        self::assertSame('14 days', (string) $age);
198    }
199
200    /**
201     * @covers \Fisharebest\Webtrees\Age::__construct
202     * @covers \Fisharebest\Webtrees\Age::ageDays
203     * @covers \Fisharebest\Webtrees\Age::ageYears
204     * @covers \Fisharebest\Webtrees\Age::ageYearsString
205     * @covers \Fisharebest\Webtrees\Age::__toString
206     */
207    public function testDifferentMonth(): void
208    {
209        $x = new Date('13 APR 2019');
210        $y = new Date('27 JUN 2019');
211        $age = new Age($x, $y);
212
213        self::assertSame(75, $age->ageDays());
214        self::assertSame(0, $age->ageYears());
215        self::assertSame('0', $age->ageYearsString());
216        self::assertSame('2 months', (string) $age);
217    }
218
219    /**
220     * @covers \Fisharebest\Webtrees\Age::__construct
221     * @covers \Fisharebest\Webtrees\Age::ageDays
222     * @covers \Fisharebest\Webtrees\Age::ageYears
223     * @covers \Fisharebest\Webtrees\Age::ageYearsString
224     * @covers \Fisharebest\Webtrees\Age::__toString
225     */
226    public function testDifferentYear(): void
227    {
228        $x = new Date('13 APR 2012');
229        $y = new Date('27 JUN 2019');
230        $age = new Age($x, $y);
231
232        self::assertSame(2631, $age->ageDays());
233        self::assertSame(7, $age->ageYears());
234        self::assertSame('7', $age->ageYearsString());
235        self::assertSame('7 years', (string) $age);
236    }
237}
238