On this page
AMQP
Installation
If you use Codeception installed using composer, install this module with the following command:
composer require --dev codeception/module-amqp
Alternatively, you can enable AMQP
module in suite configuration file and run
codecept init upgrade4
This module was bundled with Codeception 2 and 3, but since version 4 it is necessary to install it separately.
Some modules are bundled with PHAR files.
Warning. Using PHAR file and composer in the same project can cause unexpected errors.
Description
This module interacts with message broker software that implements the Advanced Message Queuing Protocol (AMQP) standard. For example, RabbitMQ (tested).
Config
- host: localhost - host to connect
- username: guest - username to connect
- password: guest - password to connect
- vhost: ‘/’ - vhost to connect
- cleanup: true - defined queues will be purged before running every test.
- queues: [mail, twitter] - queues to cleanup
- single_channel - create and use only one channel during test execution
Example
modules: enabled: - AMQP: host: 'localhost' port: '5672' username: 'guest' password: 'guest' vhost: '/' queues: [queue1, queue2] single_channel: false
Public Properties
- connection - AMQPStreamConnection - current connection
Actions
bindQueueToExchange
Binds a queue to an exchange
This is an alias of method queue_bind
of PhpAmqpLib\Channel\AMQPChannel
.
<?php
$I->bindQueueToExchange(
'nameOfMyQueueToBind', // name of the queue
'transactionTracking.transaction', // exchange name to bind to
'your.routing.key' // Optionally, provide a binding key
)
param string
$queueparam string
$exchangeparam string
$routing_keyparam bool
$nowaitparam array
$argumentsparam int
$ticketreturn mixed|null
declareExchange
Declares an exchange
This is an alias of method exchange_declare
of PhpAmqpLib\Channel\AMQPChannel
.
<?php
$I->declareExchange(
'nameOfMyExchange', // exchange name
'topic' // exchange type
)
param string
$exchangeparam string
$typeparam bool
$passiveparam bool
$durableparam bool
$auto_deleteparam bool
$internalparam bool
$nowaitparam array
$argumentsparam int
$ticketreturn mixed|null
declareQueue
Declares queue, creates if needed
This is an alias of method queue_declare
of PhpAmqpLib\Channel\AMQPChannel
.
<?php
$I->declareQueue(
'nameOfMyQueue', // exchange name
)
param string
$queueparam bool
$passiveparam bool
$durableparam bool
$exclusiveparam bool
$auto_deleteparam bool
$nowaitparam array
$argumentsparam int
$ticketreturn mixed|null
dontSeeQueueIsEmpty
Checks if queue is not empty.
<?php
$I->pushToQueue('queue.emails', 'Hello, davert');
$I->dontSeeQueueIsEmpty('queue.emails');
?>
param string
$queue
grabMessageFromQueue
Takes last message from queue.
<?php
$message = $I->grabMessageFromQueue('queue.emails');
?>
param string
$queuereturn \PhpAmqpLib\Message\AMQPMessage
purgeAllQueues
Purge all queues defined in config.
<?php
$I->purgeAllQueues();
?>
purgeQueue
Purge a specific queue defined in config.
<?php
$I->purgeQueue('queue.emails');
?>
param string
$queueName
pushToExchange
Sends message to exchange by sending exchange name, message and (optionally) a routing key
<?php
$I->pushToExchange('exchange.emails', 'thanks');
$I->pushToExchange('exchange.emails', new AMQPMessage('Thanks!'));
$I->pushToExchange('exchange.emails', new AMQPMessage('Thanks!'), 'severity');
?>
param string
$exchangeparam string|\PhpAmqpLib\Message\AMQPMessage
$messageparam string
$routing_key
pushToQueue
Sends message to queue
<?php
$I->pushToQueue('queue.jobs', 'create user');
$I->pushToQueue('queue.jobs', new AMQPMessage('create'));
?>
param string
$queueparam string|\PhpAmqpLib\Message\AMQPMessage
$message
scheduleQueueCleanup
Add a queue to purge list
param string
$queue
seeMessageInQueueContainsText
Checks if message containing text received.
This method drops message from queue This method will wait for message. If none is sent the script will stuck.
<?php
$I->pushToQueue('queue.emails', 'Hello, davert');
$I->seeMessageInQueueContainsText('queue.emails','davert');
?>
param string
$queueparam string
$text
seeNumberOfMessagesInQueue
Checks that queue have expected number of message
<?php
$I->pushToQueue('queue.emails', 'Hello, davert');
$I->seeNumberOfMessagesInQueue('queue.emails',1);
?>
param string
$queueparam int
$expected
seeQueueIsEmpty
Checks that queue is empty
<?php
$I->pushToQueue('queue.emails', 'Hello, davert');
$I->purgeQueue('queue.emails');
$I->seeQueueIsEmpty('queue.emails');
?>
param string
$queueparam int
$expected
© 2011 Michael Bodnarchuk and contributors
Licensed under the MIT License.
https://codeception.com/docs/modules/AMQP