Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -26,5 +26,8 @@
},
"config": {
"classmap-authoritative": true
},
"require-dev": {
"codeception/stub": "^3.7"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Redis module for Codeception

## Requirements

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

## Installation

Expand Down
26 changes: 14 additions & 12 deletions src/Codeception/Module/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"'];
}
Expand All @@ -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()
);
}
}
Expand Down Expand Up @@ -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':
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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':
Expand All @@ -591,6 +592,7 @@ private function checkKeyContains(string $key, $item, $itemValue = null): bool
} else {
$result = true;
}

break;

case 'hash':
Expand Down
26 changes: 9 additions & 17 deletions tests/unit/Codeception/Module/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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);

Expand All @@ -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 = [
Expand All @@ -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);
Expand Down Expand Up @@ -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
{
Expand Down