From 7975cc32b6fade5243dec24e461f93718c108a36 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 12 Jan 2024 13:01:33 +0700 Subject: [PATCH 1/8] Adjust envelop changes --- src/Adapter.php | 58 +++++++++++++++++++-------------------- src/MessageSerializer.php | 3 +- src/QueueProvider.php | 1 + 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/Adapter.php b/src/Adapter.php index 589f017..c33263c 100644 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -10,6 +10,7 @@ use Yiisoft\Queue\AMQP\Exception\NotImplementedException; use Yiisoft\Queue\Cli\LoopInterface; use Yiisoft\Queue\Enum\JobStatus; +use Yiisoft\Queue\Message\IdEnvelope; use Yiisoft\Queue\Message\MessageInterface; final class Adapter implements AdapterInterface @@ -23,33 +24,34 @@ public function __construct( public function withChannel(string $channel): self { - $instance = clone $this; - $instance->queueProvider = $this->queueProvider->withChannelName($channel); + $new = clone $this; + $new->queueProvider = $this->queueProvider->withChannelName($channel); - return $instance; + return $new; } /** - * @param callable(MessageInterface): bool $handlerCallback + * @param callable(MessageInterface): bool $handlerCallback */ public function runExisting(callable $handlerCallback): void { $channel = $this->queueProvider->getChannel(); - (new ExistingMessagesConsumer($channel, $this->queueProvider - ->getQueueSettings() - ->getName(), $this->serializer)) - ->consume($handlerCallback); + $queueName = $this->queueProvider->getQueueSettings()->getName(); + $consumer = new ExistingMessagesConsumer( + $channel, + $queueName, + $this->serializer + ); + + $consumer->consume($handlerCallback); } - /** - * @return never - */ - public function status(string $id): JobStatus + public function status(string|int $id): JobStatus { throw new NotImplementedException('Status check is not supported by the adapter ' . self::class . '.'); } - public function push(MessageInterface $message): void + public function push(MessageInterface $message): MessageInterface { $payload = $this->serializer->serialize($message); $amqpMessage = new AMQPMessage( @@ -57,30 +59,28 @@ public function push(MessageInterface $message): void array_merge(['message_id' => uniqid(more_entropy: true)], $this->queueProvider->getMessageProperties()) ); $exchangeSettings = $this->queueProvider->getExchangeSettings(); - $this->queueProvider - ->getChannel() - ->basic_publish( - $amqpMessage, - $exchangeSettings?->getName() ?? '', - $exchangeSettings ? '' : $this->queueProvider - ->getQueueSettings() - ->getName() - ); + $channel = $this->queueProvider->getChannel(); + $channel->basic_publish( + $amqpMessage, + $exchangeSettings?->getName() ?? '', + $exchangeSettings ? '' : $this->queueProvider + ->getQueueSettings() + ->getName() + ); /** @var string $messageId */ $messageId = $amqpMessage->get('message_id'); - $message->setId($messageId); + + return new IdEnvelope($message, $messageId); } public function subscribe(callable $handlerCallback): void { $channel = $this->queueProvider->getChannel(); + $queueName = $this->queueProvider->getQueueSettings()->getName(); + $channel->basic_consume( - $this->queueProvider - ->getQueueSettings() - ->getName(), - $this->queueProvider - ->getQueueSettings() - ->getName(), + $queueName, + $queueName, false, false, false, diff --git a/src/MessageSerializer.php b/src/MessageSerializer.php index 9afc07e..e91de5a 100644 --- a/src/MessageSerializer.php +++ b/src/MessageSerializer.php @@ -7,6 +7,7 @@ use InvalidArgumentException; use JsonException; use Yiisoft\Queue\AMQP\Exception\NoKeyInPayloadException; +use Yiisoft\Queue\Message\IdEnvelope; use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\Message\MessageInterface; @@ -18,7 +19,7 @@ class MessageSerializer implements MessageSerializerInterface public function serialize(MessageInterface $message): string { $payload = [ - 'id' => $message->getId(), + 'id' => $message->getMetadata()[IdEnvelope::MESSAGE_ID_KEY] ?? null, 'name' => $message->getHandlerName(), 'data' => $message->getData(), 'meta' => $message->getMetadata(), diff --git a/src/QueueProvider.php b/src/QueueProvider.php index 54bf096..2d64060 100644 --- a/src/QueueProvider.php +++ b/src/QueueProvider.php @@ -31,6 +31,7 @@ public function __construct( public function __destruct() { $this->channel?->close(); + unset($this->channel); } public function getChannel(): AMQPChannel From 04ff42933cc2d23b85e83becede8e6818cdebe01 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 12 Jan 2024 13:01:48 +0700 Subject: [PATCH 2/8] Set channels and exchanges not auto-deletable --- src/Settings/Exchange.php | 2 +- src/Settings/Queue.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Settings/Exchange.php b/src/Settings/Exchange.php index 53e54d8..25935de 100644 --- a/src/Settings/Exchange.php +++ b/src/Settings/Exchange.php @@ -14,7 +14,7 @@ public function __construct( private string $type = AMQPExchangeType::DIRECT, private bool $passive = false, private bool $durable = false, - private bool $autoDelete = true, + private bool $autoDelete = false, private bool $internal = false, private bool $nowait = false, private AMQPTable|array $arguments = [], diff --git a/src/Settings/Queue.php b/src/Settings/Queue.php index 69cb206..6df4bb8 100644 --- a/src/Settings/Queue.php +++ b/src/Settings/Queue.php @@ -14,7 +14,7 @@ public function __construct( private bool $passive = false, private bool $durable = false, private bool $exclusive = false, - private bool $autoDelete = true, + private bool $autoDelete = false, private bool $nowait = false, private AMQPTable|array $arguments = [], private ?int $ticket = null From eb1153fe312644be657064ebbd94cebfe4962fa9 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 12 Jan 2024 13:02:29 +0700 Subject: [PATCH 3/8] Adjust envelop changes --- tests/Support/FakeAdapter.php | 4 ++-- tests/Support/FileHelper.php | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/Support/FakeAdapter.php b/tests/Support/FakeAdapter.php index ddbbe15..3f0f7ee 100644 --- a/tests/Support/FakeAdapter.php +++ b/tests/Support/FakeAdapter.php @@ -25,12 +25,12 @@ public function runExisting(callable $handlerCallback): void // TODO: Implement runExisting() method. } - public function status(string $id): JobStatus + public function status(string|int $id): JobStatus { // TODO: Implement status() method. } - public function push(MessageInterface $message): void + public function push(MessageInterface $message): MessageInterface { // TODO: Implement push() method. } diff --git a/tests/Support/FileHelper.php b/tests/Support/FileHelper.php index beff8b2..d1674a2 100644 --- a/tests/Support/FileHelper.php +++ b/tests/Support/FileHelper.php @@ -14,8 +14,15 @@ final class FileHelper */ public function put(string $fileName, int|string $data): void { - if (file_put_contents("{$this->getRuntimeDir()}/$fileName", $data) === false) { - throw new RuntimeException("Runtime dir {$this->getRuntimeDir()} or file $fileName are not writable."); + $path = $this->getRuntimeDir() . DIRECTORY_SEPARATOR . $fileName; + if (file_put_contents($path, $data) === false) { + throw new RuntimeException( + sprintf( + 'Runtime dir %"s" or file "%s" are not writable.', + $this->getRuntimeDir(), + $fileName + ) + ); } } @@ -28,7 +35,12 @@ public function get(string $filename): ?string $result = file_get_contents($path); if ($result === false) { - throw new RuntimeException("File '$path' exists but is not readable."); + throw new RuntimeException( + sprintf( + 'File "%s" exists but is not readable.', + $path + ) + ); } return $result; From ebca70ec17278c7b5753cb5918f5bb5d7b34b02c Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 12 Jan 2024 13:02:47 +0700 Subject: [PATCH 4/8] Add management plugin --- tests/docker-compose.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index 18aebec..3a56465 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -44,9 +44,12 @@ services: PHP_VERSION: '8.2' rabbitmq: - image: rabbitmq:3-alpine + image: rabbitmq:3-management healthcheck: test: rabbitmq-diagnostics check_port_connectivity interval: 3s timeout: 5s retries: 3 + ports: + - 5672:5672 + - 5673:15672 From 150486df111d2fddfd995b93798fa66fdf5d86d0 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 12 Jan 2024 13:03:22 +0700 Subject: [PATCH 5/8] Rename vars --- src/Middleware/DelayMiddleware.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Middleware/DelayMiddleware.php b/src/Middleware/DelayMiddleware.php index c1eb0a7..b48d50f 100644 --- a/src/Middleware/DelayMiddleware.php +++ b/src/Middleware/DelayMiddleware.php @@ -43,20 +43,27 @@ public function processPush(PushRequest $request, MessageHandlerPushInterface $h { $adapter = $request->getAdapter(); if (!$adapter instanceof Adapter) { - $type = get_debug_type($adapter); - $class = Adapter::class; throw new InvalidArgumentException( - "This middleware works only with the $class. $type given." + sprintf( + 'This middleware works only with the %s. %s given.', + Adapter::class, + get_debug_type($adapter) + ) ); } $queueProvider = $adapter->getQueueProvider(); - $exchangeSettings = $this->getExchangeSettings($queueProvider->getExchangeSettings()); - $queueSettings = $this->getQueueSettings($queueProvider->getQueueSettings(), $queueProvider->getExchangeSettings()); + $originalExchangeSettings = $queueProvider->getExchangeSettings(); + $delayedExchangeSettings = $this->getExchangeSettings($originalExchangeSettings); + $queueSettings = $this->getQueueSettings( + $queueProvider->getQueueSettings(), + $originalExchangeSettings + ); + $adapter = $adapter->withQueueProvider( $queueProvider ->withMessageProperties($this->getMessageProperties($queueProvider)) - ->withExchangeSettings($exchangeSettings) + ->withExchangeSettings($delayedExchangeSettings) ->withQueueSettings($queueSettings) ); From 662879c97642c3bcb5530d16f88f767cdcf47a88 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 16:17:35 +0700 Subject: [PATCH 6/8] Adjust changes --- src/Adapter.php | 1 + src/Exception/NoKeyInPayloadException.php | 54 --------- src/ExistingMessagesConsumer.php | 1 + src/MessageSerializer.php | 65 ----------- src/MessageSerializerInterface.php | 14 --- src/Middleware/DelayMiddleware.php | 2 +- src/QueueProvider.php | 2 +- tests/Integration/DelayMiddlewareTest.php | 9 +- tests/Support/MainTestCase.php | 8 +- tests/Unit/ExchangeSettingsTest.php | 5 +- tests/Unit/FriendlyExceptionTest.php | 29 ----- tests/Unit/MessageSerializerTest.php | 134 ---------------------- tests/Unit/QueueProviderTest.php | 7 +- tests/Unit/QueueSettingsTest.php | 9 +- tests/Unit/QueueTest.php | 19 +-- tests/Unit/UnitTestCase.php | 5 +- tests/yii | 3 +- 17 files changed, 40 insertions(+), 327 deletions(-) delete mode 100644 src/Exception/NoKeyInPayloadException.php delete mode 100644 src/MessageSerializer.php delete mode 100644 src/MessageSerializerInterface.php delete mode 100644 tests/Unit/FriendlyExceptionTest.php delete mode 100644 tests/Unit/MessageSerializerTest.php diff --git a/src/Adapter.php b/src/Adapter.php index c33263c..772f675 100644 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -12,6 +12,7 @@ use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\IdEnvelope; use Yiisoft\Queue\Message\MessageInterface; +use Yiisoft\Queue\Message\MessageSerializerInterface; final class Adapter implements AdapterInterface { diff --git a/src/Exception/NoKeyInPayloadException.php b/src/Exception/NoKeyInPayloadException.php deleted file mode 100644 index 79ec038..0000000 --- a/src/Exception/NoKeyInPayloadException.php +++ /dev/null @@ -1,54 +0,0 @@ -expectedKey . '" in payload'; - } - - /** - * @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/ExistingMessagesConsumer.php b/src/ExistingMessagesConsumer.php index 4e030eb..f01f97a 100644 --- a/src/ExistingMessagesConsumer.php +++ b/src/ExistingMessagesConsumer.php @@ -8,6 +8,7 @@ use PhpAmqpLib\Message\AMQPMessage; use Throwable; use Yiisoft\Queue\Message\MessageInterface; +use Yiisoft\Queue\Message\MessageSerializerInterface; /** * @internal diff --git a/src/MessageSerializer.php b/src/MessageSerializer.php deleted file mode 100644 index e91de5a..0000000 --- a/src/MessageSerializer.php +++ /dev/null @@ -1,65 +0,0 @@ - $message->getMetadata()[IdEnvelope::MESSAGE_ID_KEY] ?? null, - 'name' => $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): Message - { - $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) . '.'); - } - - $name = $payload['name'] ?? null; - if (!is_string($name)) { - throw new NoKeyInPayloadException('name', $payload); - } - - $id = $payload['id'] ?? null; - if ($id !== null && !is_string($id)) { - throw new NoKeyInPayloadException('id', $payload); - } - - $meta = $payload['meta'] ?? []; - if (!is_array($meta)) { - throw new NoKeyInPayloadException('meta', $payload); - } - - return new Message( - $name, - $payload['data'] ?? null, - $meta, - $id, - ); - } -} diff --git a/src/MessageSerializerInterface.php b/src/MessageSerializerInterface.php deleted file mode 100644 index 4d84521..0000000 --- a/src/MessageSerializerInterface.php +++ /dev/null @@ -1,14 +0,0 @@ -withName("{$exchangeSettings->getName()}.dlx") - ->withAutoDelete(true) + ->withAutoDelete(false) ->withType(AMQPExchangeType::TOPIC); } } diff --git a/src/QueueProvider.php b/src/QueueProvider.php index 2d64060..ae50b8b 100644 --- a/src/QueueProvider.php +++ b/src/QueueProvider.php @@ -31,7 +31,7 @@ public function __construct( public function __destruct() { $this->channel?->close(); - unset($this->channel); + //unset($this->channel); } public function getChannel(): AMQPChannel diff --git a/tests/Integration/DelayMiddlewareTest.php b/tests/Integration/DelayMiddlewareTest.php index 2d4003e..45a1209 100644 --- a/tests/Integration/DelayMiddlewareTest.php +++ b/tests/Integration/DelayMiddlewareTest.php @@ -9,7 +9,7 @@ use Psr\Log\LoggerInterface; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\AMQP\Adapter; -use Yiisoft\Queue\AMQP\MessageSerializer; + use Yiisoft\Queue\AMQP\Middleware\DelayMiddleware; use Yiisoft\Queue\AMQP\QueueProvider; use Yiisoft\Queue\AMQP\Settings\Queue as QueueSettings; @@ -18,6 +18,7 @@ use Yiisoft\Queue\AMQP\Tests\Support\SimpleMessageHandler; use Yiisoft\Queue\Cli\LoopInterface; use Yiisoft\Queue\Cli\SignalLoop; +use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\Middleware\CallableFactory; use Yiisoft\Queue\Middleware\Push\MiddlewareFactoryPush; @@ -44,7 +45,7 @@ public function testMainFlow(): void $this->createConnection(), new QueueSettings(), ), - new MessageSerializer(), + new JsonMessageSerializer(), new SignalLoop(), ); $queue = $this->makeQueue($adapter); @@ -75,7 +76,7 @@ public function testMainFlowWithFakeAdapter(): void $this->createConnection(), new QueueSettings(), ), - new MessageSerializer(), + new JsonMessageSerializer(), new SignalLoop(), ); $queue = $this->makeQueue($adapter); @@ -83,7 +84,7 @@ public function testMainFlowWithFakeAdapter(): void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage("This middleware works only with the $adapterClass. $fakeAdapterClass given."); $queue->push( - new Message('simple', 'test-delay-middleware-main'), + new Message(SimpleMessageHandler::class, 'test-delay-middleware-main'), new DelayMiddleware(3), ); } diff --git a/tests/Support/MainTestCase.php b/tests/Support/MainTestCase.php index 66a9f16..030f1ae 100644 --- a/tests/Support/MainTestCase.php +++ b/tests/Support/MainTestCase.php @@ -15,10 +15,10 @@ abstract class MainTestCase extends TestCase protected function createConnection(): AMQPStreamConnection { return new AMQPStreamConnection( - getenv('RABBITMQ_HOST'), - getenv('RABBITMQ_PORT'), - getenv('RABBITMQ_USER'), - getenv('RABBITMQ_PASSWORD') + getenv('RABBITMQ_HOST') ?: '127.0.0.1', + getenv('RABBITMQ_PORT') ?: 5672, + getenv('RABBITMQ_USER') ?: 'guest', + getenv('RABBITMQ_PASSWORD') ?: 'guest', ); } diff --git a/tests/Unit/ExchangeSettingsTest.php b/tests/Unit/ExchangeSettingsTest.php index 5d16634..879dd19 100644 --- a/tests/Unit/ExchangeSettingsTest.php +++ b/tests/Unit/ExchangeSettingsTest.php @@ -7,10 +7,11 @@ use PhpAmqpLib\Exchange\AMQPExchangeType; use PhpAmqpLib\Wire\AMQPTable; use Yiisoft\Queue\AMQP\Adapter; -use Yiisoft\Queue\AMQP\MessageSerializer; + use Yiisoft\Queue\AMQP\QueueProvider; use Yiisoft\Queue\AMQP\Settings\Exchange as ExchangeSettings; use Yiisoft\Queue\AMQP\Settings\Queue as QueueSettings; +use Yiisoft\Queue\Message\JsonMessageSerializer; final class ExchangeSettingsTest extends UnitTestCase { @@ -38,7 +39,7 @@ public function testCommonSettings(): void ]) ) ), - new MessageSerializer(), + new JsonMessageSerializer(), $this->getLoop(), ); $exchangeSettings = $adapter->getQueueProvider()->getExchangeSettings(); diff --git a/tests/Unit/FriendlyExceptionTest.php b/tests/Unit/FriendlyExceptionTest.php deleted file mode 100644 index 6e14fab..0000000 --- a/tests/Unit/FriendlyExceptionTest.php +++ /dev/null @@ -1,29 +0,0 @@ -getName()); - $this->assertMatchesRegularExpression('/test/', $exception->getSolution()); - } - - public function testExchangeDeclaredException(): void - { - $exception = new ExchangeDeclaredException(); - - self::assertSame('Exchange is declared', $exception->getName()); - } -} diff --git a/tests/Unit/MessageSerializerTest.php b/tests/Unit/MessageSerializerTest.php deleted file mode 100644 index 8a2c9a0..0000000 --- a/tests/Unit/MessageSerializerTest.php +++ /dev/null @@ -1,134 +0,0 @@ -createConnection() - ->channel(); - $channel->queue_declare($queue); - $channel->exchange_declare($exchange, AMQPExchangeType::DIRECT); - $channel->queue_bind($queue, $exchange); - $channel->basic_publish($message, $exchange); - } - - /** - * @throws Exception - */ - private function getCustomAdapter(string $queueExchangeName): Adapter - { - $queueProvider = new QueueProvider( - $this->createConnection(), - $this->getQueueSettings(), - ); - return new Adapter( - $queueProvider - ->withQueueSettings(new QueueSettings($queueExchangeName)) - ->withExchangeSettings(new ExchangeSettings($queueExchangeName)), - new MessageSerializer(), - $this->getLoop(), - ); - } - - public function testNoKeyInPayloadExceptionName(): void - { - $queueExchangeName = 'yii-test-no-key-in-payload-exception-name'; - $this->publishWithAMQPLib( - $queueExchangeName, - $queueExchangeName, - new AMQPMessage( - json_encode(['test'], JSON_THROW_ON_ERROR), - ['content_type' => 'text/json', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT] - ) - ); - - $this->expectException(NoKeyInPayloadException::class); - $this - ->getQueue() - ->withAdapter($this->getCustomAdapter($queueExchangeName)) - ->run(); - } - - public function testNoKeyInPayloadExceptionId(): void - { - $queueExchangeName = 'yii-test-no-key-in-payload-exception-id'; - $this->publishWithAMQPLib( - $queueExchangeName, - $queueExchangeName, - new AMQPMessage( - json_encode(['name' => 'ext-simple', 'id' => 1], JSON_THROW_ON_ERROR), - ['content_type' => 'text/json', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT] - ) - ); - - $this->expectException(NoKeyInPayloadException::class); - $this->expectExceptionMessage("No expected key 'id' in payload. Payload's keys list: name, id."); - $this - ->getQueue() - ->withAdapter($this->getCustomAdapter($queueExchangeName)) - ->run(); - } - - public function testNoKeyInPayloadExceptionMeta(): void - { - $queueExchangeName = 'yii-test-no-key-in-payload-exception-meta'; - $this->publishWithAMQPLib( - $queueExchangeName, - $queueExchangeName, - new AMQPMessage( - json_encode(['name' => 'ext-simple', 'meta' => ''], JSON_THROW_ON_ERROR), - ['content_type' => 'text/json', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT] - ) - ); - - $this->expectException(NoKeyInPayloadException::class); - $this->expectExceptionMessage("No expected key 'meta' in payload. Payload's keys list: name, meta."); - $this - ->getQueue() - ->withAdapter($this->getCustomAdapter($queueExchangeName)) - ->run(); - } - - public function testInvalidArgumentException(): void - { - $queueExchangeName = 'yii-test-invalid-argument-exception'; - $this->publishWithAMQPLib( - $queueExchangeName, - $queueExchangeName, - new AMQPMessage( - json_encode('test', JSON_THROW_ON_ERROR), - ['content_type' => 'text/json', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT] - ) - ); - - $this->expectException(InvalidArgumentException::class); - $this - ->getQueue() - ->withAdapter($this->getCustomAdapter($queueExchangeName)) - ->run(); - } -} diff --git a/tests/Unit/QueueProviderTest.php b/tests/Unit/QueueProviderTest.php index 897f257..da9584c 100644 --- a/tests/Unit/QueueProviderTest.php +++ b/tests/Unit/QueueProviderTest.php @@ -6,13 +6,14 @@ use Yiisoft\Queue\AMQP\Adapter; use Yiisoft\Queue\AMQP\Exception\ExchangeDeclaredException; -use Yiisoft\Queue\AMQP\MessageSerializer; + use Yiisoft\Queue\AMQP\QueueProvider; use Yiisoft\Queue\AMQP\Settings\Exchange as ExchangeSettings; use Yiisoft\Queue\AMQP\Settings\ExchangeSettingsInterface; use Yiisoft\Queue\AMQP\Settings\Queue as QueueSettings; use Yiisoft\Queue\AMQP\Settings\QueueSettingsInterface; use Yiisoft\Queue\AMQP\Tests\Support\FileHelper; +use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Queue\Message\Message; final class QueueProviderTest extends UnitTestCase @@ -34,7 +35,7 @@ public function testWithQueueAndExchangeSettings(): void ->withExchangeSettings( new ExchangeSettings($this->exchangeName) ), - new MessageSerializer(), + new JsonMessageSerializer(), $this->getLoop(), ); @@ -81,7 +82,7 @@ public function testWithChannelNameExchangeDeclaredException(): void new ExchangeSettings('yii-queue-test-with-channel-name') ) ->withChannelName('yii-queue-test-channel-name'), - new MessageSerializer(), + new JsonMessageSerializer(), $this->getLoop(), ); } diff --git a/tests/Unit/QueueSettingsTest.php b/tests/Unit/QueueSettingsTest.php index 446e6db..ed61cf3 100644 --- a/tests/Unit/QueueSettingsTest.php +++ b/tests/Unit/QueueSettingsTest.php @@ -7,10 +7,11 @@ use PhpAmqpLib\Exception\AMQPProtocolChannelException; use PhpAmqpLib\Wire\AMQPTable; use Yiisoft\Queue\AMQP\Adapter; -use Yiisoft\Queue\AMQP\MessageSerializer; + use Yiisoft\Queue\AMQP\QueueProvider; use Yiisoft\Queue\AMQP\Settings\Exchange as ExchangeSettings; use Yiisoft\Queue\AMQP\Settings\Queue as QueueSettings; +use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Queue\Message\Message; final class QueueSettingsTest extends UnitTestCase @@ -40,7 +41,7 @@ public function testCommonSettings(): void ->withExchangeSettings( new ExchangeSettings('yii-queue-test-queue-common-settings') ), - new MessageSerializer(), + new JsonMessageSerializer(), $this->getLoop(), ); @@ -49,7 +50,7 @@ public function testCommonSettings(): void self::assertTrue($queueSettings->isDurable()); self::assertTrue($queueSettings->isPassive()); self::assertTrue($queueSettings->isExclusive()); - self::assertTrue($queueSettings->isAutoDeletable()); + self::assertFalse($queueSettings->isAutoDeletable()); self::assertTrue($queueSettings->hasNowait()); self::assertNull($queueSettings->getTicket()); @@ -85,7 +86,7 @@ public function testArgumentsXExpires(): void ->withExchangeSettings( new ExchangeSettings('yii-queue-test-queue-settings-arg') ), - new MessageSerializer(), + new JsonMessageSerializer(), $this->getLoop(), ); diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index 84f1619..c24a134 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -8,8 +8,6 @@ use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\AMQP\Adapter; use Yiisoft\Queue\AMQP\Exception\NotImplementedException; -use Yiisoft\Queue\AMQP\MessageSerializer; -use Yiisoft\Queue\AMQP\MessageSerializerInterface; use Yiisoft\Queue\AMQP\QueueProvider; use Yiisoft\Queue\AMQP\QueueProviderInterface; use Yiisoft\Queue\AMQP\Settings\Exchange as ExchangeSettings; @@ -17,7 +15,10 @@ use Yiisoft\Queue\AMQP\Tests\Support\FileHelper; use Yiisoft\Queue\Cli\LoopInterface; use Yiisoft\Queue\Exception\JobFailureException; +use Yiisoft\Queue\Message\IdEnvelope; +use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Queue\Message\Message; +use Yiisoft\Queue\Message\MessageSerializerInterface; use Yiisoft\Queue\Queue; final class QueueTest extends UnitTestCase @@ -34,10 +35,11 @@ public function testStatus(): void $queue = $this->getDefaultQueue($adapter); - $message = new Message('ext-simple', null); - $queue->push( - $message, + $message = new IdEnvelope( + new Message('ext-simple', null), + 'test-id', ); + $queue->push($message); $this->expectException(NotImplementedException::class); $this->expectExceptionMessage("Status check is not supported by the adapter $adapterClass."); @@ -84,7 +86,7 @@ public function testListenWithException(): void $queueProvider ->withQueueSettings(new QueueSettings($this->queueName)) ->withExchangeSettings(new ExchangeSettings($this->exchangeName)), - new MessageSerializer(), + new JsonMessageSerializer(), $this->getLoop(), ); $queue = $this->getDefaultQueue($adapter); @@ -110,9 +112,8 @@ public function testListen(): void $this->getQueueSettings(), ); $adapter = new Adapter( - $queueProvider - ->withChannelName('yii-queue'), - new MessageSerializer(), + $queueProvider->withChannelName('yii-queue'), + new JsonMessageSerializer(), $mockLoop, ); $queue = $this->getDefaultQueue($adapter); diff --git a/tests/Unit/UnitTestCase.php b/tests/Unit/UnitTestCase.php index 2f119c5..e617ab6 100644 --- a/tests/Unit/UnitTestCase.php +++ b/tests/Unit/UnitTestCase.php @@ -10,7 +10,7 @@ use Yiisoft\Injector\Injector; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\AMQP\Adapter; -use Yiisoft\Queue\AMQP\MessageSerializer; + use Yiisoft\Queue\AMQP\QueueProvider; use Yiisoft\Queue\AMQP\Settings\Queue as QueueSettings; use Yiisoft\Queue\AMQP\Tests\Support\ExtendedSimpleMessageHandler; @@ -18,6 +18,7 @@ use Yiisoft\Queue\AMQP\Tests\Support\MainTestCase; use Yiisoft\Queue\Cli\LoopInterface; use Yiisoft\Queue\Cli\SignalLoop; +use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Queue\Message\MessageInterface; use Yiisoft\Queue\Middleware\CallableFactory; use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher; @@ -134,7 +135,7 @@ protected function getAdapter(): AdapterInterface { return $this->adapter ??= new Adapter( $this->getQueueProvider(), - new MessageSerializer(), + new JsonMessageSerializer(), $this->getLoop(), ); } diff --git a/tests/yii b/tests/yii index 7ae7c3e..6918a2b 100755 --- a/tests/yii +++ b/tests/yii @@ -4,6 +4,7 @@ use PhpAmqpLib\Connection\AMQPStreamConnection; use Psr\Log\NullLogger; use Yiisoft\Injector\Injector; +use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Test\Support\Container\SimpleContainer; use Yiisoft\Yii\Console\Application; use Yiisoft\Queue\AMQP\Adapter; @@ -53,7 +54,7 @@ $adapter = new Adapter( ), new QueueSettings(), ), - new MessageSerializer(), + new JsonMessageSerializer(), $loop, ); $queue = new Queue( From c24813d385809266008e5f93af83b2bb12a9a098 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 16:18:14 +0700 Subject: [PATCH 7/8] Remove unused import --- tests/yii | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/yii b/tests/yii index 6918a2b..48d48f9 100755 --- a/tests/yii +++ b/tests/yii @@ -8,7 +8,7 @@ use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Test\Support\Container\SimpleContainer; use Yiisoft\Yii\Console\Application; use Yiisoft\Queue\AMQP\Adapter; -use Yiisoft\Queue\AMQP\MessageSerializer; + use Yiisoft\Queue\AMQP\QueueProvider; use Yiisoft\Queue\AMQP\Settings\Queue as QueueSettings; use Yiisoft\Queue\AMQP\Tests\Support\FileHelper; From 8275ab109a78e02f3b896ea4035fce76963cba3f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 14 Jan 2024 16:41:39 +0700 Subject: [PATCH 8/8] Fix tests --- tests/Support/FakeAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/FakeAdapter.php b/tests/Support/FakeAdapter.php index 3f0f7ee..cf8a7c9 100644 --- a/tests/Support/FakeAdapter.php +++ b/tests/Support/FakeAdapter.php @@ -5,11 +5,11 @@ namespace Yiisoft\Queue\AMQP\Tests\Support; use Yiisoft\Queue\Adapter\AdapterInterface; -use Yiisoft\Queue\AMQP\MessageSerializerInterface; use Yiisoft\Queue\AMQP\QueueProviderInterface; use Yiisoft\Queue\Cli\LoopInterface; use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\MessageInterface; +use Yiisoft\Queue\Message\MessageSerializerInterface; final class FakeAdapter implements AdapterInterface {