From af1aacc281354f594811ef9752135354fec66fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Guti=C3=A9rrez?= Date: Sat, 8 Jun 2019 02:14:04 +0200 Subject: [PATCH] support events metadata --- .php_cs | 4 +++- .travis.yml | 4 ++-- composer.json | 2 +- src/ReceivedEvent.php | 24 +++++++++++++++++++ src/Serializer/JsonEventSerializer.php | 2 ++ tests/Async/ReceivedEventTest.php | 18 ++++++++++++++ .../Serializer/JsonEventSerializerTest.php | 3 ++- .../Serializer/NativeEventSerializerTest.php | 1 + 8 files changed, 53 insertions(+), 5 deletions(-) diff --git a/.php_cs b/.php_cs index 1e890cb..17e3614 100644 --- a/.php_cs +++ b/.php_cs @@ -93,7 +93,9 @@ return Config::create() 'no_whitespace_before_comma_in_array' => true, 'no_whitespace_in_blank_line' => true, 'normalize_index_brace' => true, - 'ordered_imports' => true, + 'ordered_imports' => [ + 'imports_order' => ['const', 'class', 'function'], + ], 'php_unit_construct' => true, 'php_unit_dedicate_assert' => true, 'phpdoc_add_missing_param_annotation' => true, diff --git a/.travis.yml b/.travis.yml index 10268ce..edd3ab5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ env: - COMPOSER_FLAGS="--prefer-stable --prefer-dist" php: - - 7.2 + - 7.3 - nightly matrix: @@ -22,7 +22,7 @@ matrix: - php: 7.1 env: - COMPOSER_FLAGS="--prefer-lowest --prefer-stable --prefer-dist" - - php: 7.1 + - php: 7.2 env: - TEST_VERSION=true - COMPOSER_FLAGS="--prefer-stable --prefer-dist" diff --git a/composer.json b/composer.json index fac5b48..7bdcd77 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require": { "php": "^7.1", "ext-json": "*", - "phpgears/event": "~0.1" + "phpgears/event": "~0.1.3" }, "require-dev": { "brainmaestro/composer-git-hooks": "^2.1", diff --git a/src/ReceivedEvent.php b/src/ReceivedEvent.php index 931be1d..26d60fd 100644 --- a/src/ReceivedEvent.php +++ b/src/ReceivedEvent.php @@ -77,6 +77,30 @@ public function getPayload(): array throw new ReceivedEventException(\sprintf('Method %s should not be called ', __METHOD__)); } + /** + * {@inheritdoc} + * + * @throws ReceivedEventException + * + * @return array + */ + public function getMetadata(): array + { + throw new ReceivedEventException(\sprintf('Method %s should not be called ', __METHOD__)); + } + + /** + * {@inheritdoc} + * + * @throws ReceivedEventException + * + * @return array + */ + public function withMetadata(array $metadata) + { + throw new ReceivedEventException(\sprintf('Method %s should not be called ', __METHOD__)); + } + /** * {@inheritdoc} * diff --git a/src/Serializer/JsonEventSerializer.php b/src/Serializer/JsonEventSerializer.php index 4d904fe..69b1d6d 100644 --- a/src/Serializer/JsonEventSerializer.php +++ b/src/Serializer/JsonEventSerializer.php @@ -80,6 +80,7 @@ final public function serialize(Event $event): string protected function getSerializationAttributes(Event $event): array { return [ + 'metadata' => $event->getMetadata(), 'createdAt' => $event->getCreatedAt()->format(self::DATE_RFC3339_EXTENDED), ]; } @@ -177,6 +178,7 @@ private function getDeserializationDefinition(string $serialized): array protected function getDeserializationAttributes(array $attributes): array { return [ + 'metadata' => $attributes['metadata'], 'createdAt' => \DateTimeImmutable::createFromFormat(self::DATE_RFC3339_EXTENDED, $attributes['createdAt']), ]; } diff --git a/tests/Async/ReceivedEventTest.php b/tests/Async/ReceivedEventTest.php index e1003b6..e08ce08 100644 --- a/tests/Async/ReceivedEventTest.php +++ b/tests/Async/ReceivedEventTest.php @@ -55,6 +55,24 @@ public function testGetPayloadException(): void (new ReceivedEvent(EventStub::instance([])))->getPayload(); } + /** + * @expectedException \Gears\Event\Async\Exception\ReceivedEventException + * @expectedExceptionMessage Method Gears\Event\Async\ReceivedEvent::getMetadata should not be called + */ + public function testGetMetadataException(): void + { + (new ReceivedEvent(EventStub::instance([])))->getMetadata(); + } + + /** + * @expectedException \Gears\Event\Async\Exception\ReceivedEventException + * @expectedExceptionMessage Method Gears\Event\Async\ReceivedEvent::withMetadata should not be called + */ + public function testGetMetadataMutateException(): void + { + (new ReceivedEvent(EventStub::instance([])))->withMetadata([]); + } + /** * @expectedException \Gears\Event\Async\Exception\ReceivedEventException * @expectedExceptionMessage Method Gears\Event\Async\ReceivedEvent::getCreatedAt should not be called diff --git a/tests/Async/Serializer/JsonEventSerializerTest.php b/tests/Async/Serializer/JsonEventSerializerTest.php index bbddf1c..656e64e 100644 --- a/tests/Async/Serializer/JsonEventSerializerTest.php +++ b/tests/Async/Serializer/JsonEventSerializerTest.php @@ -34,11 +34,12 @@ public function testSerialize(): void public function testDeserialize(): void { $event = EventStub::instance(['identifier' => '1234']); + $event = $event->withMetadata(['meta' => 'data']); $eventDate = $event->getCreatedAt()->format('Y-m-d\TH:i:s.uP'); $serialized = '{"class":"Gears\\\\Event\\\\Async\\\\Tests\\\\Stub\\\\EventStub",' . '"payload":{"identifier":"1234"},' - . '"attributes":{"createdAt":"' . $eventDate . '"}}'; + . '"attributes":{"metadata":{"meta":"data"},"createdAt":"' . $eventDate . '"}}'; $deserialized = (new JsonEventSerializer())->fromSerialized($serialized); diff --git a/tests/Async/Serializer/NativeEventSerializerTest.php b/tests/Async/Serializer/NativeEventSerializerTest.php index 3a6b117..8694eaa 100644 --- a/tests/Async/Serializer/NativeEventSerializerTest.php +++ b/tests/Async/Serializer/NativeEventSerializerTest.php @@ -34,6 +34,7 @@ public function testSerialize(): void public function testDeserialize(): void { $event = EventStub::instance(['identifier' => '1234']); + $event = $event->withMetadata(['meta' => 'data']); $deserialized = (new NativeEventSerializer())->fromSerialized(\serialize($event));