From cc15b35d2fdca403bdb137a6098db3c0d0dbe53e Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 13 Jan 2024 23:11:01 +0700 Subject: [PATCH 01/14] Add envelope stack --- src/Message/EnvelopeInterface.php | 2 ++ src/Message/EnvelopeTrait.php | 13 ++++++++- src/Message/IdEnvelope.php | 6 ++-- tests/Unit/EnvelopeTest.php | 47 +++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/EnvelopeTest.php diff --git a/src/Message/EnvelopeInterface.php b/src/Message/EnvelopeInterface.php index bf336112..9f7400d1 100644 --- a/src/Message/EnvelopeInterface.php +++ b/src/Message/EnvelopeInterface.php @@ -9,6 +9,8 @@ */ interface EnvelopeInterface extends MessageInterface { + public const ENVELOPE_STACK_KEY = 'envelopes'; + public function getMessage(): MessageInterface; public function withMessage(MessageInterface $message): self; diff --git a/src/Message/EnvelopeTrait.php b/src/Message/EnvelopeTrait.php index 13e593ab..99f761ad 100644 --- a/src/Message/EnvelopeTrait.php +++ b/src/Message/EnvelopeTrait.php @@ -33,6 +33,17 @@ public function getData(): mixed public function getMetadata(): array { - return $this->message->getMetadata(); + return array_merge($this->message->getMetadata(), [ + self::ENVELOPE_STACK_KEY => array_merge( + $this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [], + [self::class], + ), + ...$this->getEnvelopeMetadata(), + ]); + } + + public function getEnvelopeMetadata(): array + { + return []; } } diff --git a/src/Message/IdEnvelope.php b/src/Message/IdEnvelope.php index a1ffccad..32d5f3f8 100644 --- a/src/Message/IdEnvelope.php +++ b/src/Message/IdEnvelope.php @@ -29,10 +29,8 @@ public function getId(): string|int|null return $this->id ?? $this->message->getMetadata()[self::MESSAGE_ID_KEY] ?? null; } - public function getMetadata(): array + public function getEnvelopeMetadata(): array { - return array_merge($this->message->getMetadata(), [ - self::MESSAGE_ID_KEY => $this->getId(), - ]); + return [self::MESSAGE_ID_KEY => $this->getId()]; } } diff --git a/tests/Unit/EnvelopeTest.php b/tests/Unit/EnvelopeTest.php new file mode 100644 index 00000000..af412125 --- /dev/null +++ b/tests/Unit/EnvelopeTest.php @@ -0,0 +1,47 @@ +assertEquals('test', $message->getMessage()->getData()); + + $stack = $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY]; + $this->assertIsArray($stack); + + $this->assertEquals([ + IdEnvelope::class, + ], $stack); + } + + public function testEnvelopeDuplicates(): void + { + $message = new Message('handler', 'test'); + $message = new IdEnvelope($message, 'test-id'); + $message = new IdEnvelope($message, 'test-id'); + $message = new IdEnvelope($message, 'test-id'); + + $this->assertEquals('test', $message->getMessage()->getData()); + + $stack = $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY]; + $this->assertIsArray($stack); + + $this->assertEquals([ + IdEnvelope::class, + IdEnvelope::class, + IdEnvelope::class, + ], $stack); + } +} From 66fc7b6745d97ca736baa1fc2feeb1e2c2de51d9 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 13 Jan 2024 23:56:11 +0700 Subject: [PATCH 02/14] Move serializer from ampq driver and refactor --- src/Exception/NoKeyInPayloadException.php | 56 +++++++ src/Message/EnvelopeInterface.php | 2 + src/Message/EnvelopeTrait.php | 4 + src/Message/IdEnvelope.php | 2 +- src/Message/Message.php | 8 + src/Message/MessageSerializer.php | 60 ++++++++ src/Message/MessageSerializerInterface.php | 12 ++ .../FailureHandling/FailureEnvelope.php | 2 +- tests/Unit/Message/MessageSerializerTest.php | 143 ++++++++++++++++++ 9 files changed, 287 insertions(+), 2 deletions(-) create mode 100644 src/Exception/NoKeyInPayloadException.php create mode 100644 src/Message/MessageSerializer.php create mode 100644 src/Message/MessageSerializerInterface.php create mode 100644 tests/Unit/Message/MessageSerializerTest.php diff --git a/src/Exception/NoKeyInPayloadException.php b/src/Exception/NoKeyInPayloadException.php new file mode 100644 index 00000000..b8dda3bb --- /dev/null +++ b/src/Exception/NoKeyInPayloadException.php @@ -0,0 +1,56 @@ +expectedKey); + } + + /** + * @return string + * + * @infection-ignore-all + */ + public function getSolution(): ?string + { + return sprintf( + "We have successfully unserialized a message, but there was no expected key \"%s\". + There are the following keys in the message: %s. + You might want to change message's structure, or make your own implementation of %s, + which won't rely on this key in the message.", + $this->expectedKey, + implode('", "', array_keys($this->payload)), + MessageSerializerInterface::class + ); + } +} diff --git a/src/Message/EnvelopeInterface.php b/src/Message/EnvelopeInterface.php index 9f7400d1..b0f8d89f 100644 --- a/src/Message/EnvelopeInterface.php +++ b/src/Message/EnvelopeInterface.php @@ -11,6 +11,8 @@ interface EnvelopeInterface extends MessageInterface { public const ENVELOPE_STACK_KEY = 'envelopes'; + public static function fromMessage(MessageInterface $message): self; + public function getMessage(): MessageInterface; public function withMessage(MessageInterface $message): self; diff --git a/src/Message/EnvelopeTrait.php b/src/Message/EnvelopeTrait.php index 99f761ad..6e66e492 100644 --- a/src/Message/EnvelopeTrait.php +++ b/src/Message/EnvelopeTrait.php @@ -31,6 +31,10 @@ public function getData(): mixed return $this->message->getData(); } + public static function fromMessage(MessageInterface $message): self + { + return new static($message); + } public function getMetadata(): array { return array_merge($this->message->getMetadata(), [ diff --git a/src/Message/IdEnvelope.php b/src/Message/IdEnvelope.php index 32d5f3f8..dec2d679 100644 --- a/src/Message/IdEnvelope.php +++ b/src/Message/IdEnvelope.php @@ -29,7 +29,7 @@ public function getId(): string|int|null return $this->id ?? $this->message->getMetadata()[self::MESSAGE_ID_KEY] ?? null; } - public function getEnvelopeMetadata(): array + private function getEnvelopeMetadata(): array { return [self::MESSAGE_ID_KEY => $this->getId()]; } diff --git a/src/Message/Message.php b/src/Message/Message.php index 07278d1f..a414ffb0 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -32,4 +32,12 @@ public function getMetadata(): array { return $this->metadata; } + + public function withMetadata(array $metadata): self + { + $instance = clone $this; + $instance->metadata = $metadata; + + return $instance; + } } diff --git a/src/Message/MessageSerializer.php b/src/Message/MessageSerializer.php new file mode 100644 index 00000000..c8ba276c --- /dev/null +++ b/src/Message/MessageSerializer.php @@ -0,0 +1,60 @@ + $message->getHandlerName(), + 'data' => $message->getData(), + 'meta' => $message->getMetadata(), + ]; + + return json_encode($payload, JSON_THROW_ON_ERROR); + } + + /** + * @throws JsonException + * @throws NoKeyInPayloadException + * @throws InvalidArgumentException + */ + public function unserialize(string $value): MessageInterface + { + $payload = json_decode($value, true, 512, JSON_THROW_ON_ERROR); + if (!is_array($payload)) { + throw new InvalidArgumentException('Payload must be array. Got ' . get_debug_type($payload) . '.'); + } + + $meta = $payload['meta'] ?? []; + if (!is_array($meta)) { + throw new InvalidArgumentException('Metadata must be array. Got ' . get_debug_type($meta) . '.'); + } + + $message = new Message('$name', $payload['data'] ?? null, $meta); + + if (isset($meta[EnvelopeInterface::ENVELOPE_STACK_KEY]) && is_array($meta[EnvelopeInterface::ENVELOPE_STACK_KEY])) { + $message = $message->withMetadata( + array_merge($message->getMetadata(), [EnvelopeInterface::ENVELOPE_STACK_KEY => []]), + ); + foreach ($meta[EnvelopeInterface::ENVELOPE_STACK_KEY] as $envelope) { + if (is_string($envelope) && class_exists($envelope) && is_subclass_of($envelope, EnvelopeInterface::class)) { + $message = $envelope::fromMessage($message); + } + } + } + + + return $message; + } +} diff --git a/src/Message/MessageSerializerInterface.php b/src/Message/MessageSerializerInterface.php new file mode 100644 index 00000000..b034590c --- /dev/null +++ b/src/Message/MessageSerializerInterface.php @@ -0,0 +1,12 @@ +createSerializer(); + + $this->expectExceptionMessage(sprintf('Payload must be array. Got %s.', get_debug_type($payload))); + $this->expectException(InvalidArgumentException::class); + $serializer->unserialize(json_encode($payload)); + } + + public static function dataUnsupportedPayloadFormat(): iterable + { + yield 'string' => ['']; + yield 'number' => [1]; + yield 'boolean' => [true]; + yield 'null' => [null]; + } + + /** + * @dataProvider dataUnsupportedMetadataFormat + */ + public function testMetadataFormat(mixed $meta): void + { + $payload = ['data' => 'test', 'meta' => $meta]; + $serializer = $this->createSerializer(); + + $this->expectExceptionMessage(sprintf('Metadata must be array. Got %s.', get_debug_type($meta))); + $this->expectException(InvalidArgumentException::class); + $serializer->unserialize(json_encode($payload)); + } + + public static function dataUnsupportedMetadataFormat(): iterable + { + yield 'string' => ['']; + yield 'number' => [1]; + yield 'boolean' => [true]; + } + + public function testUnserializeFromData(): void + { + $payload = ['data' => 'test']; + $serializer = $this->createSerializer(); + + $message = $serializer->unserialize(json_encode($payload)); + + $this->assertInstanceOf(MessageInterface::class, $message); + $this->assertEquals($payload['data'], $message->getData()); + $this->assertEquals([], $message->getMetadata()); + } + + public function testUnserializeWithMetadata(): void + { + $payload = ['data' => 'test', 'meta' => ['int' => 1, 'str' => 'string', 'bool' => true]]; + $serializer = $this->createSerializer(); + + $message = $serializer->unserialize(json_encode($payload)); + + $this->assertInstanceOf(MessageInterface::class, $message); + $this->assertEquals($payload['data'], $message->getData()); + $this->assertEquals(['int' => 1, 'str' => 'string', 'bool' => true], $message->getMetadata()); + } + + public function testUnserializeEnvelopeStack(): void + { + $payload = [ + 'data' => 'test', + 'meta' => [ + EnvelopeInterface::ENVELOPE_STACK_KEY => [ + IdEnvelope::class, + ], + ], + ]; + $serializer = $this->createSerializer(); + + $message = $serializer->unserialize(json_encode($payload)); + + $this->assertInstanceOf(MessageInterface::class, $message); + $this->assertEquals($payload['data'], $message->getData()); + $this->assertEquals([IdEnvelope::class], $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY]); + + $this->assertInstanceOf(IdEnvelope::class, $message); + $this->assertInstanceOf(Message::class, $message->getMessage()); + } + + public function testSerialize(): void + { + $message = new Message('handler', 'test'); + + $serializer = $this->createSerializer(); + + $json = $serializer->serialize($message); + + $this->assertEquals( + '{"name":"handler","data":"test","meta":[]}', + $json, + ); + } + + public function testSerializeEnvelopeStack(): void + { + $message = new Message('handler', 'test'); + $message = new IdEnvelope($message, 'test-id'); + + $serializer = $this->createSerializer(); + + $json = $serializer->serialize($message); + + $this->assertEquals( + sprintf( + '{"name":"handler","data":"test","meta":{"envelopes":["%s"],"%s":"test-id"}}', + str_replace('\\', '\\\\', IdEnvelope::class), + IdEnvelope::MESSAGE_ID_KEY, + ), + $json, + ); + } + + private function createSerializer(): MessageSerializer + { + return new MessageSerializer(); + } +} From d70d5c1410424e4280ef99653b6e6842dd827021 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 13 Jan 2024 16:56:24 +0000 Subject: [PATCH 03/14] Apply fixes from StyleCI --- src/Message/EnvelopeTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Message/EnvelopeTrait.php b/src/Message/EnvelopeTrait.php index 6e66e492..b971bdff 100644 --- a/src/Message/EnvelopeTrait.php +++ b/src/Message/EnvelopeTrait.php @@ -35,6 +35,7 @@ public static function fromMessage(MessageInterface $message): self { return new static($message); } + public function getMetadata(): array { return array_merge($this->message->getMetadata(), [ From 86c0cf5ca07c066f06e4901079cf026ec122e7c3 Mon Sep 17 00:00:00 2001 From: xepozz Date: Sat, 13 Jan 2024 16:56:46 +0000 Subject: [PATCH 04/14] Apply Rector changes (CI) --- src/QueueInterface.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/QueueInterface.php b/src/QueueInterface.php index 2fa93fb3..d38c06e2 100644 --- a/src/QueueInterface.php +++ b/src/QueueInterface.php @@ -19,17 +19,13 @@ interface QueueInterface /** * Pushes a message into the queue. * - * @param MessageInterface $message * @param array|callable|MiddlewarePushInterface|string ...$middlewareDefinitions - * * @return MessageInterface */ public function push(MessageInterface $message, MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions): MessageInterface; /** * Execute all existing jobs and exit - * - * @param int $max */ public function run(int $max = 0): void; From f1b4f916be0308331e96c1e0d1043093db04046e Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 13 Jan 2024 23:59:45 +0700 Subject: [PATCH 05/14] Support php80 --- src/Message/EnvelopeTrait.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Message/EnvelopeTrait.php b/src/Message/EnvelopeTrait.php index b971bdff..7e58a97b 100644 --- a/src/Message/EnvelopeTrait.php +++ b/src/Message/EnvelopeTrait.php @@ -38,13 +38,16 @@ public static function fromMessage(MessageInterface $message): self public function getMetadata(): array { - return array_merge($this->message->getMetadata(), [ - self::ENVELOPE_STACK_KEY => array_merge( - $this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [], - [self::class], - ), - ...$this->getEnvelopeMetadata(), - ]); + return array_merge( + $this->message->getMetadata(), + [ + self::ENVELOPE_STACK_KEY => array_merge( + $this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [], + [self::class], + ), + ], + $this->getEnvelopeMetadata(), + ); } public function getEnvelopeMetadata(): array From 3a689a2124806ae15ca8671652cc4bc7f372c020 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 00:04:46 +0700 Subject: [PATCH 06/14] Test envelope decodes right --- tests/Unit/Message/MessageSerializerTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Unit/Message/MessageSerializerTest.php b/tests/Unit/Message/MessageSerializerTest.php index 93ea5b19..941008fc 100644 --- a/tests/Unit/Message/MessageSerializerTest.php +++ b/tests/Unit/Message/MessageSerializerTest.php @@ -100,6 +100,7 @@ public function testUnserializeEnvelopeStack(): void $this->assertEquals([IdEnvelope::class], $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY]); $this->assertInstanceOf(IdEnvelope::class, $message); + $this->assertNull($message->getId()); $this->assertInstanceOf(Message::class, $message->getMessage()); } @@ -134,6 +135,11 @@ public function testSerializeEnvelopeStack(): void ), $json, ); + + $message = $serializer->unserialize($json); + + $this->assertInstanceOf(IdEnvelope::class, $message); + $this->assertEquals('test-id', $message->getId()); } private function createSerializer(): MessageSerializer From 5dd848cb022f3ba4c4c9fbc35a7363bc725421d7 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 00:08:05 +0700 Subject: [PATCH 07/14] Remove redundant class --- src/Exception/NoKeyInPayloadException.php | 56 ----------------------- src/Message/MessageSerializer.php | 2 - 2 files changed, 58 deletions(-) delete mode 100644 src/Exception/NoKeyInPayloadException.php diff --git a/src/Exception/NoKeyInPayloadException.php b/src/Exception/NoKeyInPayloadException.php deleted file mode 100644 index b8dda3bb..00000000 --- a/src/Exception/NoKeyInPayloadException.php +++ /dev/null @@ -1,56 +0,0 @@ -expectedKey); - } - - /** - * @return string - * - * @infection-ignore-all - */ - public function getSolution(): ?string - { - return sprintf( - "We have successfully unserialized a message, but there was no expected key \"%s\". - There are the following keys in the message: %s. - You might want to change message's structure, or make your own implementation of %s, - which won't rely on this key in the message.", - $this->expectedKey, - implode('", "', array_keys($this->payload)), - MessageSerializerInterface::class - ); - } -} diff --git a/src/Message/MessageSerializer.php b/src/Message/MessageSerializer.php index c8ba276c..21bb481b 100644 --- a/src/Message/MessageSerializer.php +++ b/src/Message/MessageSerializer.php @@ -6,7 +6,6 @@ use InvalidArgumentException; use JsonException; -use Yiisoft\Queue\Exception\NoKeyInPayloadException; final class MessageSerializer implements MessageSerializerInterface { @@ -26,7 +25,6 @@ public function serialize(MessageInterface $message): string /** * @throws JsonException - * @throws NoKeyInPayloadException * @throws InvalidArgumentException */ public function unserialize(string $value): MessageInterface From d4f709ea2c369c24efb15a7ab56ba397357a3161 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 00:09:37 +0700 Subject: [PATCH 08/14] Rename class --- .../{MessageSerializer.php => JsonMessageSerializer.php} | 2 +- ...geSerializerTest.php => JsonMessageSerializerTest.php} | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename src/Message/{MessageSerializer.php => JsonMessageSerializer.php} (96%) rename tests/Unit/Message/{MessageSerializerTest.php => JsonMessageSerializerTest.php} (95%) diff --git a/src/Message/MessageSerializer.php b/src/Message/JsonMessageSerializer.php similarity index 96% rename from src/Message/MessageSerializer.php rename to src/Message/JsonMessageSerializer.php index 21bb481b..4d6df216 100644 --- a/src/Message/MessageSerializer.php +++ b/src/Message/JsonMessageSerializer.php @@ -7,7 +7,7 @@ use InvalidArgumentException; use JsonException; -final class MessageSerializer implements MessageSerializerInterface +final class JsonMessageSerializer implements MessageSerializerInterface { /** * @throws JsonException diff --git a/tests/Unit/Message/MessageSerializerTest.php b/tests/Unit/Message/JsonMessageSerializerTest.php similarity index 95% rename from tests/Unit/Message/MessageSerializerTest.php rename to tests/Unit/Message/JsonMessageSerializerTest.php index 941008fc..ad5ac205 100644 --- a/tests/Unit/Message/MessageSerializerTest.php +++ b/tests/Unit/Message/JsonMessageSerializerTest.php @@ -10,12 +10,12 @@ use Yiisoft\Queue\Message\IdEnvelope; use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\Message\MessageInterface; -use Yiisoft\Queue\Message\MessageSerializer; +use Yiisoft\Queue\Message\JsonMessageSerializer; /** * Testing message serialization options */ -final class MessageSerializerTest extends TestCase +final class JsonMessageSerializerTest extends TestCase { /** * @dataProvider dataUnsupportedPayloadFormat @@ -142,8 +142,8 @@ public function testSerializeEnvelopeStack(): void $this->assertEquals('test-id', $message->getId()); } - private function createSerializer(): MessageSerializer + private function createSerializer(): JsonMessageSerializer { - return new MessageSerializer(); + return new JsonMessageSerializer(); } } From 12eb2d1d0ac503b5ba3e2fc93c63e6ff9159482d Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 00:09:52 +0700 Subject: [PATCH 09/14] Add default DI binding --- config/di.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/di.php b/config/di.php index 5e4a038f..8bfe7340 100644 --- a/config/di.php +++ b/config/di.php @@ -6,6 +6,8 @@ use Yiisoft\Queue\Cli\LoopInterface; use Yiisoft\Queue\Cli\SignalLoop; use Yiisoft\Queue\Cli\SimpleLoop; +use Yiisoft\Queue\Message\JsonMessageSerializer; +use Yiisoft\Queue\Message\MessageSerializerInterface; use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher; use Yiisoft\Queue\Middleware\Consume\MiddlewareFactoryConsume; use Yiisoft\Queue\Middleware\Consume\MiddlewareFactoryConsumeInterface; @@ -52,4 +54,5 @@ FailureMiddlewareDispatcher::class => [ '__construct()' => ['middlewareDefinitions' => $params['yiisoft/queue']['middlewares-fail']], ], + MessageSerializerInterface::class => JsonMessageSerializer::class, ]; From f5d1915caa86a828ff77f0af15d0cdfd6a91dd2e Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 00:13:02 +0700 Subject: [PATCH 10/14] Add test case --- tests/Unit/Message/JsonMessageSerializerTest.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Message/JsonMessageSerializerTest.php b/tests/Unit/Message/JsonMessageSerializerTest.php index ad5ac205..776a9835 100644 --- a/tests/Unit/Message/JsonMessageSerializerTest.php +++ b/tests/Unit/Message/JsonMessageSerializerTest.php @@ -8,9 +8,9 @@ use PHPUnit\Framework\TestCase; use Yiisoft\Queue\Message\EnvelopeInterface; use Yiisoft\Queue\Message\IdEnvelope; +use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\Message\MessageInterface; -use Yiisoft\Queue\Message\JsonMessageSerializer; /** * Testing message serialization options @@ -140,6 +140,17 @@ public function testSerializeEnvelopeStack(): void $this->assertInstanceOf(IdEnvelope::class, $message); $this->assertEquals('test-id', $message->getId()); + $this->assertEquals([ + EnvelopeInterface::ENVELOPE_STACK_KEY => [ + IdEnvelope::class, + ], + IdEnvelope::MESSAGE_ID_KEY => 'test-id', + ], $message->getMetadata()); + + $this->assertEquals([ + EnvelopeInterface::ENVELOPE_STACK_KEY => [], + IdEnvelope::MESSAGE_ID_KEY => 'test-id', + ], $message->getMessage()->getMetadata()); } private function createSerializer(): JsonMessageSerializer From a0ecfa10b5ea4b13a9746c7e674296e6ae43db8e Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 16:19:30 +0700 Subject: [PATCH 11/14] Fix error string --- src/Worker/Worker.php | 2 +- tests/Unit/WorkerTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Worker/Worker.php b/src/Worker/Worker.php index a42f0c75..5f8ac7bb 100644 --- a/src/Worker/Worker.php +++ b/src/Worker/Worker.php @@ -51,7 +51,7 @@ public function process(MessageInterface $message, QueueInterface $queue): Messa $name = $message->getHandlerName(); $handler = $this->getHandler($name); if ($handler === null) { - throw new RuntimeException("Queue handler with name $name doesn't exist"); + throw new RuntimeException(sprintf('Queue handler with name "%s" does not exist', $name)); } $request = new ConsumeRequest($message, $queue); diff --git a/tests/Unit/WorkerTest.php b/tests/Unit/WorkerTest.php index 4187358f..2a17e9e3 100644 --- a/tests/Unit/WorkerTest.php +++ b/tests/Unit/WorkerTest.php @@ -108,7 +108,7 @@ public function testJobExecutedWithStaticDefinitionHandler(): void public function testJobFailWithDefinitionUndefinedMethodHandler(): void { - $this->expectExceptionMessage("Queue handler with name simple doesn't exist"); + $this->expectExceptionMessage('Queue handler with name "simple" does not exist'); $message = new Message('simple', ['test-data']); $logger = new SimpleLogger(); @@ -124,7 +124,7 @@ public function testJobFailWithDefinitionUndefinedMethodHandler(): void public function testJobFailWithDefinitionUndefinedClassHandler(): void { - $this->expectExceptionMessage("Queue handler with name simple doesn't exist"); + $this->expectExceptionMessage('Queue handler with name "simple" does not exist'); $message = new Message('simple', ['test-data']); $logger = new SimpleLogger(); @@ -146,7 +146,7 @@ public function testJobFailWithDefinitionUndefinedClassHandler(): void public function testJobFailWithDefinitionClassNotFoundInContainerHandler(): void { - $this->expectExceptionMessage("Queue handler with name simple doesn't exist"); + $this->expectExceptionMessage('Queue handler with name "simple" does not exist'); $message = new Message('simple', ['test-data']); $logger = new SimpleLogger(); $container = new SimpleContainer(); From ba21c45095cb4ae6d68c8829d88eac6d849106eb Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 16:41:39 +0700 Subject: [PATCH 12/14] Fix tests --- src/Message/JsonMessageSerializer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Message/JsonMessageSerializer.php b/src/Message/JsonMessageSerializer.php index 4d6df216..81a6220c 100644 --- a/src/Message/JsonMessageSerializer.php +++ b/src/Message/JsonMessageSerializer.php @@ -39,7 +39,8 @@ public function unserialize(string $value): MessageInterface throw new InvalidArgumentException('Metadata must be array. Got ' . get_debug_type($meta) . '.'); } - $message = new Message('$name', $payload['data'] ?? null, $meta); + // TODO: will be removed later + $message = new Message($payload['name'] ?? '$name', $payload['data'] ?? null, $meta); if (isset($meta[EnvelopeInterface::ENVELOPE_STACK_KEY]) && is_array($meta[EnvelopeInterface::ENVELOPE_STACK_KEY])) { $message = $message->withMetadata( From f98566906d38ea8fc3087c8181ce2c3ce1477657 Mon Sep 17 00:00:00 2001 From: vjik Date: Sun, 21 Jan 2024 11:35:52 +0000 Subject: [PATCH 13/14] Apply Rector changes (CI) --- src/QueueInterface.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/QueueInterface.php b/src/QueueInterface.php index b3ae71e7..75a0e98a 100644 --- a/src/QueueInterface.php +++ b/src/QueueInterface.php @@ -27,7 +27,6 @@ public function push(MessageInterface $message, MiddlewarePushInterface|callable /** * Execute all existing jobs and exit * - * @param int $max * * @return int How many messages were processed */ From bf62af97ca6f742708c46a775e51b9be7c9e4289 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 21 Jan 2024 11:36:11 +0000 Subject: [PATCH 14/14] Apply fixes from StyleCI --- src/QueueInterface.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/QueueInterface.php b/src/QueueInterface.php index 75a0e98a..489909e1 100644 --- a/src/QueueInterface.php +++ b/src/QueueInterface.php @@ -27,7 +27,6 @@ public function push(MessageInterface $message, MiddlewarePushInterface|callable /** * Execute all existing jobs and exit * - * * @return int How many messages were processed */ public function run(int $max = 0): int;