Convert test to use DataProvider. Add cleanup TODO to relativeTime
This commit is contained in:
parent
81123945f4
commit
7b7f8d205d
@ -30,6 +30,8 @@ class Util {
|
|||||||
|
|
||||||
// For relative time display, compare the stored time to the current time
|
// For relative time display, compare the stored time to the current time
|
||||||
// and display it as "X seconds/minutes/hours/days etc." ago
|
// and display it as "X seconds/minutes/hours/days etc." ago
|
||||||
|
//
|
||||||
|
// TODO: Convert to either accepting a DateTime or use DateTime->fromFormat()
|
||||||
public static function relative_time(string $tickTime): string {
|
public static function relative_time(string $tickTime): string {
|
||||||
$datetime = new DateTime($tickTime);
|
$datetime = new DateTime($tickTime);
|
||||||
$now = new DateTime('now', $datetime->getTimezone());
|
$now = new DateTime('now', $datetime->getTimezone());
|
||||||
|
@ -1,31 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
|
||||||
final class UtilTest extends TestCase
|
final class UtilTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testCanDisplayRelativeTime(): void
|
// Define test date (strings) and expected outputs for
|
||||||
{
|
// testCanDisplayRelativeTime
|
||||||
|
public static function dateProvider(): array {
|
||||||
$datetime = new DateTimeImmutable();
|
$datetime = new DateTimeImmutable();
|
||||||
|
|
||||||
$oneMinuteAgo = $datetime->modify('-1 minute')->format('c');
|
return [
|
||||||
$relativeTime = Util::relative_time($oneMinuteAgo);
|
'1 minute ago' => [$datetime->modify('-1 minute')->format('c'), '1 minute ago'],
|
||||||
$this->assertSame($relativeTime, "1 minute ago");
|
'2 hours ago' => [$datetime->modify('-2 hours')->format('c'), '2 hours ago'],
|
||||||
|
'3 days ago' => [$datetime->modify('-3 days')->format('c'), '3 days ago'],
|
||||||
|
'4 months ago' => [$datetime->modify('-4 months')->format('c'), '4 months ago'],
|
||||||
|
'5 years ago' => [$datetime->modify('-5 years')->format('c'), '5 years ago']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
$twoHoursAgo = $datetime->modify('-2 hours')->format('c');
|
// Validate that the datetime strings provided by dateProvider
|
||||||
$relativeTime = Util::relative_time($twoHoursAgo);
|
// yield the expected display strings
|
||||||
$this->assertSame($relativeTime, "2 hours ago");
|
#[DataProvider('dateProvider')]
|
||||||
|
public function testCanDisplayRelativeTime(string $datetimeString, string $display): void {
|
||||||
$threeDaysAgo = $datetime->modify('-3 days')->format('c');
|
$relativeTime = Util::relative_time($datetimeString);
|
||||||
$relativeTime = Util::relative_time($threeDaysAgo);
|
$this->assertSame($relativeTime, $display);
|
||||||
$this->assertSame($relativeTime, "3 days ago");
|
}
|
||||||
|
|
||||||
$fourMonthsAgo = $datetime->modify('-4 months')->format('c');
|
|
||||||
$relativeTime = Util::relative_time($fourMonthsAgo);
|
|
||||||
$this->assertSame($relativeTime, "4 months ago");
|
|
||||||
|
|
||||||
$fiveYearsAgo = $datetime->modify('-5 years')->format('c');
|
|
||||||
$relativeTime = Util::relative_time($fiveYearsAgo);
|
|
||||||
$this->assertSame($relativeTime, "5 years ago");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user