From e3c4560ad06165ca432f9dad0081884bfd351476 Mon Sep 17 00:00:00 2001 From: David Badura Date: Wed, 3 Apr 2024 13:46:07 +0200 Subject: [PATCH] add has header and add only header if used --- docs/pages/pipeline.md | 8 ++--- src/Message/Message.php | 6 ++++ .../ExcludeArchivedEventTranslator.php | 27 -------------- .../ExcludeEventWithHeaderTranslator.php | 26 ++++++++++++++ .../IncludeEventWithHeaderTranslator.php | 26 ++++++++++++++ .../OnlyArchivedEventTranslator.php | 27 -------------- .../Message/MessageHeaderRegistry.php | 4 +-- src/Repository/DefaultRepository.php | 11 ++---- .../MessageDecorator/SplitStreamDecorator.php | 8 +++-- src/Store/ArchivedHeader.php | 4 --- src/Store/DoctrineDbalStore.php | 34 ++++++------------ src/Store/DoctrineDbalStoreStream.php | 21 +++++++---- src/Store/DoctrineHelper.php | 23 ------------ src/Store/NewStreamStartHeader.php | 14 -------- src/Store/StreamStartHeader.php | 10 ++++++ tests/Unit/Message/MessageTest.php | 14 ++++---- .../Serializer/HeadersSerializerTest.php | 8 ++--- ... ExcludeEventWithHeaderTranslatorTest.php} | 28 ++++----------- ... IncludeEventWithHeaderTranslatorTest.php} | 30 ++++------------ .../Unit/Repository/DefaultRepositoryTest.php | 4 +-- .../SplitStreamDecoratorTest.php | 6 ++-- tests/Unit/Store/ArchivedHeaderTest.php | 19 ---------- tests/Unit/Store/DoctrineDbalStoreTest.php | 2 -- tests/Unit/Store/DoctrineDbalStreamTest.php | 35 ++++--------------- tests/Unit/Store/DoctrineHelperTest.php | 34 ------------------ tests/Unit/Store/NewStreamStartHeaderTest.php | 19 ---------- 26 files changed, 142 insertions(+), 306 deletions(-) delete mode 100644 src/Message/Translator/ExcludeArchivedEventTranslator.php create mode 100644 src/Message/Translator/ExcludeEventWithHeaderTranslator.php create mode 100644 src/Message/Translator/IncludeEventWithHeaderTranslator.php delete mode 100644 src/Message/Translator/OnlyArchivedEventTranslator.php delete mode 100644 src/Store/NewStreamStartHeader.php create mode 100644 src/Store/StreamStartHeader.php rename tests/Unit/Message/Translator/{ExcludeArchivedEventTranslatorTest.php => ExcludeEventWithHeaderTranslatorTest.php} (59%) rename tests/Unit/Message/Translator/{OnlyArchivedEventTranslatorTest.php => IncludeEventWithHeaderTranslatorTest.php} (56%) delete mode 100644 tests/Unit/Store/ArchivedHeaderTest.php delete mode 100644 tests/Unit/Store/NewStreamStartHeaderTest.php diff --git a/docs/pages/pipeline.md b/docs/pages/pipeline.md index 2a58c68f..94e8e060 100644 --- a/docs/pages/pipeline.md +++ b/docs/pages/pipeline.md @@ -245,9 +245,9 @@ $middleware = new FilterEventTranslator(static function (AggregateChanged $event With this middleware you can exclude archived events. ```php -use Patchlevel\EventSourcing\Message\Translator\ExcludeArchivedEventTranslator; +use Patchlevel\EventSourcing\Message\Translator\ExcludeEventWithHeaderTranslator; -$middleware = new ExcludeArchivedEventTranslator(); +$middleware = new ExcludeEventWithHeaderTranslator(); ``` !!! warning @@ -258,9 +258,9 @@ $middleware = new ExcludeArchivedEventTranslator(); With this middleware you can only allow archived events. ```php -use Patchlevel\EventSourcing\Message\Translator\OnlyArchivedEventTranslator; +use Patchlevel\EventSourcing\Message\Translator\IncludeEventWithHeaderTranslator; -$middleware = new OnlyArchivedEventTranslator(); +$middleware = new IncludeEventWithHeaderTranslator(); ``` !!! warning diff --git a/src/Message/Message.php b/src/Message/Message.php index 5c6249b4..40635c26 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -70,6 +70,12 @@ public function header(string $name): object return $header; } + /** @param class-string $name */ + public function hasHeader(string $name): bool + { + return array_key_exists($name, $this->headers); + } + public function withHeader(object $header): self { $message = clone $this; diff --git a/src/Message/Translator/ExcludeArchivedEventTranslator.php b/src/Message/Translator/ExcludeArchivedEventTranslator.php deleted file mode 100644 index 9bc904da..00000000 --- a/src/Message/Translator/ExcludeArchivedEventTranslator.php +++ /dev/null @@ -1,27 +0,0 @@ - */ - public function __invoke(Message $message): array - { - try { - $archivedHeader = $message->header(ArchivedHeader::class); - - if ($archivedHeader->archived) { - return []; - } - } catch (HeaderNotFound) { - } - - return [$message]; - } -} diff --git a/src/Message/Translator/ExcludeEventWithHeaderTranslator.php b/src/Message/Translator/ExcludeEventWithHeaderTranslator.php new file mode 100644 index 00000000..4fa4b785 --- /dev/null +++ b/src/Message/Translator/ExcludeEventWithHeaderTranslator.php @@ -0,0 +1,26 @@ + */ + public function __invoke(Message $message): array + { + if ($message->hasHeader($this->header)) { + return []; + } + + return [$message]; + } +} diff --git a/src/Message/Translator/IncludeEventWithHeaderTranslator.php b/src/Message/Translator/IncludeEventWithHeaderTranslator.php new file mode 100644 index 00000000..8c88514a --- /dev/null +++ b/src/Message/Translator/IncludeEventWithHeaderTranslator.php @@ -0,0 +1,26 @@ + */ + public function __invoke(Message $message): array + { + if ($message->hasHeader($this->header)) { + return [$message]; + } + + return []; + } +} diff --git a/src/Message/Translator/OnlyArchivedEventTranslator.php b/src/Message/Translator/OnlyArchivedEventTranslator.php deleted file mode 100644 index b1db0ed8..00000000 --- a/src/Message/Translator/OnlyArchivedEventTranslator.php +++ /dev/null @@ -1,27 +0,0 @@ - */ - public function __invoke(Message $message): array - { - try { - $archivedHeader = $message->header(ArchivedHeader::class); - - if ($archivedHeader->archived) { - return [$message]; - } - } catch (HeaderNotFound) { - } - - return []; - } -} diff --git a/src/Metadata/Message/MessageHeaderRegistry.php b/src/Metadata/Message/MessageHeaderRegistry.php index d4bdb56d..934f0e62 100644 --- a/src/Metadata/Message/MessageHeaderRegistry.php +++ b/src/Metadata/Message/MessageHeaderRegistry.php @@ -7,7 +7,7 @@ use Patchlevel\EventSourcing\Aggregate\AggregateHeader; use Patchlevel\EventSourcing\Debug\Trace\TraceHeader; use Patchlevel\EventSourcing\Store\ArchivedHeader; -use Patchlevel\EventSourcing\Store\NewStreamStartHeader; +use Patchlevel\EventSourcing\Store\StreamStartHeader; use function array_flip; use function array_key_exists; @@ -76,7 +76,7 @@ public static function createWithInternalHeaders(array $headerNameToClassMap = [ 'aggregate' => AggregateHeader::class, 'trace' => TraceHeader::class, 'archived' => ArchivedHeader::class, - 'newStreamStart' => NewStreamStartHeader::class, + 'newStreamStart' => StreamStartHeader::class, ]; return new self($headerNameToClassMap + $internalHeaders); diff --git a/src/Repository/DefaultRepository.php b/src/Repository/DefaultRepository.php index 75019e6c..621c284e 100644 --- a/src/Repository/DefaultRepository.php +++ b/src/Repository/DefaultRepository.php @@ -9,7 +9,6 @@ use Patchlevel\EventSourcing\Aggregate\AggregateRootId; use Patchlevel\EventSourcing\Clock\SystemClock; use Patchlevel\EventSourcing\EventBus\EventBus; -use Patchlevel\EventSourcing\Message\HeaderNotFound; use Patchlevel\EventSourcing\Message\Message; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootMetadata; use Patchlevel\EventSourcing\Repository\MessageDecorator\MessageDecorator; @@ -18,9 +17,9 @@ use Patchlevel\EventSourcing\Snapshot\SnapshotVersionInvalid; use Patchlevel\EventSourcing\Store\ArchivableStore; use Patchlevel\EventSourcing\Store\CriteriaBuilder; -use Patchlevel\EventSourcing\Store\NewStreamStartHeader; use Patchlevel\EventSourcing\Store\Store; use Patchlevel\EventSourcing\Store\Stream; +use Patchlevel\EventSourcing\Store\StreamStartHeader; use Patchlevel\EventSourcing\Store\UniqueConstraintViolation; use Psr\Clock\ClockInterface; use Psr\Log\LoggerInterface; @@ -377,13 +376,7 @@ private function archive(Message ...$messages): void $lastMessageWithNewStreamStart = null; foreach ($messages as $message) { - try { - $newStreamStartHeader = $message->header(NewStreamStartHeader::class); - } catch (HeaderNotFound) { - continue; - } - - if (!$newStreamStartHeader->newStreamStart) { + if (!$message->hasHeader(StreamStartHeader::class)) { continue; } diff --git a/src/Repository/MessageDecorator/SplitStreamDecorator.php b/src/Repository/MessageDecorator/SplitStreamDecorator.php index 61a6e6a2..b2e48224 100644 --- a/src/Repository/MessageDecorator/SplitStreamDecorator.php +++ b/src/Repository/MessageDecorator/SplitStreamDecorator.php @@ -6,7 +6,7 @@ use Patchlevel\EventSourcing\Message\Message; use Patchlevel\EventSourcing\Metadata\Event\EventMetadataFactory; -use Patchlevel\EventSourcing\Store\NewStreamStartHeader; +use Patchlevel\EventSourcing\Store\StreamStartHeader; final class SplitStreamDecorator implements MessageDecorator { @@ -20,6 +20,10 @@ public function __invoke(Message $message): Message $event = $message->event(); $metadata = $this->eventMetadataFactory->metadata($event::class); - return $message->withHeader(new NewStreamStartHeader($metadata->splitStream)); + if (!$metadata->splitStream) { + return $message; + } + + return $message->withHeader(new StreamStartHeader()); } } diff --git a/src/Store/ArchivedHeader.php b/src/Store/ArchivedHeader.php index d6590603..6b6b5805 100644 --- a/src/Store/ArchivedHeader.php +++ b/src/Store/ArchivedHeader.php @@ -7,8 +7,4 @@ /** @psalm-immutable */ final class ArchivedHeader { - public function __construct( - public readonly bool $archived, - ) { - } } diff --git a/src/Store/DoctrineDbalStore.php b/src/Store/DoctrineDbalStore.php index 437d0ca2..fd706276 100644 --- a/src/Store/DoctrineDbalStore.php +++ b/src/Store/DoctrineDbalStore.php @@ -174,35 +174,23 @@ function (Connection $connection) use ($messages): void { try { $aggregateHeader = $message->header(AggregateHeader::class); - - $parameters[] = $aggregateHeader->aggregateName; - $parameters[] = $aggregateHeader->aggregateId; - $parameters[] = $aggregateHeader->playhead; - $parameters[] = $data->name; - $parameters[] = $data->payload; - - $parameters[] = $aggregateHeader->recordedOn; - $types[$offset + 5] = $dateTimeType; } catch (HeaderNotFound $e) { throw new MissingDataForStorage($e->name, $e); } - try { - $newStreamStart = $message->header(NewStreamStartHeader::class)->newStreamStart; - } catch (HeaderNotFound) { - $newStreamStart = false; - } + $parameters[] = $aggregateHeader->aggregateName; + $parameters[] = $aggregateHeader->aggregateId; + $parameters[] = $aggregateHeader->playhead; + $parameters[] = $data->name; + $parameters[] = $data->payload; - $parameters[] = $newStreamStart; - $types[$offset + 6] = $booleanType; + $parameters[] = $aggregateHeader->recordedOn; + $types[$offset + 5] = $dateTimeType; - try { - $archived = $message->header(ArchivedHeader::class)->archived; - } catch (HeaderNotFound) { - $archived = false; - } + $parameters[] = $message->hasHeader(StreamStartHeader::class); + $types[$offset + 6] = $booleanType; - $parameters[] = $archived; + $parameters[] = $message->hasHeader(ArchivedHeader::class); $types[$offset + 7] = $booleanType; $parameters[] = $this->headersSerializer->serialize($this->getCustomHeaders($message)); @@ -305,7 +293,7 @@ private function getCustomHeaders(Message $message): array { $filteredHeaders = [ AggregateHeader::class, - NewStreamStartHeader::class, + StreamStartHeader::class, ArchivedHeader::class, ]; diff --git a/src/Store/DoctrineDbalStoreStream.php b/src/Store/DoctrineDbalStoreStream.php index 75634624..ae4a5ec5 100644 --- a/src/Store/DoctrineDbalStoreStream.php +++ b/src/Store/DoctrineDbalStoreStream.php @@ -120,18 +120,25 @@ private function buildGenerator( $this->index = $data['id']; $event = $eventSerializer->deserialize(new SerializedEvent($data['event'], $data['payload'])); - $customHeaders = $headersSerializer->deserialize(DoctrineHelper::normalizeCustomHeaders($data['custom_headers'], $platform)); - - yield Message::create($event) + $message = Message::create($event) ->withHeader(new AggregateHeader( $data['aggregate'], $data['aggregate_id'], DoctrineHelper::normalizePlayhead($data['playhead'], $platform), DoctrineHelper::normalizeRecordedOn($data['recorded_on'], $platform), - )) - ->withHeader(new ArchivedHeader(DoctrineHelper::normalizeArchived($data['archived'], $platform))) - ->withHeader(new NewStreamStartHeader(DoctrineHelper::normalizeNewStreamStart($data['new_stream_start'], $platform))) - ->withHeaders($customHeaders); + )); + + if ($data['archived']) { + $message = $message->withHeader(new ArchivedHeader()); + } + + if ($data['new_stream_start']) { + $message = $message->withHeader(new StreamStartHeader()); + } + + $customHeaders = $headersSerializer->deserialize(DoctrineHelper::normalizeCustomHeaders($data['custom_headers'], $platform)); + + yield $message->withHeaders($customHeaders); } } diff --git a/src/Store/DoctrineHelper.php b/src/Store/DoctrineHelper.php index 6b87a3de..957c93ea 100644 --- a/src/Store/DoctrineHelper.php +++ b/src/Store/DoctrineHelper.php @@ -10,7 +10,6 @@ use Doctrine\DBAL\Types\Types; use function is_array; -use function is_bool; use function is_int; final class DoctrineHelper @@ -49,26 +48,4 @@ public static function normalizeCustomHeaders(string $customHeaders, AbstractPla return $normalizedCustomHeaders; } - - public static function normalizeArchived(mixed $value, AbstractPlatform $platform): bool - { - $normalizedValue = Type::getType(Types::BOOLEAN)->convertToPHPValue($value, $platform); - - if (!is_bool($normalizedValue)) { - throw new InvalidType('archived', 'boolean'); - } - - return $normalizedValue; - } - - public static function normalizeNewStreamStart(mixed $value, AbstractPlatform $platform): bool - { - $normalizedValue = Type::getType(Types::BOOLEAN)->convertToPHPValue($value, $platform); - - if (!is_bool($normalizedValue)) { - throw new InvalidType('new_stream_start', 'boolean'); - } - - return $normalizedValue; - } } diff --git a/src/Store/NewStreamStartHeader.php b/src/Store/NewStreamStartHeader.php deleted file mode 100644 index 0e84f722..00000000 --- a/src/Store/NewStreamStartHeader.php +++ /dev/null @@ -1,14 +0,0 @@ -withHeader(new NewStreamStartHeader(true)) - ->withHeader(new ArchivedHeader(true)); + ->withHeader(new StreamStartHeader()) + ->withHeader(new ArchivedHeader()); self::assertEquals( [ @@ -61,8 +61,8 @@ public function testAllHeaders(): void 3, $recordedAt, ), - new NewStreamStartHeader(true), - new ArchivedHeader(true), + new StreamStartHeader(), + new ArchivedHeader(), ], $message->headers(), ); @@ -85,8 +85,8 @@ public function testCreateWithAllHeaders(): void 3, new DateTimeImmutable('2020-05-06 13:34:24'), ), - new NewStreamStartHeader(true), - new ArchivedHeader(true), + new StreamStartHeader(), + new ArchivedHeader(), ]; $message = Message::createWithHeaders( diff --git a/tests/Unit/Message/Serializer/HeadersSerializerTest.php b/tests/Unit/Message/Serializer/HeadersSerializerTest.php index 11951382..f23fcbe7 100644 --- a/tests/Unit/Message/Serializer/HeadersSerializerTest.php +++ b/tests/Unit/Message/Serializer/HeadersSerializerTest.php @@ -27,13 +27,13 @@ public function testSerialize(): void $content = $serializer->serialize([ new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2020-01-01T20:00:00.000000+0100')), - new ArchivedHeader(false), + new ArchivedHeader(), ]); self::assertEquals( [ 'aggregate' => '{"aggregateName":"profile","aggregateId":"1","playhead":1,"recordedOn":"2020-01-01T20:00:00+01:00"}', - 'archived' => '{"archived":false}', + 'archived' => '[]', ], $content, ); @@ -52,14 +52,14 @@ public function testDeserialize(): void $deserializedMessage = $serializer->deserialize( [ 'aggregate' => '{"aggregateName":"profile","aggregateId":"1","playhead":1,"recordedOn":"2020-01-01T20:00:00+01:00"}', - 'archived' => '{"archived":false}', + 'archived' => '[]', ], ); self::assertEquals( [ new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2020-01-01T20:00:00.000000+0100')), - new ArchivedHeader(false), + new ArchivedHeader(), ], $deserializedMessage, ); diff --git a/tests/Unit/Message/Translator/ExcludeArchivedEventTranslatorTest.php b/tests/Unit/Message/Translator/ExcludeEventWithHeaderTranslatorTest.php similarity index 59% rename from tests/Unit/Message/Translator/ExcludeArchivedEventTranslatorTest.php rename to tests/Unit/Message/Translator/ExcludeEventWithHeaderTranslatorTest.php index 18128065..f1139f69 100644 --- a/tests/Unit/Message/Translator/ExcludeArchivedEventTranslatorTest.php +++ b/tests/Unit/Message/Translator/ExcludeEventWithHeaderTranslatorTest.php @@ -5,26 +5,26 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Message\Translator; use Patchlevel\EventSourcing\Message\Message; -use Patchlevel\EventSourcing\Message\Translator\ExcludeArchivedEventTranslator; +use Patchlevel\EventSourcing\Message\Translator\ExcludeEventWithHeaderTranslator; use Patchlevel\EventSourcing\Store\ArchivedHeader; use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; use PHPUnit\Framework\TestCase; -/** @covers \Patchlevel\EventSourcing\Message\Translator\ExcludeArchivedEventTranslator */ -final class ExcludeArchivedEventTranslatorTest extends TestCase +/** @covers \Patchlevel\EventSourcing\Message\Translator\ExcludeEventWithHeaderTranslator */ +final class ExcludeEventWithHeaderTranslatorTest extends TestCase { public function testExcludedEvent(): void { - $translator = new ExcludeArchivedEventTranslator(); + $translator = new ExcludeEventWithHeaderTranslator(ArchivedHeader::class); $message = Message::create( new ProfileCreated( ProfileId::fromString('1'), Email::fromString('hallo@patchlevel.de'), ), - )->withHeader(new ArchivedHeader(true)); + )->withHeader(new ArchivedHeader()); $result = $translator($message); @@ -33,23 +33,7 @@ public function testExcludedEvent(): void public function testIncludeEvent(): void { - $translator = new ExcludeArchivedEventTranslator(); - - $message = Message::create( - new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('hallo@patchlevel.de'), - ), - )->withHeader(new ArchivedHeader(false)); - - $result = $translator($message); - - self::assertSame([$message], $result); - } - - public function testHeaderNotSet(): void - { - $translator = new ExcludeArchivedEventTranslator(); + $translator = new ExcludeEventWithHeaderTranslator(ArchivedHeader::class); $message = Message::create( new ProfileCreated( diff --git a/tests/Unit/Message/Translator/OnlyArchivedEventTranslatorTest.php b/tests/Unit/Message/Translator/IncludeEventWithHeaderTranslatorTest.php similarity index 56% rename from tests/Unit/Message/Translator/OnlyArchivedEventTranslatorTest.php rename to tests/Unit/Message/Translator/IncludeEventWithHeaderTranslatorTest.php index a92c9e6f..e7f23fab 100644 --- a/tests/Unit/Message/Translator/OnlyArchivedEventTranslatorTest.php +++ b/tests/Unit/Message/Translator/IncludeEventWithHeaderTranslatorTest.php @@ -5,26 +5,26 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Message\Translator; use Patchlevel\EventSourcing\Message\Message; -use Patchlevel\EventSourcing\Message\Translator\OnlyArchivedEventTranslator; +use Patchlevel\EventSourcing\Message\Translator\IncludeEventWithHeaderTranslator; use Patchlevel\EventSourcing\Store\ArchivedHeader; use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; use PHPUnit\Framework\TestCase; -/** @covers \Patchlevel\EventSourcing\Message\Translator\OnlyArchivedEventTranslator */ -final class OnlyArchivedEventTranslatorTest extends TestCase +/** @covers \Patchlevel\EventSourcing\Message\Translator\IncludeEventWithHeaderTranslator */ +final class IncludeEventWithHeaderTranslatorTest extends TestCase { public function testExcludedEvent(): void { - $translator = new OnlyArchivedEventTranslator(); + $translator = new IncludeEventWithHeaderTranslator(ArchivedHeader::class); $message = Message::create( new ProfileCreated( ProfileId::fromString('1'), Email::fromString('hallo@patchlevel.de'), ), - )->withHeader(new ArchivedHeader(false)); + ); $result = $translator($message); @@ -33,33 +33,17 @@ public function testExcludedEvent(): void public function testIncludeEvent(): void { - $translator = new OnlyArchivedEventTranslator(); + $translator = new IncludeEventWithHeaderTranslator(ArchivedHeader::class); $message = Message::create( new ProfileCreated( ProfileId::fromString('1'), Email::fromString('hallo@patchlevel.de'), ), - )->withHeader(new ArchivedHeader(true)); + )->withHeader(new ArchivedHeader()); $result = $translator($message); self::assertSame([$message], $result); } - - public function testHeaderNotSet(): void - { - $translator = new OnlyArchivedEventTranslator(); - - $message = Message::create( - new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('hallo@patchlevel.de'), - ), - ); - - $result = $translator($message); - - self::assertSame([], $result); - } } diff --git a/tests/Unit/Repository/DefaultRepositoryTest.php b/tests/Unit/Repository/DefaultRepositoryTest.php index f45ebde4..a26e3b2b 100644 --- a/tests/Unit/Repository/DefaultRepositoryTest.php +++ b/tests/Unit/Repository/DefaultRepositoryTest.php @@ -240,7 +240,7 @@ public function testDecorator(): void return false; } - if ($message->header(ArchivedHeader::class)->archived !== false) { + if (!$message->hasHeader(ArchivedHeader::class)) { return false; } @@ -256,7 +256,7 @@ public function testDecorator(): void $decorator = new class implements MessageDecorator { public function __invoke(Message $message): Message { - return $message->withHeader(new ArchivedHeader(false)); + return $message->withHeader(new ArchivedHeader()); } }; diff --git a/tests/Unit/Repository/MessageDecorator/SplitStreamDecoratorTest.php b/tests/Unit/Repository/MessageDecorator/SplitStreamDecoratorTest.php index 2a319e85..1f24a923 100644 --- a/tests/Unit/Repository/MessageDecorator/SplitStreamDecoratorTest.php +++ b/tests/Unit/Repository/MessageDecorator/SplitStreamDecoratorTest.php @@ -7,7 +7,7 @@ use Patchlevel\EventSourcing\Message\Message; use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory; use Patchlevel\EventSourcing\Repository\MessageDecorator\SplitStreamDecorator; -use Patchlevel\EventSourcing\Store\NewStreamStartHeader; +use Patchlevel\EventSourcing\Store\StreamStartHeader; use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; @@ -32,7 +32,7 @@ public function testWithoutSplittingStream(): void $decorator = new SplitStreamDecorator(new AttributeEventMetadataFactory()); $decoratedMessage = $decorator($message); - self::assertFalse($decoratedMessage->header(NewStreamStartHeader::class)->newStreamStart); + self::assertFalse($decoratedMessage->hasHeader(StreamStartHeader::class)); } public function testSplittingStream(): void @@ -47,6 +47,6 @@ public function testSplittingStream(): void $decorator = new SplitStreamDecorator(new AttributeEventMetadataFactory()); $decoratedMessage = $decorator($message); - self::assertTrue($decoratedMessage->header(NewStreamStartHeader::class)->newStreamStart); + self::assertTrue($decoratedMessage->hasHeader(StreamStartHeader::class)); } } diff --git a/tests/Unit/Store/ArchivedHeaderTest.php b/tests/Unit/Store/ArchivedHeaderTest.php deleted file mode 100644 index 43dc4a82..00000000 --- a/tests/Unit/Store/ArchivedHeaderTest.php +++ /dev/null @@ -1,19 +0,0 @@ -archived); - } -} diff --git a/tests/Unit/Store/DoctrineDbalStoreTest.php b/tests/Unit/Store/DoctrineDbalStoreTest.php index 19848e68..f6faa8e6 100644 --- a/tests/Unit/Store/DoctrineDbalStoreTest.php +++ b/tests/Unit/Store/DoctrineDbalStoreTest.php @@ -281,7 +281,6 @@ public function testLoadWithOneEvent(): void $abstractPlatform->createSelectSQLBuilder()->shouldBeCalledOnce()->willReturn(new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')); $abstractPlatform->getDateTimeTzFormatString()->shouldBeCalledOnce()->willReturn('Y-m-d H:i:s'); - $abstractPlatform->convertFromBoolean('0')->shouldBeCalledTimes(2)->willReturn(false); $connection->getDatabasePlatform()->willReturn($abstractPlatform->reveal()); @@ -379,7 +378,6 @@ public function testLoadWithTwoEvents(): void $abstractPlatform = $this->prophesize(AbstractPlatform::class); $abstractPlatform->createSelectSQLBuilder()->shouldBeCalledOnce()->willReturn(new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')); $abstractPlatform->getDateTimeTzFormatString()->shouldBeCalledTimes(2)->willReturn('Y-m-d H:i:s'); - $abstractPlatform->convertFromBoolean('0')->shouldBeCalledTimes(4)->willReturn(false); $connection->getDatabasePlatform()->willReturn($abstractPlatform->reveal()); diff --git a/tests/Unit/Store/DoctrineDbalStreamTest.php b/tests/Unit/Store/DoctrineDbalStreamTest.php index 657d63e1..c642d788 100644 --- a/tests/Unit/Store/DoctrineDbalStreamTest.php +++ b/tests/Unit/Store/DoctrineDbalStreamTest.php @@ -13,9 +13,7 @@ use Patchlevel\EventSourcing\Message\Serializer\HeadersSerializer; use Patchlevel\EventSourcing\Serializer\EventSerializer; use Patchlevel\EventSourcing\Serializer\SerializedEvent; -use Patchlevel\EventSourcing\Store\ArchivedHeader; use Patchlevel\EventSourcing\Store\DoctrineDbalStoreStream; -use Patchlevel\EventSourcing\Store\NewStreamStartHeader; use Patchlevel\EventSourcing\Store\StreamClosed; use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; @@ -78,9 +76,7 @@ public function testOneMessage(): void Email::fromString('info@patchlevel.de'), ); $message = Message::create($event) - ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))) - ->withHeader(new ArchivedHeader(false)) - ->withHeader(new NewStreamStartHeader(false)); + ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))); $eventSerializer = $this->prophesize(EventSerializer::class); $eventSerializer->deserialize(new SerializedEvent('profile_created', '{}')) @@ -92,7 +88,6 @@ public function testOneMessage(): void $platform = $this->prophesize(AbstractPlatform::class); $platform->getDateTimeTzFormatString()->shouldBeCalledOnce()->willReturn('Y-m-d H:i:s'); - $platform->convertFromBoolean('0')->shouldBeCalledTimes(2)->willReturn(false); $result = $this->prophesize(Result::class); $result->iterateAssociative()->shouldBeCalledOnce()->willReturn(new ArrayIterator($messages)); @@ -165,17 +160,11 @@ public function testMultipleMessages(): void $messages = [ Message::create($event) - ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))) - ->withHeader(new ArchivedHeader(false)) - ->withHeader(new NewStreamStartHeader(false)), + ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))), Message::create($event) - ->withHeader(new AggregateHeader('profile', '2', 1, new DateTimeImmutable('2022-10-10 10:10:10'))) - ->withHeader(new ArchivedHeader(false)) - ->withHeader(new NewStreamStartHeader(false)), + ->withHeader(new AggregateHeader('profile', '2', 1, new DateTimeImmutable('2022-10-10 10:10:10'))), Message::create($event) - ->withHeader(new AggregateHeader('profile', '3', 1, new DateTimeImmutable('2022-10-10 10:10:10'))) - ->withHeader(new ArchivedHeader(false)) - ->withHeader(new NewStreamStartHeader(false)), + ->withHeader(new AggregateHeader('profile', '3', 1, new DateTimeImmutable('2022-10-10 10:10:10'))), ]; $eventSerializer = $this->prophesize(EventSerializer::class); @@ -194,7 +183,6 @@ public function testMultipleMessages(): void $platform = $this->prophesize(AbstractPlatform::class); $platform->getDateTimeTzFormatString()->shouldBeCalledTimes(3)->willReturn('Y-m-d H:i:s'); - $platform->convertFromBoolean('0')->shouldBeCalledTimes(6)->willReturn(false); $result = $this->prophesize(Result::class); $result->iterateAssociative()->shouldBeCalledOnce()->willReturn(new ArrayIterator($messagesArray)); @@ -255,9 +243,7 @@ public function testWithNoList(): void Email::fromString('info@patchlevel.de'), ); $message = Message::create($event) - ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))) - ->withHeader(new ArchivedHeader(false)) - ->withHeader(new NewStreamStartHeader(false)); + ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))); $eventSerializer = $this->prophesize(EventSerializer::class); $eventSerializer->deserialize(new SerializedEvent('profile_created', '{}')) @@ -269,7 +255,6 @@ public function testWithNoList(): void $platform = $this->prophesize(AbstractPlatform::class); $platform->getDateTimeTzFormatString()->shouldBeCalledOnce()->willReturn('Y-m-d H:i:s'); - $platform->convertFromBoolean('0')->shouldBeCalledTimes(2)->willReturn(false); $result = $this->prophesize(Result::class); $result->iterateAssociative()->shouldBeCalledOnce()->willReturn(new ArrayIterator($messages)); @@ -316,9 +301,7 @@ public function testClose(): void Email::fromString('info@patchlevel.de'), ); $message = Message::create($event) - ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))) - ->withHeader(new ArchivedHeader(false)) - ->withHeader(new NewStreamStartHeader(false)); + ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))); $eventSerializer = $this->prophesize(EventSerializer::class); $eventSerializer->deserialize(new SerializedEvent('profile_created', '{}')) @@ -330,7 +313,6 @@ public function testClose(): void $platform = $this->prophesize(AbstractPlatform::class); $platform->getDateTimeTzFormatString()->shouldBeCalledOnce()->willReturn('Y-m-d H:i:s'); - $platform->convertFromBoolean('0')->shouldBeCalledTimes(2)->willReturn(false); $result = $this->prophesize(Result::class); $result->iterateAssociative()->shouldBeCalledOnce()->willReturn(new ArrayIterator($messages)); @@ -396,10 +378,6 @@ public function testPosition(): void ProfileId::fromString('foo'), Email::fromString('info@patchlevel.de'), ); - $message = Message::create($event) - ->withHeader(new AggregateHeader('profile', '1', 1, new DateTimeImmutable('2022-10-10 10:10:10'))) - ->withHeader(new ArchivedHeader(false)) - ->withHeader(new NewStreamStartHeader(false)); $eventSerializer = $this->prophesize(EventSerializer::class); $eventSerializer->deserialize(new SerializedEvent('profile_created', '{}')) @@ -411,7 +389,6 @@ public function testPosition(): void $platform = $this->prophesize(AbstractPlatform::class); $platform->getDateTimeTzFormatString()->shouldBeCalledOnce()->willReturn('Y-m-d H:i:s'); - $platform->convertFromBoolean('0')->shouldBeCalledTimes(2)->willReturn(false); $result = $this->prophesize(Result::class); $result->iterateAssociative()->shouldBeCalledOnce()->willReturn(new ArrayIterator($messages)); diff --git a/tests/Unit/Store/DoctrineHelperTest.php b/tests/Unit/Store/DoctrineHelperTest.php index 5ddb2f30..fecabc4d 100644 --- a/tests/Unit/Store/DoctrineHelperTest.php +++ b/tests/Unit/Store/DoctrineHelperTest.php @@ -34,40 +34,6 @@ public function testNormalizePlayheadInvalid(): void DoctrineHelper::normalizePlayhead('asd', $platform->reveal()); } - public function testNormalizeArchived(): void - { - $platform = $this->prophesize(AbstractPlatform::class); - $platform->convertFromBoolean('1')->shouldBeCalledOnce()->willReturn(true); - - $result = DoctrineHelper::normalizeArchived('1', $platform->reveal()); - self::assertTrue($result); - } - - public function testNormalizeArchivedInvalid(): void - { - $platform = $this->prophesize(AbstractPlatform::class); - - $this->expectException(InvalidType::class); - DoctrineHelper::normalizeArchived('asd', $platform->reveal()); - } - - public function testNormalizeNewStreamStart(): void - { - $platform = $this->prophesize(AbstractPlatform::class); - $platform->convertFromBoolean('1')->shouldBeCalledOnce()->willReturn(true); - - $result = DoctrineHelper::normalizeNewStreamStart('1', $platform->reveal()); - self::assertTrue($result); - } - - public function testNormalizeNewStreamStartInvalid(): void - { - $platform = $this->prophesize(AbstractPlatform::class); - - $this->expectException(InvalidType::class); - DoctrineHelper::normalizeNewStreamStart('asd', $platform->reveal()); - } - public function testNormalizeRecordedOn(): void { $platform = $this->prophesize(AbstractPlatform::class); diff --git a/tests/Unit/Store/NewStreamStartHeaderTest.php b/tests/Unit/Store/NewStreamStartHeaderTest.php deleted file mode 100644 index cc6bcf56..00000000 --- a/tests/Unit/Store/NewStreamStartHeaderTest.php +++ /dev/null @@ -1,19 +0,0 @@ -newStreamStart); - } -}