Skip to content

Commit

Permalink
Merge pull request #55 from patchlevel/until-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBadura authored Mar 15, 2021
2 parents 1b4bee2 + ca49491 commit 3a643cf
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Pipeline/Middleware/UntilEventMiddleware.php
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 tests/Unit/Pipeline/Middleware/UntilEventMiddlewareTest.php
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);
}
}

0 comments on commit 3a643cf

Please sign in to comment.