Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bdf79c9

Browse files
authoredDec 2, 2021
Update codebase to PHP 7.4 (#12)
1 parent be39936 commit bdf79c9

File tree

5 files changed

+121
-158
lines changed

5 files changed

+121
-158
lines changed
 

‎.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
strategy:
1616
matrix:
17-
php: [5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0]
17+
php: [7.4, 8.0, 8.1]
1818

1919
steps:
2020
- name: Checkout code

‎composer.json

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
{
2-
"name":"codeception/module-amqp",
3-
"description":"AMQP module for Codeception",
4-
"keywords":["codeception", "amqp"],
5-
"homepage":"http://codeception.com/",
6-
"type":"library",
7-
"license":"MIT",
8-
"authors":[
9-
{
10-
"name":"Michael Bodnarchuk"
11-
},
12-
{
13-
"name":"Gintautas Miselis"
14-
}
15-
],
16-
"minimum-stability": "RC",
17-
"require": {
18-
"php": ">=5.6.0 <9.0",
19-
"codeception/codeception": "^4.0",
20-
"php-amqplib/php-amqplib": "^2.10|^3.0"
21-
},
22-
"autoload":{
23-
"classmap": ["src/"]
24-
},
25-
"config": {
26-
"classmap-authoritative": true
27-
}
2+
"name": "codeception/module-amqp",
3+
"description": "AMQP module for Codeception",
4+
"keywords": [ "codeception", "amqp" ],
5+
"homepage": "https://codeception.com/",
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Michael Bodnarchuk"
11+
},
12+
{
13+
"name": "Gintautas Miselis"
14+
}
15+
],
16+
"minimum-stability": "RC",
17+
"require": {
18+
"php": "^7.4 | ^8.0",
19+
"codeception/codeception": "^4.0",
20+
"php-amqplib/php-amqplib": "^2.10 | ^3.0"
21+
},
22+
"autoload": {
23+
"classmap": [
24+
"src/"
25+
]
26+
},
27+
"config": {
28+
"classmap-authoritative": true
29+
}
2830
}

