From afaa7c2a083c00c186928aff38a1745e9f97c5a4 Mon Sep 17 00:00:00 2001 From: TavoNiievez Date: Wed, 3 Nov 2021 19:54:03 -0500 Subject: [PATCH] Update codebase to PHP 7.4 --- .github/workflows/main.yml | 2 +- composer.json | 5 +++- readme.md | 2 +- src/Codeception/Module/Redis.php | 26 +++++++++++---------- tests/unit/Codeception/Module/RedisTest.php | 26 +++++++-------------- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 62630a3..05e8325 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 0363e8d..4d73062 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ], "minimum-stability": "RC", "require": { - "php": "^7.1 || ^8.0", + "php": "^7.4 || ^8.0", "codeception/codeception": "^4.0", "predis/predis": "^1.0" }, @@ -26,5 +26,8 @@ }, "config": { "classmap-authoritative": true + }, + "require-dev": { + "codeception/stub": "^3.7" } } diff --git a/readme.md b/readme.md index 04e57f9..08f3049 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,7 @@ Redis module for Codeception ## Requirements -* `PHP 7.1` or higher. +* `PHP 7.4` or higher. ## Installation diff --git a/src/Codeception/Module/Redis.php b/src/Codeception/Module/Redis.php index 32b5669..7146e94 100644 --- a/src/Codeception/Module/Redis.php +++ b/src/Codeception/Module/Redis.php @@ -76,12 +76,10 @@ class Redis extends Module implements RequiresPackage /** * The Redis driver - * - * @var RedisDriver */ - public $driver; + public ?RedisDriver $driver = null; - public function _requires() + public function _requires(): array { return [\Predis\Client::class => '"predis/predis": "^1.0"']; } @@ -95,10 +93,10 @@ public function _initialize() { try { $this->driver = new RedisDriver($this->config); - } catch (Exception $e) { + } catch (Exception $exception) { throw new ModuleException( __CLASS__, - $e->getMessage() + $exception->getMessage() ); } } @@ -205,10 +203,11 @@ public function grabFromRedis(string $key) } else { $reply = $this->driver->lrange( $key, - isset($args[1]) ? $args[1] : 0, - isset($args[2]) ? $args[2] : -1 + $args[1] ?? 0, + $args[2] ?? -1 ); } + break; case 'set': @@ -222,10 +221,11 @@ public function grabFromRedis(string $key) 'The method grabFromRedis(), when used with sorted sets, expects either one argument or three' ); } + $reply = $this->driver->zrange( $key, isset($args[2]) ? $args[1] : 0, - isset($args[2]) ? $args[2] : -1, + $args[2] ?? -1, 'WITHSCORES' ); break; @@ -287,6 +287,7 @@ public function haveInRedis(string $type, string $key, $value): void 'If second argument of haveInRedis() method is "string", third argument must be a scalar' ); } + $this->driver->set($key, $value); break; @@ -305,6 +306,7 @@ public function haveInRedis(string $type, string $key, $value): void 'If second argument of haveInRedis() method is "zset", third argument must be an (associative) array' ); } + $this->driver->zadd($key, $value); break; @@ -315,6 +317,7 @@ public function haveInRedis(string $type, string $key, $value): void 'If second argument of haveInRedis() method is "hash", third argument must be an array' ); } + $this->driver->hmset($key, $value); break; @@ -467,7 +470,6 @@ public function seeInRedis(string $key, $value = null): void * ``` * * @param string $command The command name - * * @return mixed */ public function sendCommandToRedis(string $command) @@ -528,7 +530,6 @@ public function seeRedisKeyContains(string $key, $item, $itemValue = null): void * Converts boolean values to "0" and "1" * * @param mixed $var The variable - * * @return mixed */ private function boolToString($var) @@ -569,7 +570,7 @@ private function checkKeyContains(string $key, $item, $itemValue = null): bool switch ($this->driver->type($key)) { case 'string': $reply = $this->driver->get($key); - $result = strpos($reply, $item) !== false; + $result = strpos($reply, (string) $item) !== false; break; case 'list': @@ -591,6 +592,7 @@ private function checkKeyContains(string $key, $item, $itemValue = null): bool } else { $result = true; } + break; case 'hash': diff --git a/tests/unit/Codeception/Module/RedisTest.php b/tests/unit/Codeception/Module/RedisTest.php index 4753014..30f495d 100644 --- a/tests/unit/Codeception/Module/RedisTest.php +++ b/tests/unit/Codeception/Module/RedisTest.php @@ -6,28 +6,21 @@ use Codeception\Lib\ModuleContainer; use Codeception\Module\Redis; use Codeception\Test\Unit; -use Codeception\Util\Stub; +use Codeception\Stub; +use PHPUnit\Framework\AssertionFailedError; final class RedisTest extends Unit { - /** - * @var array - */ - protected static $config = [ + protected static array $config = [ 'database' => 15 ]; - /** - * @var Redis - */ - protected $module; + protected ?Redis $module = null; /** * Keys that will be created for the tests - * - * @var array */ - protected static $keys = [ + protected static array $keys = [ 'string' => [ 'name' => 'test:string', 'value' => 'hello' @@ -60,6 +53,7 @@ protected function _setUp() if (!class_exists(\Predis\Client::class)) { $this->markTestSkipped('Predis is not installed'); } + /** @var ModuleContainer $container */ $container = Stub::make(ModuleContainer::class); @@ -69,8 +63,8 @@ protected function _setUp() $this->module->_initialize(); $this->module->driver->flushDb(); - } catch (Predis\Connection\ConnectionException $e) { - $this->markTestSkipped($e->getMessage()); + } catch (Predis\Connection\ConnectionException $exception) { + $this->markTestSkipped($exception->getMessage()); } $addMethods = [ @@ -96,7 +90,7 @@ protected function _setUp() protected function shouldFail($exceptionClass = null) { if (!$exceptionClass) { - $exceptionClass = \PHPUnit\Framework\AssertionFailedError::class; + $exceptionClass = AssertionFailedError::class; } $this->expectException($exceptionClass); @@ -1354,8 +1348,6 @@ public function testSendCommandToRedis() * Explicitely cast the scores of a Zset associative array as float/double * * @param array $arr The ZSet associative array - * - * @return array */ private function scoresToFloat(array $arr): array {