-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from patchlevel/add-clock
add clock
- Loading branch information
Showing
2 changed files
with
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing; | ||
|
||
use DateTimeImmutable; | ||
|
||
use function sleep; | ||
use function sprintf; | ||
|
||
final class Clock | ||
{ | ||
private static ?DateTimeImmutable $frozenDateTime = null; | ||
|
||
public static function freeze(DateTimeImmutable $frozenDateTime): void | ||
{ | ||
self::$frozenDateTime = $frozenDateTime; | ||
} | ||
|
||
/** | ||
* @param positive-int $seconds | ||
*/ | ||
public static function sleep(int $seconds): void | ||
{ | ||
if (self::$frozenDateTime instanceof DateTimeImmutable) { | ||
self::$frozenDateTime = self::$frozenDateTime->modify(sprintf('+%s seconds', $seconds)); | ||
|
||
return; | ||
} | ||
|
||
sleep($seconds); | ||
} | ||
|
||
public static function createDateTimeImmutable(): DateTimeImmutable | ||
{ | ||
return self::$frozenDateTime ?: new DateTimeImmutable(); | ||
} | ||
|
||
public static function reset(): void | ||
{ | ||
self::$frozenDateTime = null; | ||
} | ||
} |
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,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit; | ||
|
||
use DateTimeImmutable; | ||
use Patchlevel\EventSourcing\Clock; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ClockTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
Clock::reset(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Clock::reset(); | ||
} | ||
|
||
public function testCreateDateTimeImmutable(): void | ||
{ | ||
$before = new DateTimeImmutable(); | ||
$date = Clock::createDateTimeImmutable(); | ||
$after = new DateTimeImmutable(); | ||
|
||
self::assertGreaterThanOrEqual($before, $date); | ||
self::assertLessThanOrEqual($after, $date); | ||
} | ||
|
||
public function testCreateDateTimeImmutableWithFrozenClock(): void | ||
{ | ||
$current = new DateTimeImmutable(); | ||
|
||
Clock::freeze($current); | ||
|
||
$new = Clock::createDateTimeImmutable(); | ||
|
||
self::assertEquals($current, $new); | ||
} | ||
|
||
public function testReset(): void | ||
{ | ||
$current = new DateTimeImmutable(); | ||
Clock::freeze($current); | ||
Clock::reset(); | ||
|
||
$before = new DateTimeImmutable(); | ||
$date = Clock::createDateTimeImmutable(); | ||
$after = new DateTimeImmutable(); | ||
|
||
self::assertNotEquals($current, $date); | ||
self::assertGreaterThanOrEqual($before, $date); | ||
self::assertLessThanOrEqual($after, $date); | ||
} | ||
|
||
public function testSleep(): void | ||
{ | ||
$date1 = Clock::createDateTimeImmutable(); | ||
Clock::sleep(1); | ||
$date2 = Clock::createDateTimeImmutable(); | ||
|
||
$diff = $date1->diff($date2); | ||
|
||
self::assertEquals(1, $diff->s); | ||
} | ||
|
||
public function testSleepWithFrozenClock(): void | ||
{ | ||
$current = new DateTimeImmutable(); | ||
Clock::freeze($current); | ||
|
||
$date1 = Clock::createDateTimeImmutable(); | ||
Clock::sleep(45); | ||
$date2 = Clock::createDateTimeImmutable(); | ||
|
||
$diff = $date1->diff($date2); | ||
|
||
self::assertEquals(45, $diff->s); | ||
} | ||
} |