Skip to content

Commit

Permalink
support events metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Jun 8, 2019
1 parent cd32be9 commit af1aacc
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ env:
- COMPOSER_FLAGS="--prefer-stable --prefer-dist"

php:
- 7.2
- 7.3
- nightly

matrix:
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions src/ReceivedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ public function getPayload(): array
throw new ReceivedEventException(\sprintf('Method %s should not be called ', __METHOD__));
}

/**
* {@inheritdoc}
*
* @throws ReceivedEventException
*
* @return array<string, mixed>
*/
public function getMetadata(): array
{
throw new ReceivedEventException(\sprintf('Method %s should not be called ', __METHOD__));
}

/**
* {@inheritdoc}
*
* @throws ReceivedEventException
*
* @return array<string, mixed>
*/
public function withMetadata(array $metadata)
{
throw new ReceivedEventException(\sprintf('Method %s should not be called ', __METHOD__));
}

/**
* {@inheritdoc}
*
Expand Down
2 changes: 2 additions & 0 deletions src/Serializer/JsonEventSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
}
Expand Down Expand Up @@ -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']),
];
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Async/ReceivedEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/Async/Serializer/JsonEventSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions tests/Async/Serializer/NativeEventSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit af1aacc

Please sign in to comment.