Skip to content

Commit

Permalink
Update codebase to PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
TavoNiievez committed Nov 1, 2021
1 parent b454378 commit 452e31b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
php: [7.1, 7.2, 7.3, 7.4, 8.0]
php: [7.4, 8.0, 8.1]

steps:
- name: Checkout code
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
],
"minimum-stability": "RC",
"require": {
"php": "^7.1 || ^8.0",
"php": "^7.4 | ^8.0",
"codeception/codeception": "^4.0",
"php-amqplib/php-amqplib": "^2.10|^3.0"
"php-amqplib/php-amqplib": "^2.10 | ^3.0"
},
"autoload": {
"classmap": [
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ An AMQP module for Codeception.

## Requirements

* `PHP 7.1` or higher.
* `PHP 7.4` or higher.

## Installation

Expand Down
43 changes: 21 additions & 22 deletions src/Codeception/Module/AMQP.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,9 @@ class AMQP extends Module implements RequiresPackage
'queues' => []
];

/**
* @var AMQPStreamConnection
*/
public $connection;
public ?AMQPStreamConnection $connection = null;

/**
* @var int
*/
protected $channelId;
protected ?int $channelId = null;

/**
* @var string[]
Expand All @@ -82,7 +76,7 @@ class AMQP extends Module implements RequiresPackage

public function _requires()
{
return [\PhpAmqpLib\Connection\AMQPStreamConnection::class => '"php-amqplib/php-amqplib": "~2.4"'];
return [AMQPStreamConnection::class => '"php-amqplib/php-amqplib": "~2.4"'];
}

public function _initialize()
Expand All @@ -95,8 +89,8 @@ public function _initialize()

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');
} catch (Exception $exception) {
throw new ModuleException(__CLASS__, $exception->getMessage() . ' while establishing connection to MQ server');
}
}

Expand Down Expand Up @@ -162,7 +156,7 @@ public function pushToQueue(string $queue, $message): void
* )
* ```
*
* @return mixed|null
* @return mixed
*/
public function declareExchange(
string $exchange,
Expand Down Expand Up @@ -200,7 +194,7 @@ public function declareExchange(
* )
* ```
*
* @return mixed|null
* @return mixed
*/
public function declareQueue(
string $queue = '',
Expand Down Expand Up @@ -238,7 +232,7 @@ public function declareQueue(
* )
* ```
*
* @return mixed|null
* @return mixed
*/
public function bindQueueToExchange(
string $queue,
Expand Down Expand Up @@ -283,12 +277,14 @@ public function scheduleQueueCleanup(string $queue): void
public function seeMessageInQueueContainsText(string $queue, string $text): void
{
$msg = $this->getChannel()->basic_get($queue);
if ($msg === null) {
if (!$msg instanceof AMQPMessage) {
$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->assertStringContainsString($text, $msg->body);

Expand Down Expand Up @@ -371,13 +367,13 @@ public function grabMessageFromQueue(string $queue): ?AMQPMessage
* $I->purgeQueue('queue.emails');
* ```
*/
public function purgeQueue(string $queue = ''): void
public function purgeQueue(string $queueName = ''): void
{
if (! in_array($queue, $this->config['queues'])) {
throw new ModuleException(__CLASS__, "'{$queue}' doesn't exist in queues config list");
if (! in_array($queueName, $this->config['queues'])) {
throw new ModuleException(__CLASS__, "'{$queueName}' doesn't exist in queues config list");
}

$this->getChannel()->queue_purge($queue, true);
$this->getChannel()->queue_purge($queueName, true);
}

/**
Expand All @@ -398,6 +394,7 @@ protected function getChannel(): AMQPChannel
if ($this->config['single_channel'] && $this->channelId === null) {
$this->channelId = $this->connection->get_free_channel_id();
}

return $this->connection->channel($this->channelId);
}

Expand All @@ -406,16 +403,18 @@ protected function cleanup(): void
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) {
} catch (AMQPProtocolChannelException $exception) {
// ignore if exchange/queue doesn't exist and rethrow exception if it's something else
if ($e->getCode() !== 404) {
throw $e;
if ($exception->getCode() !== 404) {
throw $exception;
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions tests/unit/Codeception/Module/AMQPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
declare(strict_types=1);

use Codeception\Lib\ModuleContainer;
use Codeception\Module\AMQP;
use Codeception\PHPUnit\TestCase;
use Codeception\Util\Stub;

final class AMQPTest extends TestCase
{
/**
* @var array
*/
protected $config = [
protected array $config = [
'host' => 'localhost',
'username' => 'guest',
'password' => 'guest',
Expand All @@ -21,17 +19,14 @@ final class AMQPTest extends TestCase
'queues' => ['queue1']
];

/**
* @var \Codeception\Module\AMQP
*/
protected $module;
protected AMQP $module;

public function _setUp()
{
$container = Stub::make(ModuleContainer::class);
$this->module = new \Codeception\Module\AMQP($container);
$this->module = new AMQP($container);
$this->module->_setConfig($this->config);

$res = @stream_socket_client('tcp://localhost:5672');
if ($res === false) {
$this->markTestSkipped('AMQP is not running');
Expand Down

0 comments on commit 452e31b

Please sign in to comment.