Skip to content

Commit

Permalink
Move message class name to metadata and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorprogger committed Sep 18, 2024
1 parent 6c78406 commit 9eb4dd9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/Message/JsonMessageSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ public function serialize(MessageInterface $message): string
'name' => $message->getHandlerName(),
'data' => $message->getData(),
'meta' => $message->getMetadata(),
'class' => $message instanceof EnvelopeInterface ? $message->getMessage()::class : $message::class,
];
if (!isset($payload['meta']['message-class'])) {
$payload['meta']['message-class'] = $message instanceof EnvelopeInterface
? $message->getMessage()::class
: $message::class;
}

return json_encode($payload, JSON_THROW_ON_ERROR);
}
Expand All @@ -37,12 +41,12 @@ public function unserialize(string $value): MessageInterface

$name = $payload['name'] ?? null;
if (!isset($name) || !is_string($name)) {
throw new InvalidArgumentException('Handler name must be string. Got ' . get_debug_type($name) . '.');
throw new InvalidArgumentException('Handler name must be a string. Got ' . get_debug_type($name) . '.');
}

$meta = $payload['meta'] ?? [];
if (!is_array($meta)) {
throw new InvalidArgumentException('Metadata must be array. Got ' . get_debug_type($meta) . '.');
throw new InvalidArgumentException('Metadata must be an array. Got ' . get_debug_type($meta) . '.');
}

$envelopes = [];
Expand All @@ -51,7 +55,7 @@ public function unserialize(string $value): MessageInterface
}
$meta[EnvelopeInterface::ENVELOPE_STACK_KEY] = [];

$class = $payload['class'] ?? Message::class;
$class = $payload['meta']['message-class'] ?? Message::class;
if (!is_subclass_of($class, MessageInterface::class)) {
$class = Message::class;
}
Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/Message/JsonMessageSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testMetadataFormat(mixed $meta): void
$payload = ['name' => 'handler', 'data' => 'test', 'meta' => $meta];
$serializer = $this->createSerializer();

$this->expectExceptionMessage(sprintf('Metadata must be array. Got %s.', get_debug_type($meta)));
$this->expectExceptionMessage(sprintf('Metadata must be an array. Got %s.', get_debug_type($meta)));
$this->expectException(InvalidArgumentException::class);
$serializer->unserialize(json_encode($payload));
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public function testSerialize(): void
$json = $serializer->serialize($message);

$this->assertEquals(
'{"name":"handler","data":"test","meta":[],"class":"Yiisoft\\\\Queue\\\\Message\\\\Message"}',
'{"name":"handler","data":"test","meta":{"message-class":"Yiisoft\\\\Queue\\\\Message\\\\Message"}}',
$json,
);
}
Expand All @@ -131,7 +131,7 @@ public function testSerializeEnvelopeStack(): void

$this->assertEquals(
sprintf(
'{"name":"handler","data":"test","meta":{"envelopes":["%s"],"%s":"test-id"},"class":"%s"}',
'{"name":"handler","data":"test","meta":{"envelopes":["%s"],"%s":"test-id","message-class":"%s"}}',
str_replace('\\', '\\\\', IdEnvelope::class),
IdEnvelope::MESSAGE_ID_KEY,
str_replace('\\', '\\\\', Message::class),
Expand All @@ -148,11 +148,13 @@ public function testSerializeEnvelopeStack(): void
IdEnvelope::class,
],
IdEnvelope::MESSAGE_ID_KEY => 'test-id',
"message-class" => Message::class,
], $message->getMetadata());

$this->assertEquals([
EnvelopeInterface::ENVELOPE_STACK_KEY => [],
IdEnvelope::MESSAGE_ID_KEY => 'test-id',
"message-class" => Message::class,
], $message->getMessage()->getMetadata());
}

Expand Down

0 comments on commit 9eb4dd9

Please sign in to comment.