From 44dc3c5eb6e2c0ed6182f60215ed87bc3b1f401c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0ilgalis?= Date: Mon, 23 Nov 2020 14:16:54 +0200 Subject: [PATCH 1/5] Add tests --- tests/units/RedisMock.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 7c14a7d..92ec659 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -1247,12 +1247,22 @@ public function testHSetHMSetHGetHDelHExistsHKeysHLenHGetAll() ->isEqualTo(1) ->integer($redisMock->hset('test', 'test2', 'something else')) ->isEqualTo(1) + ->integer($redisMock->hset('test', 'test4', 'something else 4')) + ->isEqualTo(1) + ->integer($redisMock->hset('test', 'test5', 'something else 5')) + ->isEqualTo(1) + ->integer($redisMock->hset('test', 'test6', 'something else 6')) + ->isEqualTo(1) ->integer($redisMock->hdel('test', 'test2')) ->isEqualTo(1) ->integer($redisMock->hdel('test', 'test3')) ->isEqualTo(0) ->integer($redisMock->hdel('raoul', 'test2')) ->isEqualTo(0) + ->integer($redisMock->hdel('test', ['test4'])) + ->isEqualTo(1) + ->integer($redisMock->hdel('test', ['test5', 'test6'])) + ->isEqualTo(2) ->string($redisMock->type('test')) ->isEqualTo('hash') ->integer($redisMock->hdel('test', 'test1')) From e1ddc18d29d85dc9078587b5fff906d1ad105242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0ilgalis?= Date: Mon, 23 Nov 2020 14:28:31 +0200 Subject: [PATCH 2/5] Update hdel to support deleting multiple hash fields. --- README.md | 2 +- src/M6Web/Component/RedisMock/RedisMock.php | 25 ++++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d3288a1..e194d25 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Redis command | Description **EXPIRE** *key* *seconds* | Sets a key's time to live in seconds **FLUSHDB** | Flushes the database **GET** *key* | Gets the value of a key -**HDEL** *key* *field* | Delete one hash fields +**HDEL** *key* *array\* | Delete hash fields **HEXISTS** *key* *field* | Determines if a hash field exists **HMGET** *key* *array\* | Gets the values of multiple hash fields **HGET** *key* *field* | Gets the value of a hash field diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index c9790a3..92948ba 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -739,10 +739,10 @@ public function hmget($key, $fields) return $this->returnPipedInfo($result); } - public function hdel($key, $field) + public function hdel($key, $fields) { if (func_num_args() > 2) { - throw new UnsupportedException('In RedisMock, `hdel` command can not delete more than one entry at once.'); + throw new UnsupportedException('In RedisMock, `hdel` command does not accept more than two arguments.'); } if (isset(self::$dataValues[$this->storage][$key]) && !is_array(self::$dataValues[$this->storage][$key])) { @@ -753,16 +753,21 @@ public function hdel($key, $field) return $this->returnPipedInfo(0); } - if (array_key_exists($field, self::$dataValues[$this->storage][$key])) { - unset(self::$dataValues[$this->storage][$key][$field]); - if (0 === count(self::$dataValues[$this->storage][$key])) { - unset(self::$dataTypes[$this->storage][$key]); - } + $fields = is_array($fields) ? $fields : [$fields]; + $info = 0; - return $this->returnPipedInfo(1); - } else { - return $this->returnPipedInfo(0); + foreach ($fields as $field) { + if (array_key_exists($field, self::$dataValues[$this->storage][$key])) { + unset(self::$dataValues[$this->storage][$key][$field]); + if (0 === count(self::$dataValues[$this->storage][$key])) { + unset(self::$dataTypes[$this->storage][$key]); + } + + $info++; + } } + + return $this->returnPipedInfo($info); } public function hkeys($key) From c3a976e0741b1676d099d8f35b75400d7660c3d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0ilgalis?= Date: Mon, 3 Jan 2022 13:52:05 +0200 Subject: [PATCH 3/5] Remove unused import --- tests/units/RedisMockFactory.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/units/RedisMockFactory.php b/tests/units/RedisMockFactory.php index 4cca61b..16f7f51 100644 --- a/tests/units/RedisMockFactory.php +++ b/tests/units/RedisMockFactory.php @@ -3,7 +3,6 @@ namespace M6Web\Component\RedisMock\tests\units; use M6Web\Component\RedisMock\RedisMockFactory as Factory; -use M6Web\Component\RedisMock\RedisMock as Mock; use atoum; /** From 9ab14f08366a98fa07093af14b965c715ca8fb64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0ilgalis?= Date: Mon, 3 Jan 2022 13:52:52 +0200 Subject: [PATCH 4/5] Fix test namespace --- composer.json | 3 +++ tests/units/RedisMock.php | 2 +- tests/units/RedisMockFactory.php | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index cdf2149..9e61941 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,9 @@ "autoload": { "psr-0": {"M6Web\\Component\\RedisMock": "src/"} }, + "autoload-dev": { + "psr-4": {"M6Web\\Component\\RedisMock\\Tests\\Units\\": "tests/units/"} + }, "require": { "php": ">=7.1.0" }, diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 92ec659..9e2a1f2 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -1,6 +1,6 @@ Date: Mon, 3 Jan 2022 20:06:47 +0200 Subject: [PATCH 5/5] Fix deprecations --- src/M6Web/Component/RedisMock/RedisMockFactory.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/M6Web/Component/RedisMock/RedisMockFactory.php b/src/M6Web/Component/RedisMock/RedisMockFactory.php index 4502256..91c86fe 100644 --- a/src/M6Web/Component/RedisMock/RedisMockFactory.php +++ b/src/M6Web/Component/RedisMock/RedisMockFactory.php @@ -300,13 +300,19 @@ protected function getMethodSignature(\ReflectionMethod $method) $signatures = array(); foreach ($method->getParameters() as $parameter) { $signature = ''; + $parameterType = $parameter->getType(); + $isReflectionNamedType = $parameterType instanceof \ReflectionNamedType; // typeHint - if ($parameter->isArray()) { + if ($isReflectionNamedType && $parameterType->getName() === 'array') { $signature .= 'array '; - } elseif (method_exists($parameter, 'isCallable') && $parameter->isCallable()) { + } elseif ( + method_exists($parameter, 'isCallable') + && $isReflectionNamedType + && $parameterType->getName() === 'callable' + ) { $signature .= 'callable '; - } elseif ($parameter->getClass()) { - $signature .= sprintf('\%s ', $parameter->getClass()); + } elseif ($isReflectionNamedType && $parameterType->getName() === 'object') { + $signature .= sprintf('\%s ', get_class($parameter)); } // reference if ($parameter->isPassedByReference()) {