Skip to content

Commit

Permalink
Merge pull request #95 from patchlevel/add-clock
Browse files Browse the repository at this point in the history
add clock
  • Loading branch information
DanielBadura authored Dec 4, 2021
2 parents 1e85f98 + 9cf20d1 commit d7e3979
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Clock.php
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;
}
}
83 changes: 83 additions & 0 deletions tests/Unit/ClockTest.php
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);
}
}

0 comments on commit d7e3979

Please sign in to comment.