-
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 #55 from patchlevel/until-middleware
- Loading branch information
Showing
2 changed files
with
118 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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Pipeline\Middleware; | ||
|
||
use DateTimeImmutable; | ||
use Patchlevel\EventSourcing\Pipeline\EventBucket; | ||
|
||
class UntilEventMiddleware implements Middleware | ||
{ | ||
private DateTimeImmutable $until; | ||
|
||
public function __construct(DateTimeImmutable $until) | ||
{ | ||
$this->until = $until; | ||
} | ||
|
||
/** | ||
* @return list<EventBucket> | ||
*/ | ||
public function __invoke(EventBucket $bucket): array | ||
{ | ||
$recordedOn = $bucket->event()->recordedOn(); | ||
|
||
if ($recordedOn === null) { | ||
return []; | ||
} | ||
|
||
if ($recordedOn < $this->until) { | ||
return [$bucket]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
tests/Unit/Pipeline/Middleware/UntilEventMiddlewareTest.php
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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\Pipeline; | ||
|
||
use DateTimeImmutable; | ||
use Patchlevel\EventSourcing\Aggregate\AggregateChanged; | ||
use Patchlevel\EventSourcing\Pipeline\EventBucket; | ||
use Patchlevel\EventSourcing\Pipeline\Middleware\UntilEventMiddleware; | ||
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile; | ||
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class UntilEventMiddlewareTest extends TestCase | ||
{ | ||
public function testPositive(): void | ||
{ | ||
$until = new DateTimeImmutable('2020-02-02 00:00:00'); | ||
|
||
$middleware = new UntilEventMiddleware($until); | ||
|
||
$bucket = new EventBucket( | ||
Profile::class, | ||
AggregateChanged::deserialize([ | ||
'aggregateId' => '1', | ||
'playhead' => 0, | ||
'event' => ProfileCreated::class, | ||
'payload' => '{}', | ||
'recordedOn' => new DateTimeImmutable('2020-02-01 00:00:00'), | ||
]) | ||
); | ||
|
||
$result = $middleware($bucket); | ||
|
||
self::assertEquals([$bucket], $result); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$until = new DateTimeImmutable('2020-01-01 00:00:00'); | ||
|
||
$middleware = new UntilEventMiddleware($until); | ||
|
||
$bucket = new EventBucket( | ||
Profile::class, | ||
AggregateChanged::deserialize([ | ||
'aggregateId' => '1', | ||
'playhead' => 0, | ||
'event' => ProfileCreated::class, | ||
'payload' => '{}', | ||
'recordedOn' => new DateTimeImmutable('2020-02-01 00:00:00'), | ||
]) | ||
); | ||
|
||
$result = $middleware($bucket); | ||
|
||
self::assertEquals([], $result); | ||
} | ||
|
||
public function testNullEdgeCase(): void | ||
{ | ||
$until = new DateTimeImmutable('2020-01-01 00:00:00'); | ||
|
||
$middleware = new UntilEventMiddleware($until); | ||
|
||
$bucket = new EventBucket( | ||
Profile::class, | ||
AggregateChanged::deserialize([ | ||
'aggregateId' => '1', | ||
'playhead' => 0, | ||
'event' => ProfileCreated::class, | ||
'payload' => '{}', | ||
'recordedOn' => null, | ||
]) | ||
); | ||
|
||
$result = $middleware($bucket); | ||
|
||
self::assertEquals([], $result); | ||
} | ||
} |