1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
namespace Codeception\Module;
use Codeception\Exception\ModuleException as ModuleException;
use Codeception\Lib\Interfaces\RequiresPackage;
use Codeception\Module as CodeceptionModule;
use Codeception\TestInterface;
use Exception;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exception\AMQPProtocolChannelException;
use PhpAmqpLib\Message\AMQPMessage;
/**
* This module interacts with message broker software that implements
* the Advanced Message Queuing Protocol (AMQP) standard. For example, RabbitMQ (tested).
*
* <div class="alert alert-info">
* To use this module with Composer you need <em>"php-amqplib/php-amqplib": "~2.4"</em> package.
* </div>
*
* ## 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
*/
class AMQP extends CodeceptionModule implements RequiresPackage
{
protected $config = [
'host' => 'localhost',
'username' => 'guest',
'password' => 'guest',
'port' => '5672',
'vhost' => '/',
'cleanup' => true,
'single_channel' => false
];
/**
* @var AMQPStreamConnection
*/
public $connection;
/**
* @var int
*/
protected $channelId;
protected $requiredFields = ['host', 'username', 'password', 'vhost'];
public function _requires()
{
return ['PhpAmqpLib\Connection\AMQPStreamConnection' => '"php-amqplib/php-amqplib": "~2.4"'];
}
public function _initialize()
{
$host = $this->config['host'];
$port = $this->config['port'];
$username = $this->config['username'];
$password = $this->config['password'];
$vhost = $this->config['vhost'];
try {
$this->connection = new AMQPStreamConnection($host, $port, $username, $password, $vhost);
} catch (Exception $e) {
throw new ModuleException(__CLASS__, $e->getMessage() . ' while establishing connection to MQ server');
}
}
public function _before(TestInterface $test)
{
if ($this->config['cleanup']) {
$this->cleanup();
}
}
/**
* Sends message to exchange by sending exchange name, message
* and (optionally) a routing key
*
* ``` php
* <?php
* $I->pushToExchange('exchange.emails', 'thanks');
* $I->pushToExchange('exchange.emails', new AMQPMessage('Thanks!'));
* $I->pushToExchange('exchange.emails', new AMQPMessage('Thanks!'), 'severity');
* ?>
* ```
*
* @param string $exchange
* @param string|\PhpAmqpLib\Message\AMQPMessage $message
* @param string $routing_key
*/
public function pushToExchange($exchange, $message, $routing_key = null)
{
$message = $message instanceof AMQPMessage
? $message
: new AMQPMessage($message);
$this->getChannel()->basic_publish($message, $exchange, $routing_key);
}
/**
* Sends message to queue
*
* ``` php
* <?php
* $I->pushToQueue('queue.jobs', 'create user');
* $I->pushToQueue('queue.jobs', new AMQPMessage('create'));
* ?>
* ```
*
* @param string $queue
* @param string|\PhpAmqpLib\Message\AMQPMessage $message
*/
public function pushToQueue($queue, $message)
{
$message = $message instanceof AMQPMessage
? $message
: new AMQPMessage($message);
$this->getChannel()->queue_declare($queue);
$this->getChannel()->basic_publish($message, '', $queue);
}
/**
* Declares an exchange
*
* This is an alias of method `exchange_declare` of `PhpAmqpLib\Channel\AMQPChannel`.
*
* ```php
* <?php
* $I->declareExchange(
* 'nameOfMyExchange', // exchange name
* 'topic' // exchange type
* )
* ```
*
* @param string $exchange
* @param string $type
* @param bool $passive
* @param bool $durable
* @param bool $auto_delete
* @param bool $internal
* @param bool $nowait
* @param array $arguments
* @param int $ticket
* @return mixed|null
*/
public function declareExchange(
$exchange,
$type,
$passive = false,
$durable = false,
$auto_delete = true,
$internal = false,
$nowait = false,
$arguments = null,
$ticket = null
) {
return $this->getChannel()->exchange_declare(
$exchange,
$type,
$passive,
$durable,
$auto_delete,
$internal,
$nowait,
$arguments,
$ticket
);
}
/**
* Declares queue, creates if needed
*
* This is an alias of method `queue_declare` of `PhpAmqpLib\Channel\AMQPChannel`.
*
* ```php
* <?php
* $I->declareQueue(
* 'nameOfMyQueue', // exchange name
* )
* ```
*
* @param string $queue
* @param bool $passive
* @param bool $durable
* @param bool $exclusive
* @param bool $auto_delete
* @param bool $nowait
* @param array $arguments
* @param int $ticket
* @return mixed|null
*/
public function declareQueue(
$queue = '',
$passive = false,
$durable = false,
$exclusive = false,
$auto_delete = true,
$nowait = false,
$arguments = null,
$ticket = null
) {
return $this->getChannel()->queue_declare(
$queue,
$passive,
$durable,
$exclusive,
$auto_delete,
$nowait,
$arguments,
$ticket
);
}
/**
* Binds a queue to an exchange
*
* This is an alias of method `queue_bind` of `PhpAmqpLib\Channel\AMQPChannel`.
*
* ```php
* <?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 $queue
* @param string $exchange
* @param string $routing_key
* @param bool $nowait
* @param array $arguments
* @param int $ticket
* @return mixed|null
*/
public function bindQueueToExchange(
$queue,
$exchange,
$routing_key = '',
$nowait = false,
$arguments = null,
$ticket = null
) {
return $this->getChannel()->queue_bind(
$queue,
$exchange,
$routing_key,
$nowait,
$arguments,
$ticket
);
}
/**
* 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
* <?php
* $I->pushToQueue('queue.emails', 'Hello, davert');
* $I->seeMessageInQueueContainsText('queue.emails','davert');
* ?>
* ```
*
* @param string $queue
* @param string $text
*/
public function seeMessageInQueueContainsText($queue, $text)
{
$msg = $this->getChannel()->basic_get($queue);
if (!$msg) {
$this->fail("Message was not received");
}
if (!$msg instanceof AMQPMessage) {
$this->fail("Received message is not format of AMQPMessage");
}
$this->debugSection("Message", $msg->body);
$this->assertContains($text, $msg->body);
}
/**
* Takes last message from queue.
*
* ``` php
* <?php
* $message = $I->grabMessageFromQueue('queue.emails');
* ?>
* ```
*
* @param string $queue
* @return \PhpAmqpLib\Message\AMQPMessage
*/
public function grabMessageFromQueue($queue)
{
$message = $this->getChannel()->basic_get($queue);
return $message;
}
/**
* Purge a specific queue defined in config.
*
* ``` php
* <?php
* $I->purgeQueue('queue.emails');
* ?>
* ```
*
* @param string $queueName
*/
public function purgeQueue($queueName = '')
{
if (! in_array($queueName, $this->config['queues'])) {
throw new ModuleException(__CLASS__, "'$queueName' doesn't exist in queues config list");
}
$this->getChannel()->queue_purge($queueName, true);
}
/**
* Purge all queues defined in config.
*
* ``` php
* <?php
* $I->purgeAllQueues();
* ?>
* ```
*/
public function purgeAllQueues()
{
$this->cleanup();
}
/**
* @return \PhpAmqpLib\Channel\AMQPChannel
*/
protected function getChannel()
{
if ($this->config['single_channel'] && $this->channelId === null) {
$this->channelId = $this->connection->get_free_channel_id();
}
return $this->connection->channel($this->channelId);
}
protected function cleanup()
{
if (!isset($this->config['queues'])) {
throw new ModuleException(__CLASS__, "please set queues for cleanup");
}
if (!$this->connection) {
return;
}
foreach ($this->config['queues'] as $queue) {
try {
$this->getChannel()->queue_purge($queue);
} catch (AMQPProtocolChannelException $e) {
// ignore if exchange/queue doesn't exist and rethrow exception if it's something else
if ($e->getCode() !== 404) {
throw $e;
}
}
}
}
}