diff --git a/README.md b/README.md index 482d9e9..5a6705a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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* diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index 9801d43..841f383 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -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); @@ -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) diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 716b9fb..af77f6b 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -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(); @@ -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();