Skip to content

Commit

Permalink
Merge pull request #49 from sforsberg/add-hkeys-hlen-commands
Browse files Browse the repository at this point in the history
Adds hkeys & hlen Commands
  • Loading branch information
omansour authored Jul 1, 2016
2 parents 51f72e4 + 1f48aa8 commit 8b7e6db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Redis command | Description
**HMGET** *key* *array\<field\>* | 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\<field, value\>* | 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
Expand Down
18 changes: 18 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
15 changes: 14 additions & 1 deletion tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ public function testZRevRangeByScore()
->isEmpty();;
}

public function testHSetHMSetHGetHDelHExistsHGetAll()
public function testHSetHMSetHGetHDelHExistsHKeysHLenHGetAll()
{
$redisMock = new Redis();

Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 8b7e6db

Please sign in to comment.