xref: /webtrees/tests/app/AgeTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
1d64ed531Ssibador<?php
2506a570fSGreg Roach
3d64ed531Ssibador/**
4d64ed531Ssibador * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6d64ed531Ssibador * This program is free software: you can redistribute it and/or modify
7d64ed531Ssibador * it under the terms of the GNU General Public License as published by
8d64ed531Ssibador * the Free Software Foundation, either version 3 of the License, or
9d64ed531Ssibador * (at your option) any later version.
10d64ed531Ssibador * This program is distributed in the hope that it will be useful,
11d64ed531Ssibador * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d64ed531Ssibador * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d64ed531Ssibador * GNU General Public License for more details.
14d64ed531Ssibador * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16d64ed531Ssibador */
17506a570fSGreg Roach
18d64ed531Ssibadordeclare(strict_types=1);
19d64ed531Ssibador
20d64ed531Ssibadornamespace Fisharebest\Webtrees;
21d64ed531Ssibador
22*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
23*202c018bSGreg Roach
24204c2daeSGreg Roachuse function view;
25204c2daeSGreg Roach
26*202c018bSGreg Roach#[CoversClass(Age::class)]
27d64ed531Ssibadorclass AgeTest extends TestCase
28d64ed531Ssibador{
29204c2daeSGreg Roach    public function testSameDayMonthYear(): void
30d64ed531Ssibador    {
31204c2daeSGreg Roach        $x = new Date('27 APR 2019');
32204c2daeSGreg Roach        $y = new Date('27 APR 2019');
33204c2daeSGreg Roach        $age = new Age($x, $y);
34204c2daeSGreg Roach
355e933c21SGreg Roach        self::assertSame(0, $age->ageDays());
365e933c21SGreg Roach        self::assertSame(0, $age->ageYears());
375e933c21SGreg Roach        self::assertSame('0', $age->ageYearsString());
38a5fd6d7cSGreg Roach        self::assertSame('0 days', (string) $age);
39d64ed531Ssibador    }
40d64ed531Ssibador
41204c2daeSGreg Roach    public function testSameMonthYear(): void
42d64ed531Ssibador    {
43204c2daeSGreg Roach        $x = new Date('APR 2019');
44204c2daeSGreg Roach        $y = new Date('APR 2019');
45204c2daeSGreg Roach        $age = new Age($x, $y);
46204c2daeSGreg Roach
475e933c21SGreg Roach        self::assertSame(0, $age->ageDays());
485e933c21SGreg Roach        self::assertSame(0, $age->ageYears());
495e933c21SGreg Roach        self::assertSame('0', $age->ageYearsString());
50a5fd6d7cSGreg Roach        self::assertSame('0', (string) $age);
51d64ed531Ssibador    }
52d64ed531Ssibador
53204c2daeSGreg Roach    public function testSameYear(): void
54d64ed531Ssibador    {
55204c2daeSGreg Roach        $x = new Date('2019');
56204c2daeSGreg Roach        $y = new Date('2019');
57204c2daeSGreg Roach        $age = new Age($x, $y);
58204c2daeSGreg Roach
595e933c21SGreg Roach        self::assertSame(0, $age->ageDays());
605e933c21SGreg Roach        self::assertSame(0, $age->ageYears());
615e933c21SGreg Roach        self::assertSame('0', $age->ageYearsString());
62a5fd6d7cSGreg Roach        self::assertSame('0', (string) $age);
63204c2daeSGreg Roach    }
64204c2daeSGreg Roach
65204c2daeSGreg Roach    public function testReversed(): void
66204c2daeSGreg Roach    {
67204c2daeSGreg Roach        $x = new Date('13 FEB 2019');
68204c2daeSGreg Roach        $y = new Date('07 JAN 2019');
69204c2daeSGreg Roach        $age = new Age($x, $y);
70204c2daeSGreg Roach
715e933c21SGreg Roach        self::assertSame(-37, $age->ageDays());
725e933c21SGreg Roach        self::assertSame(-1, $age->ageYears());
735e933c21SGreg Roach        self::assertSame(view('icons/warning'), $age->ageYearsString());
74a5fd6d7cSGreg Roach        self::assertSame(view('icons/warning'), (string) $age);
75204c2daeSGreg Roach    }
76204c2daeSGreg Roach
77204c2daeSGreg Roach    public function testStartDateInvalid(): void
78204c2daeSGreg Roach    {
79204c2daeSGreg Roach        $x = new Date('');
80204c2daeSGreg Roach        $y = new Date('07 JAN 2019');
81204c2daeSGreg Roach        $age = new Age($x, $y);
82204c2daeSGreg Roach
835e933c21SGreg Roach        self::assertSame(-1, $age->ageDays());
845e933c21SGreg Roach        self::assertSame(-1, $age->ageYears());
855e933c21SGreg Roach        self::assertSame('', $age->ageYearsString());
86a5fd6d7cSGreg Roach        self::assertSame('', (string) $age);
87204c2daeSGreg Roach    }
88204c2daeSGreg Roach
89204c2daeSGreg Roach    public function testEndDateInvalid(): void
90204c2daeSGreg Roach    {
91204c2daeSGreg Roach        $x = new Date('07 JAN 2019');
92204c2daeSGreg Roach        $y = new Date('');
93204c2daeSGreg Roach        $age = new Age($x, $y);
94204c2daeSGreg Roach
955e933c21SGreg Roach        self::assertSame(-1, $age->ageDays());
965e933c21SGreg Roach        self::assertSame(-1, $age->ageYears());
975e933c21SGreg Roach        self::assertSame('', $age->ageYearsString());
98a5fd6d7cSGreg Roach        self::assertSame('', (string) $age);
99204c2daeSGreg Roach    }
100204c2daeSGreg Roach
101204c2daeSGreg Roach    public function testOverlappingDates1(): void
102204c2daeSGreg Roach    {
103204c2daeSGreg Roach        $x = new Date('07 JAN 2019');
104204c2daeSGreg Roach        $y = new Date('JAN 2019');
105204c2daeSGreg Roach        $age = new Age($x, $y);
106204c2daeSGreg Roach
1075e933c21SGreg Roach        self::assertSame(-6, $age->ageDays());
1085e933c21SGreg Roach        self::assertSame(0, $age->ageYears());
1095e933c21SGreg Roach        self::assertSame('0', $age->ageYearsString());
110a5fd6d7cSGreg Roach        self::assertSame('0', (string) $age);
111204c2daeSGreg Roach    }
112204c2daeSGreg Roach
113204c2daeSGreg Roach    public function testOverlappingDates2(): void
114204c2daeSGreg Roach    {
115204c2daeSGreg Roach        $x = new Date('JAN 2019');
116204c2daeSGreg Roach        $y = new Date('07 JAN 2019');
117204c2daeSGreg Roach        $age = new Age($x, $y);
118204c2daeSGreg Roach
1195e933c21SGreg Roach        self::assertSame(6, $age->ageDays());
1205e933c21SGreg Roach        self::assertSame(0, $age->ageYears());
1215e933c21SGreg Roach        self::assertSame('0', $age->ageYearsString());
122a5fd6d7cSGreg Roach        self::assertSame('0', (string) $age);
123204c2daeSGreg Roach    }
124204c2daeSGreg Roach
125204c2daeSGreg Roach    public function testDifferentDay(): void
126204c2daeSGreg Roach    {
127204c2daeSGreg Roach        $x = new Date('13 APR 2019');
128204c2daeSGreg Roach        $y = new Date('27 APR 2019');
129204c2daeSGreg Roach        $age = new Age($x, $y);
130204c2daeSGreg Roach
1315e933c21SGreg Roach        self::assertSame(14, $age->ageDays());
1325e933c21SGreg Roach        self::assertSame(0, $age->ageYears());
1335e933c21SGreg Roach        self::assertSame('0', $age->ageYearsString());
134a5fd6d7cSGreg Roach        self::assertSame('14 days', (string) $age);
135204c2daeSGreg Roach    }
136204c2daeSGreg Roach
137204c2daeSGreg Roach    public function testDifferentMonth(): void
138204c2daeSGreg Roach    {
139204c2daeSGreg Roach        $x = new Date('13 APR 2019');
140204c2daeSGreg Roach        $y = new Date('27 JUN 2019');
141204c2daeSGreg Roach        $age = new Age($x, $y);
142204c2daeSGreg Roach
1435e933c21SGreg Roach        self::assertSame(75, $age->ageDays());
1445e933c21SGreg Roach        self::assertSame(0, $age->ageYears());
1455e933c21SGreg Roach        self::assertSame('0', $age->ageYearsString());
146a5fd6d7cSGreg Roach        self::assertSame('2 months', (string) $age);
147204c2daeSGreg Roach    }
148204c2daeSGreg Roach
149204c2daeSGreg Roach    public function testDifferentYear(): void
150204c2daeSGreg Roach    {
151204c2daeSGreg Roach        $x = new Date('13 APR 2012');
152204c2daeSGreg Roach        $y = new Date('27 JUN 2019');
153204c2daeSGreg Roach        $age = new Age($x, $y);
154204c2daeSGreg Roach
1555e933c21SGreg Roach        self::assertSame(2631, $age->ageDays());
1565e933c21SGreg Roach        self::assertSame(7, $age->ageYears());
1575e933c21SGreg Roach        self::assertSame('7', $age->ageYearsString());
158a5fd6d7cSGreg Roach        self::assertSame('7 years', (string) $age);
159d64ed531Ssibador    }
160d64ed531Ssibador}
161