Skip to content

Commit

Permalink
remove old clock and replace it with a service, also add decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBadura committed May 19, 2022
1 parent 4ac867a commit da785ab
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 146 deletions.
44 changes: 0 additions & 44 deletions src/Clock.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Clock/Clock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Clock;

use DateTimeImmutable;

interface Clock
{
public function createDateTimeImmutable(): DateTimeImmutable;
}
34 changes: 34 additions & 0 deletions src/Clock/FreezeClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Clock;

use DateTimeImmutable;

use function sprintf;

final class FreezeClock implements Clock
{
public function __construct(private DateTimeImmutable $frozenDateTime)
{
}

public function freeze(DateTimeImmutable $frozenDateTime): void
{
$this->frozenDateTime = $frozenDateTime;
}

/**
* @param positive-int $seconds
*/
public function sleep(int $seconds): void
{
$this->frozenDateTime = $this->frozenDateTime->modify(sprintf('+%s seconds', $seconds));
}

public function createDateTimeImmutable(): DateTimeImmutable
{
return $this->frozenDateTime;
}
}
15 changes: 15 additions & 0 deletions src/Clock/SystemClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Clock;

use DateTimeImmutable;

final class SystemClock implements Clock
{
public function createDateTimeImmutable(): DateTimeImmutable
{
return new DateTimeImmutable();
}
}
20 changes: 20 additions & 0 deletions src/EventBus/Decorator/RecordedOnDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\EventBus\Decorator;

use Patchlevel\EventSourcing\Clock\Clock;
use Patchlevel\EventSourcing\EventBus\Message;

final class RecordedOnDecorator implements MessageDecorator
{
public function __construct(private readonly Clock $clock)
{
}

public function __invoke(Message $message): Message
{
return $message->withRecordedOn($this->clock->createDateTimeImmutable());
}
}
4 changes: 2 additions & 2 deletions src/Repository/DefaultRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Patchlevel\EventSourcing\Repository;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Clock;
use Patchlevel\EventSourcing\EventBus\Decorator\MessageDecorator;
use Patchlevel\EventSourcing\EventBus\EventBus;
use Patchlevel\EventSourcing\EventBus\Message;
Expand Down Expand Up @@ -129,7 +129,7 @@ static function (object $event) use ($aggregate, &$playhead, $messageDecorator)
->withAggregateClass($aggregate::class)
->withAggregateId($aggregate->aggregateRootId())
->withPlayhead(++$playhead)
->withRecordedOn(Clock::createDateTimeImmutable());
->withRecordedOn(new DateTimeImmutable());

if ($messageDecorator) {
$message = $messageDecorator($message);
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/Clock/FreezeClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Clock;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock\FreezeClock;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Clock\FreezeClock */
class FreezeClockTest extends TestCase
{
public function testCreateDateTimeImmutableWithFrozenClock(): void
{
$current = new DateTimeImmutable();
$clock = new FreezeClock($current);

$new = $clock->createDateTimeImmutable();

self::assertSame($current, $new);
}

public function testSleep(): void
{
$date1 = new DateTimeImmutable();
$clock = new FreezeClock($date1);
$clock->sleep(1);
$date2 = $clock->createDateTimeImmutable();

$diff = $date1->diff($date2);

self::assertSame(1, $diff->s);
}

public function testReFreeze(): void
{
$date1 = new DateTimeImmutable();
$clock = new FreezeClock($date1);
$new1 = $clock->createDateTimeImmutable();

$date2 = new DateTimeImmutable();
$clock->freeze($date2);
$new2 = $clock->createDateTimeImmutable();

self::assertSame($date1, $new1);
self::assertSame($date2, $new2);
self::assertNotSame($new1, $new2);
}
}
23 changes: 23 additions & 0 deletions tests/Unit/Clock/SystemClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Clock;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock\SystemClock;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Clock\SystemClock */
class SystemClockTest extends TestCase
{
public function testCreateDateTimeImmutable(): void
{
$before = new DateTimeImmutable();
$date = (new SystemClock())->createDateTimeImmutable();
$after = new DateTimeImmutable();

self::assertGreaterThanOrEqual($before, $date);
self::assertLessThanOrEqual($after, $date);
}
}
84 changes: 0 additions & 84 deletions tests/Unit/ClockTest.php

This file was deleted.

16 changes: 0 additions & 16 deletions tests/Unit/EventBus/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Patchlevel\EventSourcing\Tests\Unit\EventBus;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock;
use Patchlevel\EventSourcing\EventBus\HeaderNotFound;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
Expand All @@ -17,17 +16,8 @@
/** @covers \Patchlevel\EventSourcing\EventBus\Message */
class MessageTest extends TestCase
{
public function tearDown(): void
{
Clock::reset();
}

public function testEmptyMessage(): void
{
$recordedAt = new DateTimeImmutable('2020-05-06 13:34:24');

Clock::freeze($recordedAt);

$id = ProfileId::fromString('1');
$email = Email::fromString('hallo@patchlevel.de');

Expand All @@ -47,8 +37,6 @@ public function testCreateMessageWithHeader(): void
{
$recordedAt = new DateTimeImmutable('2020-05-06 13:34:24');

Clock::freeze($recordedAt);

$id = ProfileId::fromString('1');
$email = Email::fromString('hallo@patchlevel.de');

Expand All @@ -74,8 +62,6 @@ public function testChangeHeader(): void
{
$recordedAt = new DateTimeImmutable('2020-05-06 13:34:24');

Clock::freeze($recordedAt);

$id = ProfileId::fromString('1');
$email = Email::fromString('hallo@patchlevel.de');

Expand Down Expand Up @@ -120,8 +106,6 @@ public function testCustomHeaders(): void
{
$recordedAt = new DateTimeImmutable('2020-05-06 13:34:24');

Clock::freeze($recordedAt);

$id = ProfileId::fromString('1');
$email = Email::fromString('hallo@patchlevel.de');

Expand Down

0 comments on commit da785ab

Please sign in to comment.