Skip to content

Commit

Permalink
Merge pull request #69 from Babacooll/add-support-incrbyfloat-decrbyf…
Browse files Browse the repository at this point in the history
…loat

Add support for incrbyfloat and decrbyfloat
  • Loading branch information
omansour authored Jun 11, 2018
2 parents 3c73d01 + c54232f commit f4c6f55
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Redis command | Description
**DEL** *key* *[key ...]* | Deletes one or more keys
**DECR** *key* | Decrements the integer value of a key by one
**DECRBY** *key* *decrement* | Decrements the integer value of a key by `decrement` value
**DECRBYFLOAT** *key* *decrement* | Decrements the float value of a key by `decrement` value
**EXISTS** *key* | Determines if a key exists
**EXPIRE** *key* *seconds* | Sets a key's time to live in seconds
**FLUSHDB** | Flushes the database
Expand All @@ -49,6 +50,7 @@ Redis command | Description
**HINCRBY** *key* *field* *increment* | Increments the integer stored at `field` in the hash stored at `key` by `increment`.
**INCR** *key* | Increments the integer value of a key by one
**INCRBY** *key* *increment* | Increments the integer value of a key by `increment` value
**INCRBYFLOAT** *key* *increment* | Increments the float value of a key by `increment` value
**KEYS** *pattern* | Finds all keys matching the given pattern
**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*
Expand Down
34 changes: 34 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ public function incrby($key, $increment)
return $this->returnPipedInfo(self::$dataValues[$this->storage][$key]);
}

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

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

self::$dataTypes[$this->storage][$key] = 'string';

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

public function decr($key)
{
return $this->decrby($key, 1);
Expand All @@ -195,6 +212,23 @@ public function decrby($key, $decrement)
return $this->returnPipedInfo(self::$dataValues[$this->storage][$key]);
}

public function decrbyfloat($key, $decrement)
{
$this->deleteOnTtlExpired($key);

if (!isset(self::$dataValues[$this->storage][$key])) {
self::$dataValues[$this->storage][$key] = 0;
} elseif (!is_float(self::$dataValues[$this->storage][$key])) {
return $this->returnPipedInfo(null);
}

self::$dataValues[$this->storage][$key] -= (float) $decrement;

self::$dataTypes[$this->storage][$key] = 'string';

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

// Keys

public function type($key)
Expand Down
70 changes: 70 additions & 0 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,41 @@ public function testIncrby()
->isEqualTo(3);
}

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

$this->assert
->variable($redisMock->get('test'))
->isNull()
->float($redisMock->incrbyfloat('test', 0.5))
->isEqualTo(0.5)
->float($redisMock->get('test'))
->isEqualTo(0.5)
->string($redisMock->type('test'))
->isEqualTo('string')
->float($redisMock->incrbyfloat('test', 1))
->isEqualTo(1.5)
->float($redisMock->incrbyfloat('test', 2.5))
->isEqualTo(4)
->string($redisMock->set('test', 'something'))
->isEqualTo('OK')
->variable($redisMock->incrbyfloat('test', 3.5))
->isNull()
->integer($redisMock->del('test'))
->isEqualTo(1)
->string($redisMock->type('test'))
->isEqualTo('none')
->float($redisMock->incrbyfloat('test', 0.5))
->isEqualTo(0.5)
->integer($redisMock->expire('test', 1))
->isEqualTo(1);
sleep(2);
$this->assert
->float($redisMock->incrbyfloat('test', 0.5))
->isEqualTo(0.5);
}

public function testDecr()
{
$redisMock = new Redis();
Expand Down Expand Up @@ -339,6 +374,41 @@ public function testDecrby()
->isEqualTo(-3);
}

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

$this->assert
->variable($redisMock->get('test'))
->isNull()
->float($redisMock->decrbyfloat('test', 0.5))
->isEqualTo(-0.5)
->float($redisMock->get('test'))
->isEqualTo(-0.5)
->string($redisMock->type('test'))
->isEqualTo('string')
->float($redisMock->decrbyfloat('test', 1))
->isEqualTo(-1.5)
->float($redisMock->decrbyfloat('test', 2.5))
->isEqualTo(-4.0)
->string($redisMock->set('test', 'something'))
->isEqualTo('OK')
->variable($redisMock->decrbyfloat('test', 3.5))
->isNull()
->integer($redisMock->del('test'))
->isEqualTo(1)
->string($redisMock->type('test'))
->isEqualTo('none')
->float($redisMock->decrbyfloat('test', 0.5))
->isEqualTo(-0.5)
->integer($redisMock->expire('test', 1))
->isEqualTo(1);
sleep(2);
$this->assert
->float($redisMock->decrbyfloat('test', 0.5))
->isEqualTo(-0.5);
}

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

Expand Down

0 comments on commit f4c6f55

Please sign in to comment.