Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion src/Codeception/Module/AMQP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <?php
* $I->pushToQueue('queue.jobs', 'create user');
Expand All @@ -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);
}

Expand Down Expand Up @@ -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'])) {
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/Codeception/Module/AMQPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public function _setUp()
}

$this->module->_initialize();
$connection = $this->module->connection;
$connection->channel()->queue_declare('queue1');
}

public function testPushToQueue()
Expand All @@ -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');
Expand Down