xref: /webtrees/tests/app/AgeTest.php (revision e873f434551745f888937263ff89e80db3b0f785)
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 PHPUnit\Framework\Attributes\CoversClass;
23
24use function view;
25
26#[CoversClass(Age::class)]
27class AgeTest extends TestCase
28{
29    public function testSameDayMonthYear(): void
30    {
31        $x = new Date('27 APR 2019');
32        $y = new Date('27 APR 2019');
33        $age = new Age($x, $y);
34
35        self::assertSame(0, $age->ageDays());
36        self::assertSame(0, $age->ageYears());
37        self::assertSame('0', $age->ageYearsString());
38        self::assertSame('0 days', (string) $age);
39    }
40
41    public function testSameMonthYear(): void
42    {
43        $x = new Date('APR 2019');
44        $y = new Date('APR 2019');
45        $age = new Age($x, $y);
46
47        self::assertSame(0, $age->ageDays());
48        self::assertSame(0, $age->ageYears());
49        self::assertSame('0', $age->ageYearsString());
50        self::assertSame('0', (string) $age);
51    }
52
53    public function testSameYear(): void
54    {
55        $x = new Date('2019');
56        $y = new Date('2019');
57        $age = new Age($x, $y);
58
59        self::assertSame(0, $age->ageDays());
60        self::assertSame(0, $age->ageYears());
61        self::assertSame('0', $age->ageYearsString());
62        self::assertSame('0', (string) $age);
63    }
64
65    public function testReversed(): void
66    {
67        $x = new Date('13 FEB 2019');
68        $y = new Date('07 JAN 2019');
69        $age = new Age($x, $y);
70
71        self::assertSame(-37, $age->ageDays());
72        self::assertSame(-1, $age->ageYears());
73        self::assertSame(view('icons/warning'), $age->ageYearsString());
74        self::assertSame(view('icons/warning'), (string) $age);
75    }
76
77    public function testStartDateInvalid(): void
78    {
79        $x = new Date('');
80        $y = new Date('07 JAN 2019');
81        $age = new Age($x, $y);
82
83        self::assertSame(-1, $age->ageDays());
84        self::assertSame(-1, $age->ageYears());
85        self::assertSame('', $age->ageYearsString());
86        self::assertSame('', (string) $age);
87    }
88
89    public function testEndDateInvalid(): void
90    {
91        $x = new Date('07 JAN 2019');
92        $y = new Date('');
93        $age = new Age($x, $y);
94
95        self::assertSame(-1, $age->ageDays());
96        self::assertSame(-1, $age->ageYears());
97        self::assertSame('', $age->ageYearsString());
98        self::assertSame('', (string) $age);
99    }
100
101    public function testOverlappingDates1(): void
102    {
103        $x = new Date('07 JAN 2019');
104        $y = new Date('JAN 2019');
105        $age = new Age($x, $y);
106
107        self::assertSame(-6, $age->ageDays());
108        self::assertSame(0, $age->ageYears());
109        self::assertSame('0', $age->ageYearsString());
110        self::assertSame('0', (string) $age);
111    }
112
113    public function testOverlappingDates2(): void
114    {
115        $x = new Date('JAN 2019');
116        $y = new Date('07 JAN 2019');
117        $age = new Age($x, $y);
118
119        self::assertSame(6, $age->ageDays());
120        self::assertSame(0, $age->ageYears());
121        self::assertSame('0', $age->ageYearsString());
122        self::assertSame('0', (string) $age);
123    }
124
125    public function testDifferentDay(): void
126    {
127        $x = new Date('13 APR 2019');
128        $y = new Date('27 APR 2019');
129        $age = new Age($x, $y);
130
131        self::assertSame(14, $age->ageDays());
132        self::assertSame(0, $age->ageYears());
133        self::assertSame('0', $age->ageYearsString());
134        self::assertSame('14 days', (string) $age);
135    }
136
137    public function testDifferentMonth(): void
138    {
139        $x = new Date('13 APR 2019');
140        $y = new Date('27 JUN 2019');
141        $age = new Age($x, $y);
142
143        self::assertSame(75, $age->ageDays());
144        self::assertSame(0, $age->ageYears());
145        self::assertSame('0', $age->ageYearsString());
146        self::assertSame('2 months', (string) $age);
147    }
148
149    public function testDifferentYear(): void
150    {
151        $x = new Date('13 APR 2012');
152        $y = new Date('27 JUN 2019');
153        $age = new Age($x, $y);
154
155        self::assertSame(2631, $age->ageDays());
156        self::assertSame(7, $age->ageYears());
157        self::assertSame('7', $age->ageYearsString());
158        self::assertSame('7 years', (string) $age);
159    }
160}
161