|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\EventSourcing\Tests\Unit\DecisionModel; |
| 6 | + |
| 7 | +use Patchlevel\EventSourcing\DecisionModel\StoreEventAppender; |
| 8 | +use Patchlevel\EventSourcing\Message\Message; |
| 9 | +use Patchlevel\EventSourcing\Serializer\EventTagExtractor; |
| 10 | +use Patchlevel\EventSourcing\Store\AppendCondition; |
| 11 | +use Patchlevel\EventSourcing\Store\AppendStore; |
| 12 | +use Patchlevel\EventSourcing\Store\Header\StreamNameHeader; |
| 13 | +use Patchlevel\EventSourcing\Store\Header\TagsHeader; |
| 14 | +use Patchlevel\EventSourcing\Store\Query; |
| 15 | +use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; |
| 16 | +use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; |
| 17 | +use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; |
| 18 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +#[CoversClass(StoreEventAppender::class)] |
| 22 | +final class StoreEventAppenderTest extends TestCase |
| 23 | +{ |
| 24 | + public function testAppendNothing(): void |
| 25 | + { |
| 26 | + $store = $this->createMock(AppendStore::class); |
| 27 | + $store->expects($this->never())->method('append'); |
| 28 | + |
| 29 | + $appender = new StoreEventAppender($store); |
| 30 | + |
| 31 | + $appender->append([]); |
| 32 | + } |
| 33 | + |
| 34 | + public function testAppendEventsWithoutCondition(): void |
| 35 | + { |
| 36 | + $expectedEvent = new ProfileCreated( |
| 37 | + ProfileId::fromString('1'), |
| 38 | + Email::fromString('info@patchlevel.de'), |
| 39 | + ); |
| 40 | + |
| 41 | + $expectedMessage = (new Message($expectedEvent)) |
| 42 | + ->withHeader(new TagsHeader([])) |
| 43 | + ->withHeader(new StreamNameHeader('main')); |
| 44 | + |
| 45 | + $store = $this->createMock(AppendStore::class); |
| 46 | + $store->expects($this->once())->method('append')->with([$expectedMessage], null); |
| 47 | + |
| 48 | + $appender = new StoreEventAppender($store); |
| 49 | + |
| 50 | + $appender->append([$expectedEvent]); |
| 51 | + } |
| 52 | + |
| 53 | + public function testAppendEventsWithCondition(): void |
| 54 | + { |
| 55 | + $appendCondition = new AppendCondition(new Query(), 5); |
| 56 | + |
| 57 | + $event = new ProfileCreated( |
| 58 | + ProfileId::fromString('1'), |
| 59 | + Email::fromString('info@patchlevel.de'), |
| 60 | + ); |
| 61 | + |
| 62 | + $message = (new Message($event)) |
| 63 | + ->withHeader(new TagsHeader([])) |
| 64 | + ->withHeader(new StreamNameHeader('main')); |
| 65 | + |
| 66 | + $store = $this->createMock(AppendStore::class); |
| 67 | + $store->expects($this->once())->method('append')->with([$message], $appendCondition); |
| 68 | + |
| 69 | + $appender = new StoreEventAppender($store); |
| 70 | + |
| 71 | + $appender->append([$event], $appendCondition); |
| 72 | + } |
| 73 | + |
| 74 | + public function testChangeDefaultStreamName(): void |
| 75 | + { |
| 76 | + $appendCondition = new AppendCondition(new Query(), 5); |
| 77 | + |
| 78 | + $event = new ProfileCreated( |
| 79 | + ProfileId::fromString('1'), |
| 80 | + Email::fromString('info@patchlevel.de'), |
| 81 | + ); |
| 82 | + |
| 83 | + $message = (new Message($event)) |
| 84 | + ->withHeader(new TagsHeader([])) |
| 85 | + ->withHeader(new StreamNameHeader('foo')); |
| 86 | + |
| 87 | + $store = $this->createMock(AppendStore::class); |
| 88 | + $store->expects($this->once())->method('append')->with([$message], $appendCondition); |
| 89 | + |
| 90 | + $appender = new StoreEventAppender($store, defaultStreamName: 'foo'); |
| 91 | + |
| 92 | + $appender->append([$event], $appendCondition); |
| 93 | + } |
| 94 | + |
| 95 | + public function testChangeStreamName(): void |
| 96 | + { |
| 97 | + $appendCondition = new AppendCondition(new Query(), 5); |
| 98 | + |
| 99 | + $event = new ProfileCreated( |
| 100 | + ProfileId::fromString('1'), |
| 101 | + Email::fromString('info@patchlevel.de'), |
| 102 | + ); |
| 103 | + |
| 104 | + $message = (new Message($event)) |
| 105 | + ->withHeader(new TagsHeader([])) |
| 106 | + ->withHeader(new StreamNameHeader('bar')); |
| 107 | + |
| 108 | + $store = $this->createMock(AppendStore::class); |
| 109 | + $store->expects($this->once())->method('append')->with([$message], $appendCondition); |
| 110 | + |
| 111 | + $appender = new StoreEventAppender($store, defaultStreamName: 'foo'); |
| 112 | + |
| 113 | + $appender->append([$event], $appendCondition, 'bar'); |
| 114 | + } |
| 115 | + |
| 116 | + public function testExtractTags(): void |
| 117 | + { |
| 118 | + $appendCondition = new AppendCondition(new Query(), 5); |
| 119 | + |
| 120 | + $event = new ProfileCreated( |
| 121 | + ProfileId::fromString('1'), |
| 122 | + Email::fromString('info@patchlevel.de'), |
| 123 | + ); |
| 124 | + |
| 125 | + $message = (new Message($event)) |
| 126 | + ->withHeader(new TagsHeader(['profile:1'])) |
| 127 | + ->withHeader(new StreamNameHeader('main')); |
| 128 | + |
| 129 | + $store = $this->createMock(AppendStore::class); |
| 130 | + $store->expects($this->once())->method('append')->with([$message], $appendCondition); |
| 131 | + |
| 132 | + $tagExtractor = $this->createMock(EventTagExtractor::class); |
| 133 | + $tagExtractor->expects($this->once())->method('extract')->with($event)->willReturn(['profile:1']); |
| 134 | + |
| 135 | + $appender = new StoreEventAppender($store, $tagExtractor); |
| 136 | + |
| 137 | + $appender->append([$event], $appendCondition); |
| 138 | + } |
| 139 | +} |
0 commit comments