1d64ed531Ssibador<?php 2506a570fSGreg Roach 3d64ed531Ssibador/** 4d64ed531Ssibador * webtrees: online genealogy 5*d11be702SGreg 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 22204c2daeSGreg Roachuse function view; 23204c2daeSGreg Roach 24d64ed531Ssibador/** 25d64ed531Ssibador * Test the Age functions 26d64ed531Ssibador */ 27d64ed531Ssibadorclass AgeTest extends TestCase 28d64ed531Ssibador{ 29d64ed531Ssibador /** 30204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 31204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 32204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 33204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 34a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 35d64ed531Ssibador * 36d64ed531Ssibador * @return void 37d64ed531Ssibador */ 38204c2daeSGreg Roach public function testSameDayMonthYear(): void 39d64ed531Ssibador { 40204c2daeSGreg Roach $x = new Date('27 APR 2019'); 41204c2daeSGreg Roach $y = new Date('27 APR 2019'); 42204c2daeSGreg Roach $age = new Age($x, $y); 43204c2daeSGreg Roach 445e933c21SGreg Roach self::assertSame(0, $age->ageDays()); 455e933c21SGreg Roach self::assertSame(0, $age->ageYears()); 465e933c21SGreg Roach self::assertSame('0', $age->ageYearsString()); 47a5fd6d7cSGreg Roach self::assertSame('0 days', (string) $age); 48d64ed531Ssibador } 49d64ed531Ssibador 50d64ed531Ssibador /** 51d64ed531Ssibador * @covers \Fisharebest\Webtrees\Age::__construct 52204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 53204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 54204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 55a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 568a3784e1SGreg Roach * 57d64ed531Ssibador * @return void 58d64ed531Ssibador */ 59204c2daeSGreg Roach public function testSameMonthYear(): void 60d64ed531Ssibador { 61204c2daeSGreg Roach $x = new Date('APR 2019'); 62204c2daeSGreg Roach $y = new Date('APR 2019'); 63204c2daeSGreg Roach $age = new Age($x, $y); 64204c2daeSGreg Roach 655e933c21SGreg Roach self::assertSame(0, $age->ageDays()); 665e933c21SGreg Roach self::assertSame(0, $age->ageYears()); 675e933c21SGreg Roach self::assertSame('0', $age->ageYearsString()); 68a5fd6d7cSGreg Roach self::assertSame('0', (string) $age); 69d64ed531Ssibador } 70d64ed531Ssibador 71204c2daeSGreg Roach /** 72204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 73204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 74204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 75204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 76a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 77204c2daeSGreg Roach * 78204c2daeSGreg Roach * @return void 79204c2daeSGreg Roach */ 80204c2daeSGreg Roach public function testSameYear(): void 81d64ed531Ssibador { 82204c2daeSGreg Roach $x = new Date('2019'); 83204c2daeSGreg Roach $y = new Date('2019'); 84204c2daeSGreg Roach $age = new Age($x, $y); 85204c2daeSGreg Roach 865e933c21SGreg Roach self::assertSame(0, $age->ageDays()); 875e933c21SGreg Roach self::assertSame(0, $age->ageYears()); 885e933c21SGreg Roach self::assertSame('0', $age->ageYearsString()); 89a5fd6d7cSGreg Roach self::assertSame('0', (string) $age); 90204c2daeSGreg Roach } 91204c2daeSGreg Roach 92204c2daeSGreg Roach /** 93204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 94204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 95204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 96204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 97a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 98204c2daeSGreg Roach * 99204c2daeSGreg Roach * @return void 100204c2daeSGreg Roach */ 101204c2daeSGreg Roach public function testReversed(): void 102204c2daeSGreg Roach { 103204c2daeSGreg Roach $x = new Date('13 FEB 2019'); 104204c2daeSGreg Roach $y = new Date('07 JAN 2019'); 105204c2daeSGreg Roach $age = new Age($x, $y); 106204c2daeSGreg Roach 1075e933c21SGreg Roach self::assertSame(-37, $age->ageDays()); 1085e933c21SGreg Roach self::assertSame(-1, $age->ageYears()); 1095e933c21SGreg Roach self::assertSame(view('icons/warning'), $age->ageYearsString()); 110a5fd6d7cSGreg Roach self::assertSame(view('icons/warning'), (string) $age); 111204c2daeSGreg Roach } 112204c2daeSGreg Roach 113204c2daeSGreg Roach /** 114204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 115204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 116204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 117204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 118a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 119204c2daeSGreg Roach * 120204c2daeSGreg Roach * @return void 121204c2daeSGreg Roach */ 122204c2daeSGreg Roach public function testStartDateInvalid(): void 123204c2daeSGreg Roach { 124204c2daeSGreg Roach $x = new Date(''); 125204c2daeSGreg Roach $y = new Date('07 JAN 2019'); 126204c2daeSGreg Roach $age = new Age($x, $y); 127204c2daeSGreg Roach 1285e933c21SGreg Roach self::assertSame(-1, $age->ageDays()); 1295e933c21SGreg Roach self::assertSame(-1, $age->ageYears()); 1305e933c21SGreg Roach self::assertSame('', $age->ageYearsString()); 131a5fd6d7cSGreg Roach self::assertSame('', (string) $age); 132204c2daeSGreg Roach } 133204c2daeSGreg Roach 134204c2daeSGreg Roach /** 135204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 136204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 137204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 138204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 139a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 140204c2daeSGreg Roach * 141204c2daeSGreg Roach * @return void 142204c2daeSGreg Roach */ 143204c2daeSGreg Roach public function testEndDateInvalid(): void 144204c2daeSGreg Roach { 145204c2daeSGreg Roach $x = new Date('07 JAN 2019'); 146204c2daeSGreg Roach $y = new Date(''); 147204c2daeSGreg Roach $age = new Age($x, $y); 148204c2daeSGreg Roach 1495e933c21SGreg Roach self::assertSame(-1, $age->ageDays()); 1505e933c21SGreg Roach self::assertSame(-1, $age->ageYears()); 1515e933c21SGreg Roach self::assertSame('', $age->ageYearsString()); 152a5fd6d7cSGreg Roach self::assertSame('', (string) $age); 153204c2daeSGreg Roach } 154204c2daeSGreg Roach 155204c2daeSGreg Roach /** 156204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 157204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 158204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 159204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 160a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 161204c2daeSGreg Roach * 162204c2daeSGreg Roach * @return void 163204c2daeSGreg Roach */ 164204c2daeSGreg Roach public function testOverlappingDates1(): void 165204c2daeSGreg Roach { 166204c2daeSGreg Roach $x = new Date('07 JAN 2019'); 167204c2daeSGreg Roach $y = new Date('JAN 2019'); 168204c2daeSGreg Roach $age = new Age($x, $y); 169204c2daeSGreg Roach 1705e933c21SGreg Roach self::assertSame(-6, $age->ageDays()); 1715e933c21SGreg Roach self::assertSame(0, $age->ageYears()); 1725e933c21SGreg Roach self::assertSame('0', $age->ageYearsString()); 173a5fd6d7cSGreg Roach self::assertSame('0', (string) $age); 174204c2daeSGreg Roach } 175204c2daeSGreg Roach 176204c2daeSGreg Roach /** 177204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 178204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 179204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 180204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 181a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 182204c2daeSGreg Roach * 183204c2daeSGreg Roach * @return void 184204c2daeSGreg Roach */ 185204c2daeSGreg Roach public function testOverlappingDates2(): void 186204c2daeSGreg Roach { 187204c2daeSGreg Roach $x = new Date('JAN 2019'); 188204c2daeSGreg Roach $y = new Date('07 JAN 2019'); 189204c2daeSGreg Roach $age = new Age($x, $y); 190204c2daeSGreg Roach 1915e933c21SGreg Roach self::assertSame(6, $age->ageDays()); 1925e933c21SGreg Roach self::assertSame(0, $age->ageYears()); 1935e933c21SGreg Roach self::assertSame('0', $age->ageYearsString()); 194a5fd6d7cSGreg Roach self::assertSame('0', (string) $age); 195204c2daeSGreg Roach } 196204c2daeSGreg Roach 197204c2daeSGreg Roach /** 198204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 199204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 200204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 201204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 202a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 203204c2daeSGreg Roach * 204204c2daeSGreg Roach * @return void 205204c2daeSGreg Roach */ 206204c2daeSGreg Roach public function testDifferentDay(): void 207204c2daeSGreg Roach { 208204c2daeSGreg Roach $x = new Date('13 APR 2019'); 209204c2daeSGreg Roach $y = new Date('27 APR 2019'); 210204c2daeSGreg Roach $age = new Age($x, $y); 211204c2daeSGreg Roach 2125e933c21SGreg Roach self::assertSame(14, $age->ageDays()); 2135e933c21SGreg Roach self::assertSame(0, $age->ageYears()); 2145e933c21SGreg Roach self::assertSame('0', $age->ageYearsString()); 215a5fd6d7cSGreg Roach self::assertSame('14 days', (string) $age); 216204c2daeSGreg Roach } 217204c2daeSGreg Roach 218204c2daeSGreg Roach /** 219204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 220204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 221204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 222204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 223a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 224204c2daeSGreg Roach * 225204c2daeSGreg Roach * @return void 226204c2daeSGreg Roach */ 227204c2daeSGreg Roach public function testDifferentMonth(): void 228204c2daeSGreg Roach { 229204c2daeSGreg Roach $x = new Date('13 APR 2019'); 230204c2daeSGreg Roach $y = new Date('27 JUN 2019'); 231204c2daeSGreg Roach $age = new Age($x, $y); 232204c2daeSGreg Roach 2335e933c21SGreg Roach self::assertSame(75, $age->ageDays()); 2345e933c21SGreg Roach self::assertSame(0, $age->ageYears()); 2355e933c21SGreg Roach self::assertSame('0', $age->ageYearsString()); 236a5fd6d7cSGreg Roach self::assertSame('2 months', (string) $age); 237204c2daeSGreg Roach } 238204c2daeSGreg Roach 239204c2daeSGreg Roach /** 240204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::__construct 241204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageDays 242204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYears 243204c2daeSGreg Roach * @covers \Fisharebest\Webtrees\Age::ageYearsString 244a5fd6d7cSGreg Roach * @covers \Fisharebest\Webtrees\Age::__toString 245204c2daeSGreg Roach * 246204c2daeSGreg Roach * @return void 247204c2daeSGreg Roach */ 248204c2daeSGreg Roach public function testDifferentYear(): void 249204c2daeSGreg Roach { 250204c2daeSGreg Roach $x = new Date('13 APR 2012'); 251204c2daeSGreg Roach $y = new Date('27 JUN 2019'); 252204c2daeSGreg Roach $age = new Age($x, $y); 253204c2daeSGreg Roach 2545e933c21SGreg Roach self::assertSame(2631, $age->ageDays()); 2555e933c21SGreg Roach self::assertSame(7, $age->ageYears()); 2565e933c21SGreg Roach self::assertSame('7', $age->ageYearsString()); 257a5fd6d7cSGreg Roach self::assertSame('7 years', (string) $age); 258d64ed531Ssibador } 259d64ed531Ssibador} 260