From 452e31bd1c065b4b0277d8ef5dbb8b120b543d07 Mon Sep 17 00:00:00 2001 From: TavoNiievez Date: Sun, 31 Oct 2021 19:59:01 -0500 Subject: [PATCH] Update codebase to PHP 7.4 --- .github/workflows/main.yml | 2 +- composer.json | 4 +- readme.md | 2 +- src/Codeception/Module/AMQP.php | 43 +++++++++++----------- tests/unit/Codeception/Module/AMQPTest.php | 15 +++----- 5 files changed, 30 insertions(+), 36 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f0155b2..e09c7b6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/composer.json b/composer.json index 063c5c4..fdeb63c 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/readme.md b/readme.md index 0ee23f3..9b61d2b 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,7 @@ An AMQP module for Codeception. ## Requirements -* `PHP 7.1` or higher. +* `PHP 7.4` or higher. ## Installation diff --git a/src/Codeception/Module/AMQP.php b/src/Codeception/Module/AMQP.php index 7792647..1cf9ce1 100644 --- a/src/Codeception/Module/AMQP.php +++ b/src/Codeception/Module/AMQP.php @@ -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[] @@ -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() @@ -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'); } } @@ -162,7 +156,7 @@ public function pushToQueue(string $queue, $message): void * ) * ``` * - * @return mixed|null + * @return mixed */ public function declareExchange( string $exchange, @@ -200,7 +194,7 @@ public function declareExchange( * ) * ``` * - * @return mixed|null + * @return mixed */ public function declareQueue( string $queue = '', @@ -238,7 +232,7 @@ public function declareQueue( * ) * ``` * - * @return mixed|null + * @return mixed */ public function bindQueueToExchange( string $queue, @@ -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); @@ -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); } /** @@ -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); } @@ -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; } } } diff --git a/tests/unit/Codeception/Module/AMQPTest.php b/tests/unit/Codeception/Module/AMQPTest.php index fa31f9a..cd093a1 100644 --- a/tests/unit/Codeception/Module/AMQPTest.php +++ b/tests/unit/Codeception/Module/AMQPTest.php @@ -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', @@ -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');