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