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/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/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) 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()) { diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 7c14a7d..9e2a1f2 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -1,6 +1,6 @@ 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')) diff --git a/tests/units/RedisMockFactory.php b/tests/units/RedisMockFactory.php index 4cca61b..df2861c 100644 --- a/tests/units/RedisMockFactory.php +++ b/tests/units/RedisMockFactory.php @@ -1,9 +1,8 @@