-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e789fd
commit 555662a
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DR\PHPUnitExtensions\Symfony; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use PHPUnit\Framework\Attributes\Before; | ||
use Symfony\Component\Clock\Clock; | ||
use Symfony\Component\Clock\Test\ClockSensitiveTrait; | ||
|
||
trait ClockTestTrait | ||
{ | ||
use ClockSensitiveTrait; | ||
|
||
/** | ||
* @before | ||
*/ | ||
#[Before] | ||
protected function freezeTime(): void | ||
{ | ||
self::mockTime((new DateTimeImmutable())->setTimestamp($this->freezeTimeAt())); | ||
} | ||
|
||
/** | ||
* Override this method to set your own custom time freeze. | ||
*/ | ||
protected function freezeTimeAt(): int | ||
{ | ||
return 1634050575; // tuesday, 12-10-2021 16:56:15 +02:00 | ||
} | ||
|
||
/** | ||
* Returns the currently mocked time as timestamp | ||
*/ | ||
protected static function time(): int | ||
{ | ||
return Clock::get()->now()->getTimestamp(); | ||
} | ||
|
||
/** | ||
* Returns the currently mocked time as DateTimeImmutable object | ||
*/ | ||
protected static function now(): DateTimeImmutable | ||
{ | ||
return Clock::get()->now(); | ||
} | ||
|
||
/** | ||
* Sleep for a given amount of seconds. Use a float to sleep fractions of a second. | ||
*/ | ||
protected static function sleep(int|float $seconds): void | ||
{ | ||
Clock::get()->sleep($seconds); | ||
} | ||
|
||
/** | ||
* Asserts that the give timestamp of DateTime is equal to now | ||
*/ | ||
protected static function assertNow(int|DateTimeInterface $actual, string $message = ''): void | ||
{ | ||
self::assertSameTime(self::now(), $actual, $message); | ||
} | ||
|
||
/** | ||
* Asserts that two timestamps (or timestamp of DateTime) are equal. | ||
*/ | ||
protected static function assertSameTime(int|DateTimeInterface $expected, int|DateTimeInterface $actual, string $message = ''): void | ||
{ | ||
if ($expected instanceof DateTimeInterface) { | ||
$expected = $expected->getTimestamp(); | ||
} | ||
if ($actual instanceof DateTimeInterface) { | ||
$actual = $actual->getTimestamp(); | ||
} | ||
|
||
self::assertSame($expected, $actual, $message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DR\PHPUnitExtensions\Tests\Unit\Symfony; | ||
|
||
use DateTimeImmutable; | ||
use DR\PHPUnitExtensions\Symfony\ClockTestTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \DR\PHPUnitExtensions\Symfony\ClockTestTrait | ||
*/ | ||
class ClockTestTraitTest extends TestCase | ||
{ | ||
use ClockTestTrait; | ||
|
||
public function testTime(): void | ||
{ | ||
self::assertSame(1634050575, self::time()); | ||
} | ||
|
||
public function testNow(): void | ||
{ | ||
self::assertSame(1634050575, self::now()->getTimestamp()); | ||
} | ||
|
||
public function testSleep(): void | ||
{ | ||
self::sleep(123.456); | ||
self::assertSame(1634050698, self::time()); | ||
} | ||
|
||
public function testAssertNow(): void | ||
{ | ||
self::assertNow((new DateTimeImmutable())->setTimestamp(1634050575)); | ||
} | ||
|
||
public function testAssertTime(): void | ||
{ | ||
self::assertSameTime(1634050575, self::now()); | ||
} | ||
} |