‎readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ An AMQP module for Codeception.
77
[![Total Downloads](https://poser.pugx.org/codeception/module-amqp/downloads)](https://packagist.org/packages/codeception/module-amqp)
88
[![License](https://poser.pugx.org/codeception/module-amqp/license)](/LICENSE)
99

10+
## Requirements
11+
12+
* `PHP 7.4` or higher.
13+
1014
## Installation
1115

1216
```

‎src/Codeception/Module/AMQP.php

Lines changed: 74 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
namespace Codeception\Module;
36

47
use Codeception\Exception\ModuleException;
5-
use Codeception\Module as CodeceptionModule;
8+
use Codeception\Lib\Interfaces\RequiresPackage;
9+
use Codeception\Module;
610
use Codeception\TestInterface;
711
use Exception;
812
use PhpAmqpLib\Channel\AMQPChannel;
@@ -45,8 +49,11 @@
4549
*
4650
* * connection - AMQPStreamConnection - current connection
4751
*/
48-
class AMQP extends CodeceptionModule
52+
class AMQP extends Module implements RequiresPackage
4953
{
54+
/**
55+
* @var array
56+
*/
5057
protected $config = [
5158
'host' => 'localhost',
5259
'username' => 'guest',
@@ -58,18 +65,20 @@ class AMQP extends CodeceptionModule
5865
'queues' => []
5966
];
6067

61-
/**
62-
* @var AMQPStreamConnection
63-
*/
64-
public $connection;
68+
public ?AMQPStreamConnection $connection = null;
69+
70+
protected ?int $channelId = null;
6571

6672
/**
67-
* @var int
73+
* @var string[]
6874
*/
69-
protected $channelId;
70-
7175
protected $requiredFields = ['host', 'username', 'password', 'vhost'];
7276

77+
public function _requires()
78+
{
79+
return [AMQPStreamConnection::class => '"php-amqplib/php-amqplib": "~2.4"'];
80+
}
81+
7382
public function _initialize()
7483
{
7584
$host = $this->config['host'];
@@ -80,8 +89,8 @@ public function _initialize()
8089

8190
try {
8291
$this->connection = new AMQPStreamConnection($host, $port, $username, $password, $vhost);
83-
} catch (Exception $e) {
84-
throw new ModuleException(__CLASS__, $e->getMessage() . ' while establishing connection to MQ server');
92+
} catch (Exception $exception) {
93+
throw new ModuleException(__CLASS__, $exception->getMessage() . ' while establishing connection to MQ server');
8594
}
8695
}
8796

@@ -101,14 +110,11 @@ public function _before(TestInterface $test)
101110
* $I->pushToExchange('exchange.emails', 'thanks');
102111
* $I->pushToExchange('exchange.emails', new AMQPMessage('Thanks!'));
103112
* $I->pushToExchange('exchange.emails', new AMQPMessage('Thanks!'), 'severity');
104-
* ?>
105113
* ```
106114
*
107-
* @param string $exchange
108-
* @param string|\PhpAmqpLib\Message\AMQPMessage $message
109-
* @param string $routing_key
115+
* @param string|AMQPMessage $message
110116
*/
111-
public function pushToExchange($exchange, $message, $routing_key = null)
117+
public function pushToExchange(string $exchange, $message, string $routing_key = null): void
112118
{
113119
$message = $message instanceof AMQPMessage
114120
? $message
@@ -123,13 +129,11 @@ public function pushToExchange($exchange, $message, $routing_key = null)
123129
* <?php
124130
* $I->pushToQueue('queue.jobs', 'create user');
125131
* $I->pushToQueue('queue.jobs', new AMQPMessage('create'));
126-
* ?>
127132
* ```
128133
*
129-
* @param string $queue
130-
* @param string|\PhpAmqpLib\Message\AMQPMessage $message
134+
* @param string|AMQPMessage $message
131135
*/
132-
public function pushToQueue($queue, $message)
136+
public function pushToQueue(string $queue, $message): void
133137
{
134138
$message = $message instanceof AMQPMessage
135139
? $message
@@ -152,27 +156,18 @@ public function pushToQueue($queue, $message)
152156
* )
153157
* ```
154158
*
155-
* @param string $exchange
156-
* @param string $type
157-
* @param bool $passive
158-
* @param bool $durable
159-
* @param bool $auto_delete
160-
* @param bool $internal
161-
* @param bool $nowait
162-
* @param array $arguments
163-
* @param int $ticket
164-
* @return mixed|null
159+
* @return mixed
165160
*/
166161
public function declareExchange(
167-
$exchange,
168-
$type,
169-
$passive = false,
170-
$durable = false,
171-
$auto_delete = true,
172-
$internal = false,
173-
$nowait = false,
174-
$arguments = null,
175-
$ticket = null
162+
string $exchange,
163+
string $type,
164+
bool $passive = false,
165+
bool $durable = false,
166+
bool $auto_delete = true,
167+
bool $internal = false,
168+
bool $nowait = false,
169+
array $arguments = null,
170+
int $ticket = null
176171
) {
177172
return $this->getChannel()->exchange_declare(
178173
$exchange,
@@ -199,26 +194,18 @@ public function declareExchange(
199194
* )
200195
* ```
201196
*
202-
* @param string $queue
203-
* @param bool $passive
204-
* @param bool $durable
205-
* @param bool $exclusive
206-
* @param bool $auto_delete
207-
* @param bool $nowait
208-
* @param array $arguments
209-
* @param int $ticket
210-
* @return mixed|null
197+
* @return mixed
211198
*/
212199
public function declareQueue(
213-
$queue = '',
214-
$passive = false,
215-
$durable = false,
216-
$exclusive = false,
217-
$auto_delete = true,
218-
$nowait = false,
219-
$arguments = null,
220-
$ticket = null
221-
) {
200+
string $queue = '',
201+
bool $passive = false,
202+
bool $durable = false,
203+
bool $exclusive = false,
204+
bool $auto_delete = true,
205+
bool $nowait = false,
206+
array $arguments = null,
207+
int $ticket = null
208+
): ?array {
222209
return $this->getChannel()->queue_declare(
223210
$queue,
224211
$passive,
@@ -245,21 +232,15 @@ public function declareQueue(
245232
* )
246233
* ```
247234
*
248-
* @param string $queue
249-
* @param string $exchange
250-
* @param string $routing_key
251-
* @param bool $nowait
252-
* @param array $arguments
253-
* @param int $ticket
254-
* @return mixed|null
235+
* @return mixed
255236
*/
256237
public function bindQueueToExchange(
257-
$queue,
258-
$exchange,
259-
$routing_key = '',
260-
$nowait = false,
261-
$arguments = null,
262-
$ticket = null
238+
string $queue,
239+
string $exchange,
240+
string $routing_key = '',
241+
bool $nowait = false,
242+
array $arguments = null,
243+
int $ticket = null
263244
) {
264245
return $this->getChannel()->queue_bind(
265246
$queue,
@@ -273,10 +254,8 @@ public function bindQueueToExchange(
273254

274255
/**
275256
* Add a queue to purge list
276-
*
277-
* @param string $queue
278257
*/
279-
public function scheduleQueueCleanup($queue)
258+
public function scheduleQueueCleanup(string $queue): void
280259
{
281260
if (!in_array($queue, $this->config['queues'])) {
282261
$this->config['queues'][] = $queue;
@@ -293,21 +272,19 @@ public function scheduleQueueCleanup($queue)
293272
* <?php
294273
* $I->pushToQueue('queue.emails', 'Hello, davert');
295274
* $I->seeMessageInQueueContainsText('queue.emails','davert');
296-
* ?>
297275
* ```
298-
*
299-
* @param string $queue
300-
* @param string $text
301276
*/
302-
public function seeMessageInQueueContainsText($queue, $text)
277+
public function seeMessageInQueueContainsText(string $queue, string $text): void
303278
{
304279
$msg = $this->getChannel()->basic_get($queue);
305-
if (!$msg) {
280+
if (!$msg instanceof AMQPMessage) {
306281
$this->fail("Message was not received");
307282
}
283+
308284
if (!$msg instanceof AMQPMessage) {
309285
$this->fail("Received message is not format of AMQPMessage");
310286
}
287+
311288
$this->debugSection("Message", $msg->body);
312289
$this->assertStringContainsString($text, $msg->body);
313290

@@ -316,14 +293,10 @@ public function seeMessageInQueueContainsText($queue, $text)
316293

317294
/**
318295
* Count messages in queue.
319-
*
320-
* @param string $queue
321-
*
322-
* @return int
323296
*/
324-
public function _countMessage($queue)
297+
public function _countMessage(string $queue): int
325298
{
326-
list($queue, $messageCount) = $this->getChannel()->queue_declare($queue, true);
299+
[$queue, $messageCount] = $this->getChannel()->queue_declare($queue, true);
327300
return $messageCount;
328301
}
329302

@@ -334,13 +307,9 @@ public function _countMessage($queue)
334307
* <?php
335308
* $I->pushToQueue('queue.emails', 'Hello, davert');
336309
* $I->seeNumberOfMessagesInQueue('queue.emails',1);
337-
* ?>
338310
* ```
339-
*
340-
* @param string $queue
341-
* @param int $expected
342311
*/
343-
public function seeNumberOfMessagesInQueue($queue, $expected)
312+
public function seeNumberOfMessagesInQueue(string $queue, int $expected): void
344313
{
345314
$messageCount = $this->_countMessage($queue);
346315
$this->assertEquals($expected, $messageCount);
@@ -354,13 +323,9 @@ public function seeNumberOfMessagesInQueue($queue, $expected)
354323
* $I->pushToQueue('queue.emails', 'Hello, davert');
355324
* $I->purgeQueue('queue.emails');
356325
* $I->seeQueueIsEmpty('queue.emails');
357-
* ?>
358326
* ```
359-
*
360-
* @param string $queue
361-
* @param int $expected
362327
*/
363-
public function seeQueueIsEmpty($queue)
328+
public function seeQueueIsEmpty(string $queue): void
364329
{
365330
$messageCount = $this->_countMessage($queue);
366331
$this->assertEquals(0, $messageCount);
@@ -373,12 +338,9 @@ public function seeQueueIsEmpty($queue)
373338
* <?php
374339
* $I->pushToQueue('queue.emails', 'Hello, davert');
375340
* $I->dontSeeQueueIsEmpty('queue.emails');
376-
* ?>
377341
* ```
378-
*
379-
* @param string $queue
380342
*/
381-
public function dontSeeQueueIsEmpty($queue)
343+
public function dontSeeQueueIsEmpty(string $queue): void
382344
{
383345
$messageCount = $this->_countMessage($queue);
384346
$this->assertNotEquals(0, $messageCount);
@@ -390,16 +352,11 @@ public function dontSeeQueueIsEmpty($queue)
390352
* ``` php
391353
* <?php
392354
* $message = $I->grabMessageFromQueue('queue.emails');
393-
* ?>
394355
* ```
395-
*
396-
* @param string $queue
397-
* @return \PhpAmqpLib\Message\AMQPMessage
398356
*/
399-
public function grabMessageFromQueue($queue)
357+
public function grabMessageFromQueue(string $queue): ?AMQPMessage
400358
{
401-
$message = $this->getChannel()->basic_get($queue);
402-
return $message;
359+
return $this->getChannel()->basic_get($queue);
403360
}
404361

405362
/**
@@ -408,15 +365,12 @@ public function grabMessageFromQueue($queue)
408365
* ``` php
409366
* <?php
410367
* $I->purgeQueue('queue.emails');
411-
* ?>
412368
* ```
413-
*
414-
* @param string $queueName
415369
*/
416-
public function purgeQueue($queueName = '')
370+
public function purgeQueue(string $queueName = ''): void
417371
{
418372
if (! in_array($queueName, $this->config['queues'])) {
419-
throw new ModuleException(__CLASS__, "'$queueName' doesn't exist in queues config list");
373+
throw new ModuleException(__CLASS__, "'{$queueName}' doesn't exist in queues config list");
420374
}
421375

422376
$this->getChannel()->queue_purge($queueName, true);
@@ -428,40 +382,39 @@ public function purgeQueue($queueName = '')
428382
* ``` php
429383
* <?php
430384
* $I->purgeAllQueues();
431-
* ?>
432385
* ```
433386
*/
434-
public function purgeAllQueues()
387+
public function purgeAllQueues(): void
435388
{
436389
$this->cleanup();
437390
}
438391

439-
/**
440-
* @return \PhpAmqpLib\Channel\AMQPChannel
441-
*/
442-
protected function getChannel()
392+
protected function getChannel(): AMQPChannel
443393
{
444394
if ($this->config['single_channel'] && $this->channelId === null) {
445395
$this->channelId = $this->connection->get_free_channel_id();
446396
}
397+
447398
return $this->connection->channel($this->channelId);
448399
}
449400

450-
protected function cleanup()
401+
protected function cleanup(): void
451402
{
452403
if (!isset($this->config['queues'])) {
453404
throw new ModuleException(__CLASS__, "please set queues for cleanup");
454405
}
406+
455407
if (!$this->connection) {
456408
return;
457409
}
410+
458411
foreach ($this->config['queues'] as $queue) {
459412
try {
460413
$this->getChannel()->queue_purge($queue);
461-
} catch (AMQPProtocolChannelException $e) {
414+
} catch (AMQPProtocolChannelException $exception) {
462415
// ignore if exchange/queue doesn't exist and rethrow exception if it's something else
463-
if ($e->getCode() !== 404) {
464-
throw $e;
416+
if ($exception->getCode() !== 404) {
417+
throw $exception;
465418
}
466419
}
467420
}

‎tests/unit/Codeception/Module/AMQPTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
<?php
22

3+
declare(strict_types=1);
34

4-
class AMQPTest extends \Codeception\PHPUnit\TestCase
5+
use Codeception\Lib\ModuleContainer;
6+
use Codeception\Module\AMQP;
7+
use Codeception\PHPUnit\TestCase;
8+
use Codeception\Util\Stub;
9+
10+
final class AMQPTest extends TestCase
511
{
6-
protected $config = array(
12+
protected array $config = [
713
'host' => 'localhost',
814
'username' => 'guest',
915
'password' => 'guest',
1016
'port' => '5672',
1117
'vhost' => '/',
1218
'cleanup' => false,
13-
'queues' => array('queue1')
14-
);
19+
'queues' => ['queue1']
20+
];
1521

16-
/**
17-
* @var \Codeception\Module\AMQP
18-
*/
19-
protected $module = null;
22+
protected AMQP $module;
2023

2124
public function _setUp()
2225
{
23-
$container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
24-
$this->module = new \Codeception\Module\AMQP($container);
26+
$container = Stub::make(ModuleContainer::class);
27+
$this->module = new AMQP($container);
2528
$this->module->_setConfig($this->config);
29+
2630
$res = @stream_socket_client('tcp://localhost:5672');
2731
if ($res === false) {
2832
$this->markTestSkipped('AMQP is not running');

0 commit comments

Comments
 (0)
Please sign in to comment.