Skip to content

Commit 3a643cf

Browse files
authored
Merge pull request #55 from patchlevel/until-middleware
2 parents 1b4bee2 + ca49491 commit 3a643cf

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Pipeline\Middleware;
6+
7+
use DateTimeImmutable;
8+
use Patchlevel\EventSourcing\Pipeline\EventBucket;
9+
10+
class UntilEventMiddleware implements Middleware
11+
{
12+
private DateTimeImmutable $until;
13+
14+
public function __construct(DateTimeImmutable $until)
15+
{
16+
$this->until = $until;
17+
}
18+
19+
/**
20+
* @return list<EventBucket>
21+
*/
22+
public function __invoke(EventBucket $bucket): array
23+
{
24+
$recordedOn = $bucket->event()->recordedOn();
25+
26+
if ($recordedOn === null) {
27+
return [];
28+
}
29+
30+
if ($recordedOn < $this->until) {
31+
return [$bucket];
32+
}
33+
34+
return [];
35+
}
36+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Tests\Unit\Pipeline;
6+
7+
use DateTimeImmutable;
8+
use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
9+
use Patchlevel\EventSourcing\Pipeline\EventBucket;
10+
use Patchlevel\EventSourcing\Pipeline\Middleware\UntilEventMiddleware;
11+
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile;
12+
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class UntilEventMiddlewareTest extends TestCase
16+
{
17+
public function testPositive(): void
18+
{
19+
$until = new DateTimeImmutable('2020-02-02 00:00:00');
20+
21+
$middleware = new UntilEventMiddleware($until);
22+
23+
$bucket = new EventBucket(
24+
Profile::class,
25+
AggregateChanged::deserialize([
26+
'aggregateId' => '1',
27+
'playhead' => 0,
28+
'event' => ProfileCreated::class,
29+
'payload' => '{}',
30+
'recordedOn' => new DateTimeImmutable('2020-02-01 00:00:00'),
31+
])
32+
);
33+
34+
$result = $middleware($bucket);
35+
36+
self::assertEquals([$bucket], $result);
37+
}
38+
39+
public function testNegative(): void
40+
{
41+
$until = new DateTimeImmutable('2020-01-01 00:00:00');
42+
43+
$middleware = new UntilEventMiddleware($until);
44+
45+
$bucket = new EventBucket(
46+
Profile::class,
47+
AggregateChanged::deserialize([
48+
'aggregateId' => '1',
49+
'playhead' => 0,
50+
'event' => ProfileCreated::class,
51+
'payload' => '{}',
52+
'recordedOn' => new DateTimeImmutable('2020-02-01 00:00:00'),
53+
])
54+
);
55+
56+
$result = $middleware($bucket);
57+
58+
self::assertEquals([], $result);
59+
}
60+
61+
public function testNullEdgeCase(): void
62+
{
63+
$until = new DateTimeImmutable('2020-01-01 00:00:00');
64+
65+
$middleware = new UntilEventMiddleware($until);
66+
67+
$bucket = new EventBucket(
68+
Profile::class,
69+
AggregateChanged::deserialize([
70+
'aggregateId' => '1',
71+
'playhead' => 0,
72+
'event' => ProfileCreated::class,
73+
'payload' => '{}',
74+
'recordedOn' => null,
75+
])
76+
);
77+
78+
$result = $middleware($bucket);
79+
80+
self::assertEquals([], $result);
81+
}
82+
}

0 commit comments

Comments
 (0)