Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add clock #95

Merged
merged 2 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Aggregate/AggregateChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Patchlevel\EventSourcing\Aggregate;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock;
DavidBadura marked this conversation as resolved.
Show resolved Hide resolved

use function json_decode;
use function json_encode;
Expand Down Expand Up @@ -122,6 +123,6 @@ public function serialize(): array

protected function createRecordDate(): DateTimeImmutable
{
return new DateTimeImmutable();
return Clock::createDateTimeImmutable();
}
}
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;
}
}
37 changes: 19 additions & 18 deletions tests/Unit/Aggregate/AggregateChangedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use Error;
use Patchlevel\EventSourcing\Aggregate\AggregateException;
use Patchlevel\EventSourcing\Clock;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
Expand All @@ -16,6 +17,16 @@

class AggregateChangedTest extends TestCase
{
protected function setUp(): void
{
Clock::reset();
}

protected function tearDown(): void
{
Clock::reset();
}

public function testCreateEvent(): void
{
$id = ProfileId::fromString('1');
Expand All @@ -39,6 +50,9 @@ public function testCreateEvent(): void

public function testRecordNow(): void
{
$currentDate = new DateTimeImmutable('2020-01-01 12:00:00');
Clock::freeze($currentDate);

$id = ProfileId::fromString('1');
$email = Email::fromString('d.a.badura@gmail.com');

Expand All @@ -48,7 +62,7 @@ public function testRecordNow(): void
self::assertEquals($id, $recordedEvent->profileId());
self::assertEquals($email, $recordedEvent->email());
self::assertEquals(1, $recordedEvent->playhead());
self::assertInstanceOf(DateTimeImmutable::class, $recordedEvent->recordedOn());
self::assertEquals($currentDate, $recordedEvent->recordedOn());
self::assertEquals(
[
'profileId' => '1',
Expand All @@ -74,14 +88,14 @@ public function testEventAlreadyBeenRecorded(): void

public function testSerialize(): void
{
$currentDate = new DateTimeImmutable('2020-01-01 12:00:00');
Clock::freeze($currentDate);

$id = ProfileId::fromString('1');
$email = Email::fromString('d.a.badura@gmail.com');

$event = ProfileCreated::raise($id, $email);

$beforeRecording = new DateTimeImmutable();
$recordedEvent = $event->recordNow(1);
$afterRecording = new DateTimeImmutable();

$serializedEvent = $recordedEvent->serialize();

Expand All @@ -103,11 +117,7 @@ public function testSerialize(): void
self::assertEquals('{"profileId":"1","email":"d.a.badura@gmail.com"}', $serializedEvent['payload']);

self::assertArrayHasKey('recordedOn', $serializedEvent);
self::assertDateTimeImmutableBetween(
$beforeRecording,
$afterRecording,
$serializedEvent['recordedOn'],
);
self::assertEquals($currentDate, $serializedEvent['recordedOn']);
}

public function testSerializeNotRecorded(): void
Expand Down Expand Up @@ -190,13 +200,4 @@ public function testDeserializeAndSerialize(): void
$event->payload()
);
}

private static function assertDateTimeImmutableBetween(
DateTimeImmutable $fromExpected,
DateTimeImmutable $toExpected,
DateTimeImmutable $actual
): void {
self::assertGreaterThanOrEqual($fromExpected, $actual);
self::assertLessThanOrEqual($toExpected, $actual);
}
}
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);
}
}