From 46acdc11b3b4ecd5e885d1d39d22ae02f08264ea Mon Sep 17 00:00:00 2001 From: Luke Adamczewski Date: Sat, 11 Jun 2022 16:06:41 +0200 Subject: [PATCH] Added RedisMock::bitcount, RedisMock::getbit, RedisMock::setbit commands --- src/M6Web/Component/RedisMock/RedisMock.php | 51 +++++++++++++++++++ .../Component/RedisMock/RedisMockFactory.php | 9 ++-- tests/units/RedisMock.php | 35 +++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index 186f35d..38952ec 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -1316,4 +1316,55 @@ public function eval($script, $numberOfKeys, ...$arguments) { return; } + + /** + * Mock the `bitcount` command + * @see https://redis.io/commands/bitcount + * + * @param string $key + * @return int + */ + public function bitcount($key) + { + if (!isset(self::$dataValues[$this->storage][$key])) { + self::$dataValues[$this->storage][$key] = []; + } + + return count(self::$dataValues[$this->storage][$key]); + } + + /** + * Mock the `setbit` command + * @see https://redis.io/commands/setbit + * + * @param string $key + * @param int $offset + * @param int $value + * @return int original value before the update + */ + public function setbit($key, $offset, $value) + { + if (!isset(self::$dataValues[$this->storage][$key])) { + self::$dataValues[$this->storage][$key] = []; + } + + $originalValue = self::$dataValues[$this->storage][$key][$offset] ?? 0; + + self::$dataValues[$this->storage][$key][$offset] = $value; + + return $originalValue; + } + + /** + * Mock the `getbit` command + * @see https://redis.io/commands/getbit + * + * @param string $key + * @param int $offset + * @return int + */ + public function getbit($key, $offset) + { + return self::$dataValues[$this->storage][$key][$offset] ?? 0; + } } diff --git a/src/M6Web/Component/RedisMock/RedisMockFactory.php b/src/M6Web/Component/RedisMock/RedisMockFactory.php index 91c86fe..3d26e7c 100644 --- a/src/M6Web/Component/RedisMock/RedisMockFactory.php +++ b/src/M6Web/Component/RedisMock/RedisMockFactory.php @@ -4,16 +4,19 @@ /** * Adapter allowing to setup a Redis Mock inheriting of an arbitrary class - * + * * WARNING ! RedisMock doesn't implement all Redis features and commands. * The mock can have undesired behavior if your parent class uses unsupported features. - * + * * @author Adrien Samson * @author Florent Dubost */ class RedisMockFactory { protected $redisCommands = array( + 'bitcount', + 'setbit', + 'getbit', 'append', 'auth', 'bgrewriteaof', @@ -213,7 +216,7 @@ public function {{method}}({{signature}}) public function __construct() { - + } CONSTRUCTOR; diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 9e2a1f2..801338f 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -1918,4 +1918,39 @@ public function testScanCommand() ->isEqualTo([0, []]); } + + public function testBitCountCommand() + { + $redisMock = new Redis(); + $redisMock->setbit('myKey', 0, 0); + $redisMock->setbit('myKey', 1, 1); + $redisMock->setbit('myKey', 2, 1); + + + // It must return two values, start cursor after the first value of the list. + $this->assert->variable($redisMock->bitcount('myKey'))->isEqualTo(3); + $this->assert->variable($redisMock->bitcount('otherKey'))->isEqualTo(0); + } + + public function testBitGetCommand() + { + $redisMock = new Redis(); + + $this->assert->variable($redisMock->getbit('myKey', 0))->isEqualTo(0); + + $redisMock->setbit('myKey', 0, 1); + $this->assert->variable($redisMock->getbit('myKey', 0))->isEqualTo(1); + } + + public function testBitSetCommand() + { + $redisMock = new Redis(); + + $this->assert->variable($redisMock->getbit('myKey', 0))->isEqualTo(0); + + $returnValue = $redisMock->setbit('myKey', 0, 1); + $this->assert->variable($returnValue)->isEqualTo(0); + $returnValue = $redisMock->setbit('myKey', 0, 0); + $this->assert->variable($returnValue)->isEqualTo(1); + } }