From 1f48aa8e100bb4959220b78a5eb7c3c5276e0198 Mon Sep 17 00:00:00 2001 From: Sebastian Forsberg Date: Wed, 29 Jun 2016 10:07:38 -0500 Subject: [PATCH] Adds hkeys & hlen commands --- README.md | 2 ++ src/M6Web/Component/RedisMock/RedisMock.php | 18 ++++++++++++++++++ tests/units/RedisMock.php | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1926b03..2b0e291 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,8 @@ Redis command | Description **HMGET** *key* *array\* | Gets the values of multiple hash fields **HGET** *key* *field* | Gets the value of a hash field **HGETALL** *key* | Gets all the fields and values in a hash +**HKEYS** *key* | Gets all the fields in a hash +**HLEN** *key* | Gets the number of fields in a hash **HMSET** *key* *array\* | Sets each value in the corresponding field **HSET** *key* *field* *value* | Sets the string value of a hash field **HSETNX** *key* *field* *value* | Sets field in the hash stored at key to value, only if field does not yet exist diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index 4f9289f..0bd364a 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -635,6 +635,24 @@ public function hdel($key, $field) } } + public function hkeys($key) + { + if (!isset(self::$data[$key]) || !is_array(self::$data[$key]) || $this->deleteOnTtlExpired($key)) { + return $this->returnPipedInfo(array()); + } + + return $this->returnPipedInfo(array_keys(self::$data[$key])); + } + + public function hlen($key) + { + if (!isset(self::$data[$key]) || !is_array(self::$data[$key]) || $this->deleteOnTtlExpired($key)) { + return $this->returnPipedInfo(0); + } + + return $this->returnPipedInfo(count(self::$data[$key])); + } + public function hgetall($key) { if (!isset(self::$data[$key]) || $this->deleteOnTtlExpired($key)) { diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index e60ddd5..c9e76a1 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -988,7 +988,7 @@ public function testZRevRangeByScore() ->isEmpty();; } - public function testHSetHMSetHGetHDelHExistsHGetAll() + public function testHSetHMSetHGetHDelHExistsHKeysHLenHGetAll() { $redisMock = new Redis(); @@ -1017,6 +1017,11 @@ public function testHSetHMSetHGetHDelHExistsHGetAll() ->isEqualTo(0) ->string($redisMock->hget('test', 'test1')) ->isEqualTo('something else') + ->array($redisMock->hkeys('test')) + ->hasSize(1) + ->containsValues(array('test1')) + ->integer($redisMock->hlen('test')) + ->isEqualTo(1) ->array($redisMock->hgetall('test')) ->hasSize(1) ->containsValues(array('something else')) @@ -1053,6 +1058,14 @@ public function testHSetHMSetHGetHDelHExistsHGetAll() 'raoul' => 'nothing', ))) ->isEqualTo('OK') + ->array($redisMock->hkeys('test')) + ->isEqualTo(array( + 0 => 'test1', + 1 => 'blabla', + 2 => 'raoul', + )) + ->integer($redisMock->hlen('test')) + ->isEqualTo(3) ->array($redisMock->hgetall('test')) ->isEqualTo(array( 'test1' => 'somthing',