Skip to content

Commit

Permalink
Add hincrby command
Browse files Browse the repository at this point in the history
  • Loading branch information
sforsberg committed Feb 19, 2018
1 parent 904ea81 commit 04278f7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -59,6 +59,7 @@ Redis command | Description
**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
**HINCRBY** *key* *field* *increment* | Increments the integer stored at `field` in the hash stored at `key` by `increment`.
**LINDEX** *key* *index* | Returns the element at index *index* in the list stored at *key*
**LLEN** *key* | Returns the length of the list stored at *key*
**LPUSH** *key* *value* *[value ...]* | Pushs values at the head of a list
Expand Down
19 changes: 19 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Expand Up @@ -727,6 +727,25 @@ public function hexists($key, $field)
return $this->returnPipedInfo((int) isset(self::$dataValues[$this->storage][$key][$field]));
}

public function hincrby($key, $field, $increment)
{
$this->deleteOnTtlExpired($key);

if (!isset(self::$dataValues[$this->storage][$key])) {
self::$dataValues[$this->storage][$key] = array();
}

if (!isset(self::$dataValues[$this->storage][$key][$field])) {
self::$dataValues[$this->storage][$key][$field] = (int) $increment;
} elseif (!is_integer(self::$dataValues[$this->storage][$key][$field])) {
return $this->returnPipedInfo(null);
} else {
self::$dataValues[$this->storage][$key][$field] += (int) $increment;
}

return $this->returnPipedInfo(self::$dataValues[$this->storage][$key][$field]);
}

// Sorted set

public function zrange($key, $start, $stop, $withscores = false)
Expand Down
33 changes: 33 additions & 0 deletions tests/units/RedisMock.php
Expand Up @@ -1186,6 +1186,39 @@ public function testHSetHMSetHGetHDelHExistsHKeysHLenHGetAll()
;
}

public function testHincrby()
{
$redisMock = new Redis();

$this->assert
->variable($redisMock->hget('test', 'count'))
->isNull()
->integer($redisMock->hincrby('test', 'count', 5))
->isEqualTo(5)
->integer($redisMock->hget('test', 'count'))
->isEqualTo(5)
->integer($redisMock->hincrby('test', 'count', 1))
->isEqualTo(6)
->integer($redisMock->hincrby('test', 'count', 2))
->isEqualTo(8)
->integer($redisMock->hset('test', 'count', 'something'))
->isEqualTo(0)
->variable($redisMock->hincrby('test', 'count', 4))
->isNull()
->integer($redisMock->hdel('test', 'count'))
->isEqualTo(1)
->integer($redisMock->hincrby('test', 'count', 2))
->isEqualTo(2)
->integer($redisMock->expire('test', 1))
->isEqualTo(1);
sleep(2);
$this->assert
->integer($redisMock->hincrby('test', 'count', 3))
->isEqualTo(3)
->integer($redisMock->hincrby('test', 'count', -2))
->isEqualTo(1);
}

public function testLLen()
{
$redisMock = new Redis();
Expand Down

0 comments on commit 04278f7

Please sign in to comment.