From 3f39575bbda1495b502815ef1a7c1b249586ac4c Mon Sep 17 00:00:00 2001 From: viktorprogger Date: Wed, 8 Jan 2025 17:19:35 +0500 Subject: [PATCH 1/4] Update due to yiisoft/queue changes --- src/Adapter.php | 25 ++++--- src/Exception/NoKeyInPayloadException.php | 2 - src/ExistingMessagesConsumer.php | 8 +-- src/MessageSerializer.php | 12 ++-- src/Middleware/DelayMiddleware.php | 2 +- src/QueueProvider.php | 2 +- src/Settings/Queue.php | 4 +- .../Support/ExtendedSimpleMessageHandler.php | 2 +- tests/Support/FakeAdapter.php | 18 +++-- tests/Support/SimpleMessageHandler.php | 2 +- tests/Unit/QueueFactoryTest.php | 71 ------------------- tests/Unit/QueueProviderTest.php | 2 +- tests/Unit/QueueTest.php | 3 +- 13 files changed, 49 insertions(+), 104 deletions(-) delete mode 100644 tests/Unit/QueueFactoryTest.php diff --git a/src/Adapter.php b/src/Adapter.php index 589f017..4c6e17f 100644 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -4,27 +4,30 @@ namespace Yiisoft\Queue\AMQP; +use BackedEnum; use PhpAmqpLib\Message\AMQPMessage; use Throwable; use Yiisoft\Queue\Adapter\AdapterInterface; 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 { public function __construct( private QueueProviderInterface $queueProvider, - private MessageSerializerInterface $serializer, - private LoopInterface $loop, + private readonly MessageSerializerInterface $serializer, + private readonly LoopInterface $loop, ) { } - public function withChannel(string $channel): self + public function withChannel(BackedEnum|string $channel): self { $instance = clone $this; - $instance->queueProvider = $this->queueProvider->withChannelName($channel); + $channelName = is_string($channel) ? $channel : (string) $channel->value; + $instance->queueProvider = $this->queueProvider->withChannelName($channelName); return $instance; } @@ -44,12 +47,12 @@ public function runExisting(callable $handlerCallback): void /** * @return never */ - public function status(string $id): JobStatus + public function status(int|string $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( @@ -68,7 +71,8 @@ public function push(MessageInterface $message): void ); /** @var string $messageId */ $messageId = $amqpMessage->get('message_id'); - $message->setId($messageId); + + return new IdEnvelope($message, $messageId); } public function subscribe(callable $handlerCallback): void @@ -87,7 +91,7 @@ public function subscribe(callable $handlerCallback): void true, function (AMQPMessage $amqpMessage) use ($handlerCallback, $channel): void { try { - $handlerCallback($this->serializer->unserialize($amqpMessage->body)); + $handlerCallback($this->serializer->unserialize($amqpMessage->getBody())); $channel->basic_ack($amqpMessage->getDeliveryTag()); } catch (Throwable $exception) { $consumerTag = $amqpMessage->getConsumerTag(); @@ -117,4 +121,9 @@ public function withQueueProvider(QueueProviderInterface $queueProvider): self return $new; } + + public function getChannel(): string + { + return $this->queueProvider->getQueueSettings()->getName(); + } } diff --git a/src/Exception/NoKeyInPayloadException.php b/src/Exception/NoKeyInPayloadException.php index 79ec038..27f91f2 100644 --- a/src/Exception/NoKeyInPayloadException.php +++ b/src/Exception/NoKeyInPayloadException.php @@ -35,8 +35,6 @@ public function getName(): string } /** - * @return string - * * @infection-ignore-all */ public function getSolution(): ?string diff --git a/src/ExistingMessagesConsumer.php b/src/ExistingMessagesConsumer.php index 4e030eb..4fbf4d2 100644 --- a/src/ExistingMessagesConsumer.php +++ b/src/ExistingMessagesConsumer.php @@ -17,9 +17,9 @@ final class ExistingMessagesConsumer private bool $messageConsumed = false; public function __construct( - private AMQPChannel $channel, - private string $queueName, - private MessageSerializerInterface $serializer + private readonly AMQPChannel $channel, + private readonly string $queueName, + private readonly MessageSerializerInterface $serializer ) { } @@ -37,7 +37,7 @@ public function consume(callable $callback): void false, function (AMQPMessage $amqpMessage) use ($callback): void { try { - $message = $this->serializer->unserialize($amqpMessage->body); + $message = $this->serializer->unserialize($amqpMessage->getBody()); if ($this->messageConsumed = $callback($message)) { $this->channel->basic_ack($amqpMessage->getDeliveryTag()); } diff --git a/src/MessageSerializer.php b/src/MessageSerializer.php index 9afc07e..0435312 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' => IdEnvelope::fromMessage($message)->getId(), 'name' => $message->getHandlerName(), 'data' => $message->getData(), 'meta' => $message->getMetadata(), @@ -32,7 +33,7 @@ public function serialize(MessageInterface $message): string * @throws NoKeyInPayloadException * @throws InvalidArgumentException */ - public function unserialize(string $value): Message + public function unserialize(string $value): MessageInterface { $payload = json_decode($value, true, 512, JSON_THROW_ON_ERROR); if (!is_array($payload)) { @@ -45,7 +46,7 @@ public function unserialize(string $value): Message } $id = $payload['id'] ?? null; - if ($id !== null && !is_string($id)) { + if ($id !== null && !is_string($id) && !is_int($id)) { throw new NoKeyInPayloadException('id', $payload); } @@ -54,11 +55,12 @@ public function unserialize(string $value): Message throw new NoKeyInPayloadException('meta', $payload); } - return new Message( + $message = new Message( $name, $payload['data'] ?? null, $meta, - $id, ); + + return $id === null ? $message : new IdEnvelope($message, $id); } } diff --git a/src/Middleware/DelayMiddleware.php b/src/Middleware/DelayMiddleware.php index c1eb0a7..6bfd93c 100644 --- a/src/Middleware/DelayMiddleware.php +++ b/src/Middleware/DelayMiddleware.php @@ -17,7 +17,7 @@ final class DelayMiddleware implements DelayMiddlewareInterface { - public function __construct(private float $delayInSeconds, private bool $forcePersistentMessages = true) + public function __construct(private float $delayInSeconds, private readonly bool $forcePersistentMessages = true) { } diff --git a/src/QueueProvider.php b/src/QueueProvider.php index 54bf096..3af0873 100644 --- a/src/QueueProvider.php +++ b/src/QueueProvider.php @@ -18,7 +18,7 @@ final class QueueProvider implements QueueProviderInterface private ?AMQPChannel $channel = null; public function __construct( - private AbstractConnection $connection, + private readonly AbstractConnection $connection, private QueueSettingsInterface $queueSettings, private ?ExchangeSettingsInterface $exchangeSettings = null, private array $messageProperties = [], diff --git a/src/Settings/Queue.php b/src/Settings/Queue.php index 69cb206..163f75a 100644 --- a/src/Settings/Queue.php +++ b/src/Settings/Queue.php @@ -5,12 +5,12 @@ namespace Yiisoft\Queue\AMQP\Settings; use PhpAmqpLib\Wire\AMQPTable; -use Yiisoft\Queue\QueueFactoryInterface; +use Yiisoft\Queue\QueueInterface; final class Queue implements QueueSettingsInterface { public function __construct( - private string $queueName = QueueFactoryInterface::DEFAULT_CHANNEL_NAME, + private string $queueName = QueueInterface::DEFAULT_CHANNEL, private bool $passive = false, private bool $durable = false, private bool $exclusive = false, diff --git a/tests/Support/ExtendedSimpleMessageHandler.php b/tests/Support/ExtendedSimpleMessageHandler.php index 2c5ad88..fe8603b 100644 --- a/tests/Support/ExtendedSimpleMessageHandler.php +++ b/tests/Support/ExtendedSimpleMessageHandler.php @@ -11,7 +11,7 @@ */ final class ExtendedSimpleMessageHandler { - public function __construct(private FileHelper $fileHelper) + public function __construct(private readonly FileHelper $fileHelper) { } diff --git a/tests/Support/FakeAdapter.php b/tests/Support/FakeAdapter.php index ddbbe15..614d923 100644 --- a/tests/Support/FakeAdapter.php +++ b/tests/Support/FakeAdapter.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\AMQP\Tests\Support; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\AMQP\MessageSerializerInterface; use Yiisoft\Queue\AMQP\QueueProviderInterface; @@ -14,9 +15,9 @@ final class FakeAdapter implements AdapterInterface { public function __construct( - private QueueProviderInterface $queueProvider, - private MessageSerializerInterface $serializer, - private LoopInterface $loop, + private readonly QueueProviderInterface $queueProvider, + private readonly MessageSerializerInterface $serializer, + private readonly LoopInterface $loop, ) { } @@ -25,12 +26,12 @@ public function runExisting(callable $handlerCallback): void // TODO: Implement runExisting() method. } - public function status(string $id): JobStatus + public function status(int|string $id): JobStatus { // TODO: Implement status() method. } - public function push(MessageInterface $message): void + public function push(MessageInterface $message): MessageInterface { // TODO: Implement push() method. } @@ -40,8 +41,13 @@ public function subscribe(callable $handlerCallback): void // TODO: Implement subscribe() method. } - public function withChannel(string $channel): AdapterInterface + public function withChannel(BackedEnum|string $channel): AdapterInterface { // TODO: Implement withChannel() method. } + + public function getChannel(): string + { + // TODO: Implement getChannel() method. + } } diff --git a/tests/Support/SimpleMessageHandler.php b/tests/Support/SimpleMessageHandler.php index 47c814e..df9d7a7 100644 --- a/tests/Support/SimpleMessageHandler.php +++ b/tests/Support/SimpleMessageHandler.php @@ -8,7 +8,7 @@ final class SimpleMessageHandler { - public function __construct(private FileHelper $fileHelper) + public function __construct(private readonly FileHelper $fileHelper) { } diff --git a/tests/Unit/QueueFactoryTest.php b/tests/Unit/QueueFactoryTest.php deleted file mode 100644 index 6b2f267..0000000 --- a/tests/Unit/QueueFactoryTest.php +++ /dev/null @@ -1,71 +0,0 @@ -getQueue(); - $container = $this->getContainer(); - $factory = new QueueFactory( - [], - $queue, - $container, - new CallableFactory($container), - new Injector($container), - true, - $this->getAdapter() - ); - - self::assertEquals('yii-queue', $factory->get('yii-queue')->getChannelName()); - } - - public function testDifferentChannel(): void - { - $this->queueName = 'yii-queue-channel2'; - $fileHelper = new FileHelper(); - - $queueProvider = new QueueProvider( - $this->createConnection(), - $this->getQueueSettings() - ); - - $this->queueProvider = $queueProvider->withExchangeSettings(null); - - $container = $this->getContainer(); - $adapter = $this->getAdapter(); - - $factory = new QueueFactory( - [ - 'channel1' => $adapter, - 'channel2' => $adapter->withChannel($this->queueName), - ], - $this->getQueue(), - $container, - new CallableFactory($container), - new Injector($container) - ); - - $time = time(); - $queue = $factory->get('channel2'); - $queue->push(new Message('ext-simple', ['file_name' => 'test-channel-run', 'payload' => ['time' => $time]])); - - self::assertNull($fileHelper->get('test-channel-run')); - - $queue->run(); - - $result = $fileHelper->get('test-channel-run'); - self::assertNotNull($result); - self::assertEquals($time, $result); - } -} diff --git a/tests/Unit/QueueProviderTest.php b/tests/Unit/QueueProviderTest.php index 897f257..700fa30 100644 --- a/tests/Unit/QueueProviderTest.php +++ b/tests/Unit/QueueProviderTest.php @@ -60,7 +60,7 @@ public function testWithQueueAndExchangeSettings(): void self::assertNotNull($result); self::assertEquals($time, $result); - $messageBody = json_decode($message->body, true, 512, JSON_THROW_ON_ERROR); + $messageBody = json_decode($message->getBody(), true, 512, JSON_THROW_ON_ERROR); self::assertEquals($messageBody['data']['payload']['time'], $result); } diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index 84f1619..5b05269 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -17,6 +17,7 @@ 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\Message; use Yiisoft\Queue\Queue; @@ -41,7 +42,7 @@ public function testStatus(): void $this->expectException(NotImplementedException::class); $this->expectExceptionMessage("Status check is not supported by the adapter $adapterClass."); - $adapter->status($message->getId()); + $adapter->status(IdEnvelope::fromMessage($message)->getId() ?? ''); } /** From 17f66ad3c273a448798a689fb9c4fb02055e236a Mon Sep 17 00:00:00 2001 From: viktorprogger Date: Mon, 13 Jan 2025 01:06:48 +0500 Subject: [PATCH 2/4] Bugfixes --- config/di.php | 3 - src/Adapter.php | 1 + src/ExistingMessagesConsumer.php | 3 +- src/MessageSerializer.php | 66 ----------- src/MessageSerializerInterface.php | 14 --- tests/Integration/DelayMiddlewareTest.php | 5 +- tests/Integration/TestCase.php | 5 + tests/Support/FakeAdapter.php | 2 +- tests/Unit/ExchangeSettingsTest.php | 3 +- tests/Unit/MessageSerializerTest.php | 134 ---------------------- tests/Unit/QueueProviderTest.php | 5 +- tests/Unit/QueueSettingsTest.php | 5 +- tests/Unit/QueueTest.php | 8 +- tests/Unit/UnitTestCase.php | 3 +- tests/yii | 19 ++- 15 files changed, 34 insertions(+), 242 deletions(-) delete mode 100644 src/MessageSerializer.php delete mode 100644 src/MessageSerializerInterface.php delete mode 100644 tests/Unit/MessageSerializerTest.php diff --git a/config/di.php b/config/di.php index d7a7eb3..9ab313d 100644 --- a/config/di.php +++ b/config/di.php @@ -2,15 +2,12 @@ declare(strict_types=1); -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\Queue; use Yiisoft\Queue\AMQP\Settings\QueueSettingsInterface; return [ - MessageSerializerInterface::class => MessageSerializer::class, QueueProviderInterface::class => QueueProvider::class, QueueSettingsInterface::class => Queue::class, ]; diff --git a/src/Adapter.php b/src/Adapter.php index 4c6e17f..6a9408c 100644 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -13,6 +13,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/ExistingMessagesConsumer.php b/src/ExistingMessagesConsumer.php index 4fbf4d2..72c2467 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 as MessageSerializerInterfaceAlias; /** * @internal @@ -19,7 +20,7 @@ final class ExistingMessagesConsumer public function __construct( private readonly AMQPChannel $channel, private readonly string $queueName, - private readonly MessageSerializerInterface $serializer + private readonly MessageSerializerInterfaceAlias $serializer ) { } diff --git a/src/MessageSerializer.php b/src/MessageSerializer.php deleted file mode 100644 index 0435312..0000000 --- a/src/MessageSerializer.php +++ /dev/null @@ -1,66 +0,0 @@ - IdEnvelope::fromMessage($message)->getId(), - '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): 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) . '.'); - } - - $name = $payload['name'] ?? null; - if (!is_string($name)) { - throw new NoKeyInPayloadException('name', $payload); - } - - $id = $payload['id'] ?? null; - if ($id !== null && !is_string($id) && !is_int($id)) { - throw new NoKeyInPayloadException('id', $payload); - } - - $meta = $payload['meta'] ?? []; - if (!is_array($meta)) { - throw new NoKeyInPayloadException('meta', $payload); - } - - $message = new Message( - $name, - $payload['data'] ?? null, - $meta, - ); - - return $id === null ? $message : new IdEnvelope($message, $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 @@ -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); diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index d1028a0..7bd0e18 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -48,5 +48,10 @@ protected function queueListen(?string $queue = null): void $process = new Process($command); $this->processes[] = $process; $process->start(); + + usleep(500000); + if (!$process->isRunning()) { + throw new \Exception('Failed to start queue listener process'); + } } } diff --git a/tests/Support/FakeAdapter.php b/tests/Support/FakeAdapter.php index 614d923..b094ed0 100644 --- a/tests/Support/FakeAdapter.php +++ b/tests/Support/FakeAdapter.php @@ -6,11 +6,11 @@ use BackedEnum; 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 { diff --git a/tests/Unit/ExchangeSettingsTest.php b/tests/Unit/ExchangeSettingsTest.php index 5d16634..6eaae55 100644 --- a/tests/Unit/ExchangeSettingsTest.php +++ b/tests/Unit/ExchangeSettingsTest.php @@ -11,6 +11,7 @@ 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/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 700fa30..d429c9f 100644 --- a/tests/Unit/QueueProviderTest.php +++ b/tests/Unit/QueueProviderTest.php @@ -13,6 +13,7 @@ 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..175ca50 100644 --- a/tests/Unit/QueueSettingsTest.php +++ b/tests/Unit/QueueSettingsTest.php @@ -11,6 +11,7 @@ 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(), ); @@ -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 5b05269..55bb345 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; @@ -18,7 +16,9 @@ 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 @@ -85,7 +85,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); @@ -113,7 +113,7 @@ public function testListen(): void $adapter = new Adapter( $queueProvider ->withChannelName('yii-queue'), - new MessageSerializer(), + new JsonMessageSerializer(), $mockLoop, ); $queue = $this->getDefaultQueue($adapter); diff --git a/tests/Unit/UnitTestCase.php b/tests/Unit/UnitTestCase.php index 2f119c5..7d89782 100644 --- a/tests/Unit/UnitTestCase.php +++ b/tests/Unit/UnitTestCase.php @@ -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..96b0679 100755 --- a/tests/yii +++ b/tests/yii @@ -3,11 +3,9 @@ use PhpAmqpLib\Connection\AMQPStreamConnection; use Psr\Log\NullLogger; +use Symfony\Component\Console\Application; use Yiisoft\Injector\Injector; -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; @@ -15,6 +13,7 @@ use Yiisoft\Queue\AMQP\Tests\Support\SimpleMessageHandler; use Yiisoft\Queue\Cli\SignalLoop; use Yiisoft\Queue\Command\ListenCommand; use Yiisoft\Queue\Command\RunCommand; +use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Queue\Middleware\CallableFactory; use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher; use Yiisoft\Queue\Middleware\Consume\MiddlewareFactoryConsume; @@ -22,8 +21,9 @@ use Yiisoft\Queue\Middleware\FailureHandling\FailureMiddlewareDispatcher; use Yiisoft\Queue\Middleware\FailureHandling\MiddlewareFactoryFailure; use Yiisoft\Queue\Middleware\Push\MiddlewareFactoryPush; use Yiisoft\Queue\Middleware\Push\PushMiddlewareDispatcher; +use Yiisoft\Queue\Provider\PrototypeQueueProvider; use Yiisoft\Queue\Queue; -use Yiisoft\Queue\QueueFactory; +use Yiisoft\Test\Support\Container\SimpleContainer; require_once dirname(__DIR__) . '/vendor/autoload.php'; @@ -53,7 +53,7 @@ $adapter = new Adapter( ), new QueueSettings(), ), - new MessageSerializer(), + new JsonMessageSerializer(), $loop, ); $queue = new Queue( @@ -63,16 +63,13 @@ $queue = new Queue( $pushMiddlewareDispatcher, $adapter, ); -$queueFactory = new QueueFactory( - [], +$queueFactory = new PrototypeQueueProvider( $queue, - $container, - $callableFactory, - $injector, + $adapter, ); $application = new Application(); $application->add(new ListenCommand($queueFactory)); -$application->add(new RunCommand($queueFactory)); +$application->add(new RunCommand($queueFactory, [])); $application->run(); From 963142d27132da8bd139c54ab69c68552a8dd9e2 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 12 Jan 2025 20:07:13 +0000 Subject: [PATCH 3/4] Apply fixes from StyleCI --- tests/Integration/DelayMiddlewareTest.php | 1 - tests/Unit/ExchangeSettingsTest.php | 1 - tests/Unit/QueueProviderTest.php | 1 - tests/Unit/QueueSettingsTest.php | 1 - tests/Unit/UnitTestCase.php | 1 - 5 files changed, 5 deletions(-) diff --git a/tests/Integration/DelayMiddlewareTest.php b/tests/Integration/DelayMiddlewareTest.php index 653a80e..ed9168f 100644 --- a/tests/Integration/DelayMiddlewareTest.php +++ b/tests/Integration/DelayMiddlewareTest.php @@ -9,7 +9,6 @@ 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; diff --git a/tests/Unit/ExchangeSettingsTest.php b/tests/Unit/ExchangeSettingsTest.php index 6eaae55..2cb6f23 100644 --- a/tests/Unit/ExchangeSettingsTest.php +++ b/tests/Unit/ExchangeSettingsTest.php @@ -7,7 +7,6 @@ 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; diff --git a/tests/Unit/QueueProviderTest.php b/tests/Unit/QueueProviderTest.php index d429c9f..ef01c82 100644 --- a/tests/Unit/QueueProviderTest.php +++ b/tests/Unit/QueueProviderTest.php @@ -6,7 +6,6 @@ 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; diff --git a/tests/Unit/QueueSettingsTest.php b/tests/Unit/QueueSettingsTest.php index 175ca50..c9acb93 100644 --- a/tests/Unit/QueueSettingsTest.php +++ b/tests/Unit/QueueSettingsTest.php @@ -7,7 +7,6 @@ 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; diff --git a/tests/Unit/UnitTestCase.php b/tests/Unit/UnitTestCase.php index 7d89782..9bf6cd4 100644 --- a/tests/Unit/UnitTestCase.php +++ b/tests/Unit/UnitTestCase.php @@ -10,7 +10,6 @@ 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; From 6aa0bd19ec0d2caa13b39fed475a8f1d8b620f5e Mon Sep 17 00:00:00 2001 From: viktorprogger Date: Mon, 13 Jan 2025 01:10:28 +0500 Subject: [PATCH 4/4] Bugfixes --- src/Exception/NoKeyInPayloadException.php | 52 ----------------------- tests/Unit/FriendlyExceptionTest.php | 12 ------ 2 files changed, 64 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 27f91f2..0000000 --- a/src/Exception/NoKeyInPayloadException.php +++ /dev/null @@ -1,52 +0,0 @@ -expectedKey . '" in payload'; - } - - /** - * @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/tests/Unit/FriendlyExceptionTest.php b/tests/Unit/FriendlyExceptionTest.php index 6e14fab..1257be4 100644 --- a/tests/Unit/FriendlyExceptionTest.php +++ b/tests/Unit/FriendlyExceptionTest.php @@ -5,21 +5,9 @@ namespace Yiisoft\Queue\AMQP\Tests\Unit; use Yiisoft\Queue\AMQP\Exception\ExchangeDeclaredException; -use Yiisoft\Queue\AMQP\Exception\NoKeyInPayloadException; final class FriendlyExceptionTest extends UnitTestCase { - public function testNoKeyInPayloadException(): void - { - $exception = new NoKeyInPayloadException( - 'test', - ['item1', 'item2'] - ); - - self::assertSame('No key "test" in payload', $exception->getName()); - $this->assertMatchesRegularExpression('/test/', $exception->getSolution()); - } - public function testExchangeDeclaredException(): void { $exception = new ExchangeDeclaredException();