-
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.
remove old clock and replace it with a service, also add decorator
- Loading branch information
1 parent
4ac867a
commit da785ab
Showing
10 changed files
with
156 additions
and
146 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Clock; | ||
|
||
use DateTimeImmutable; | ||
|
||
interface Clock | ||
{ | ||
public function createDateTimeImmutable(): DateTimeImmutable; | ||
} |
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,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; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Clock; | ||
|
||
use DateTimeImmutable; | ||
|
||
final class SystemClock implements Clock | ||
{ | ||
public function createDateTimeImmutable(): DateTimeImmutable | ||
{ | ||
return new DateTimeImmutable(); | ||
} | ||
} |
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,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()); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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