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
 | ||||
|     // 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 { | ||||
|         $datetime = new DateTime($tickTime); | ||||
|         $now = new DateTime('now', $datetime->getTimezone()); | ||||
|  | ||||
| @ -1,31 +1,29 @@ | ||||
| <?php | ||||
| use PHPUnit\Framework\TestCase; | ||||
| use PHPUnit\Framework\Attributes\DataProvider; | ||||
| 
 | ||||
| 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(); | ||||
| 
 | ||||
|         $oneMinuteAgo = $datetime->modify('-1 minute')->format('c'); | ||||
|         $relativeTime = Util::relative_time($oneMinuteAgo); | ||||
|         $this->assertSame($relativeTime, "1 minute ago"); | ||||
| 
 | ||||
|         $twoHoursAgo = $datetime->modify('-2 hours')->format('c'); | ||||
|         $relativeTime = Util::relative_time($twoHoursAgo); | ||||
|         $this->assertSame($relativeTime, "2 hours ago"); | ||||
| 
 | ||||
|         $threeDaysAgo = $datetime->modify('-3 days')->format('c'); | ||||
|         $relativeTime = Util::relative_time($threeDaysAgo); | ||||
|         $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"); | ||||
| 
 | ||||
|         return [ | ||||
|             '1 minute ago' => [$datetime->modify('-1 minute')->format('c'), '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'] | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     // Validate that the datetime strings provided by dateProvider
 | ||||
|     // yield the expected display strings
 | ||||
|     #[DataProvider('dateProvider')]
 | ||||
|     public function testCanDisplayRelativeTime(string $datetimeString, string $display): void { | ||||
|         $relativeTime = Util::relative_time($datetimeString); | ||||
|         $this->assertSame($relativeTime, $display); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user