Skip to content

Commit a99787c

Browse files
authored
Merge pull request #8593 from kenjis/test-fix-TimeTest
test: fix TimeTest
2 parents 4c7ca62 + a14ea36 commit a99787c

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

tests/system/I18n/TimeLegacyTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,15 @@ public function testGetTimestamp(): void
399399

400400
public function testGetAge(): void
401401
{
402+
// setTestNow() does not work to parse().
402403
$time = TimeLegacy::parse('5 years ago');
403404

404-
$this->assertSame(5, $time->getAge());
405-
$this->assertSame(5, $time->age);
405+
// Considers leap year
406+
$now = TimeLegacy::now();
407+
$expected = ($now->day === '29' && $now->month === '2') ? 4 : 5;
408+
409+
$this->assertSame($expected, $time->getAge());
410+
$this->assertSame($expected, $time->age);
406411
}
407412

408413
public function testAgeNow(): void
@@ -414,31 +419,31 @@ public function testAgeNow(): void
414419

415420
public function testAgeFuture(): void
416421
{
417-
TimeLegacy::setTestNow('June 20, 2022', 'America/Chicago');
422+
TimeLegacy::setTestNow('June 20, 2022');
418423
$time = TimeLegacy::parse('August 12, 2116 4:15:23pm');
419424

420425
$this->assertSame(0, $time->getAge());
421426
}
422427

423428
public function testGetAgeSameDayOfBirthday(): void
424429
{
425-
TimeLegacy::setTestNow('December 31, 2022', 'America/Chicago');
430+
TimeLegacy::setTestNow('December 31, 2022');
426431
$time = TimeLegacy::parse('December 31, 2020');
427432

428433
$this->assertSame(2, $time->getAge());
429434
}
430435

431436
public function testGetAgeNextDayOfBirthday(): void
432437
{
433-
TimeLegacy::setTestNow('January 1, 2022', 'America/Chicago');
438+
TimeLegacy::setTestNow('January 1, 2022');
434439
$time = TimeLegacy::parse('December 31, 2020');
435440

436441
$this->assertSame(1, $time->getAge());
437442
}
438443

439444
public function testGetAgeBeforeDayOfBirthday(): void
440445
{
441-
TimeLegacy::setTestNow('December 30, 2021', 'America/Chicago');
446+
TimeLegacy::setTestNow('December 30, 2021');
442447
$time = TimeLegacy::parse('December 31, 2020');
443448

444449
$this->assertSame(0, $time->getAge());

tests/system/I18n/TimeTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,15 @@ public function testGetTimestamp(): void
406406
*/
407407
public function testGetAge(): void
408408
{
409+
// setTestNow() does not work to parse().
409410
$time = Time::parse('5 years ago');
410411

411-
$this->assertSame(5, $time->getAge());
412-
$this->assertSame(5, $time->age);
412+
// Considers leap year
413+
$now = Time::now();
414+
$expected = ($now->day === '29' && $now->month === '2') ? 4 : 5;
415+
416+
$this->assertSame($expected, $time->getAge());
417+
$this->assertSame($expected, $time->age);
413418
}
414419

415420
public function testAgeNow(): void
@@ -421,31 +426,31 @@ public function testAgeNow(): void
421426

422427
public function testAgeFuture(): void
423428
{
424-
Time::setTestNow('June 20, 2022', 'America/Chicago');
429+
Time::setTestNow('June 20, 2022');
425430
$time = Time::parse('August 12, 2116 4:15:23pm');
426431

427432
$this->assertSame(0, $time->getAge());
428433
}
429434

430435
public function testGetAgeSameDayOfBirthday(): void
431436
{
432-
Time::setTestNow('December 31, 2022', 'America/Chicago');
437+
Time::setTestNow('December 31, 2022');
433438
$time = Time::parse('December 31, 2020');
434439

435440
$this->assertSame(2, $time->getAge());
436441
}
437442

438443
public function testGetAgeNextDayOfBirthday(): void
439444
{
440-
Time::setTestNow('January 1, 2022', 'America/Chicago');
445+
Time::setTestNow('January 1, 2022');
441446
$time = Time::parse('December 31, 2020');
442447

443448
$this->assertSame(1, $time->getAge());
444449
}
445450

446451
public function testGetAgeBeforeDayOfBirthday(): void
447452
{
448-
Time::setTestNow('December 30, 2021', 'America/Chicago');
453+
Time::setTestNow('December 30, 2021');
449454
$time = Time::parse('December 31, 2020');
450455

451456
$this->assertSame(0, $time->getAge());

0 commit comments

Comments
 (0)