diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bf790a1..c4ab1de 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,13 +8,14 @@ jobs: services: rabbitmq: - image: rabbitmq:3 + image: rabbitmq:${{ matrix.rabbitmq }} ports: - 5672:5672 strategy: matrix: php: [8.2, 8.3, 8.4, 8.5, 8.6] + rabbitmq: [3, 4] steps: - name: Checkout code diff --git a/src/Codeception/Module/AMQP.php b/src/Codeception/Module/AMQP.php index f185d06..82bc7e6 100644 --- a/src/Codeception/Module/AMQP.php +++ b/src/Codeception/Module/AMQP.php @@ -128,6 +128,10 @@ public function pushToExchange(string $exchange, string|AMQPMessage $message, ?s /** * Sends message to queue * + * If the queue does not exist yet, it is created. On RabbitMQ 4.3+ it is created as a durable, + * non-auto-delete queue, because transient non-exclusive queues are not permitted by default anymore. + * On older versions it is created as a transient queue. + * * ``` php * pushToQueue('queue.jobs', 'create user'); @@ -140,7 +144,12 @@ public function pushToQueue(string $queue, string|AMQPMessage $message): void ? $message : new AMQPMessage($message); - $this->getChannel()->queue_declare($queue); + if ($this->isRabbitMq43OrNewer()) { + $this->declareQueueIfMissing($queue); + } else { + $this->getChannel()->queue_declare($queue); + } + $this->getChannel()->basic_publish($message, '', $queue); } @@ -399,6 +408,32 @@ protected function getChannel(): AMQPChannel return $this->connection->channel($this->channelId); } + private function isRabbitMq43OrNewer(): bool + { + $version = $this->connection->getServerProperties()['version'][1] ?? null; + if (!is_string($version)) { + return false; + } + + // Compare major and minor only, so pre-releases like "4.3.0-rc.1" are matched as well + [$major, $minor] = sscanf($version, '%d.%d'); + + return $major > 4 || ($major === 4 && $minor >= 3); + } + + private function declareQueueIfMissing(string $queue): void + { + try { + $this->getChannel()->queue_declare($queue, true); + } catch (AMQPProtocolChannelException $exception) { + if ($exception->getCode() !== 404) { + throw $exception; + } + + $this->getChannel()->queue_declare($queue, false, true, false, false); + } + } + protected function cleanup(): void { if (!isset($this->config['queues'])) { diff --git a/tests/unit/Codeception/Module/AMQPTest.php b/tests/unit/Codeception/Module/AMQPTest.php index 7d223dc..36d0d97 100644 --- a/tests/unit/Codeception/Module/AMQPTest.php +++ b/tests/unit/Codeception/Module/AMQPTest.php @@ -33,8 +33,6 @@ public function _setUp() } $this->module->_initialize(); - $connection = $this->module->connection; - $connection->channel()->queue_declare('queue1'); } public function testPushToQueue() @@ -44,6 +42,18 @@ public function testPushToQueue() $this->module->seeQueueIsEmpty('queue1'); } + public function testPushToNonExistingQueue() + { + $queue = 'test-non-existing-queue'; + $channel = $this->module->connection->channel(); + $channel->queue_delete($queue); + + $this->module->pushToQueue($queue, 'hello'); + $this->module->seeMessageInQueueContainsText($queue, 'hello'); + + $channel->queue_delete($queue); + } + public function testCountQueue() { $this->module->pushToQueue('queue1', 'hello');