From 9eb4dd92881fd278f40056ff6ecb9882111d06f2 Mon Sep 17 00:00:00 2001 From: viktorprogger Date: Wed, 18 Sep 2024 19:11:36 +0500 Subject: [PATCH] Move message class name to metadata and fix tests --- src/Message/JsonMessageSerializer.php | 12 ++++++++---- tests/Unit/Message/JsonMessageSerializerTest.php | 8 +++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Message/JsonMessageSerializer.php b/src/Message/JsonMessageSerializer.php index 8f30f1fb..277b182a 100644 --- a/src/Message/JsonMessageSerializer.php +++ b/src/Message/JsonMessageSerializer.php @@ -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); } @@ -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 = []; @@ -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; } diff --git a/tests/Unit/Message/JsonMessageSerializerTest.php b/tests/Unit/Message/JsonMessageSerializerTest.php index 27efc892..7c583ba8 100644 --- a/tests/Unit/Message/JsonMessageSerializerTest.php +++ b/tests/Unit/Message/JsonMessageSerializerTest.php @@ -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)); } @@ -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, ); } @@ -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), @@ -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()); }