Skip to content

Commit

Permalink
Added RedisMock::bitcount, RedisMock::getbit, RedisMock::setbit commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tworzenieweb committed Jun 11, 2022
1 parent 19fbee7 commit 46acdc1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 6 additions & 3 deletions src/M6Web/Component/RedisMock/RedisMockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <asamson.externe@m6.fr>
* @author Florent Dubost <fdubost.externe@m6.fr>
*/
class RedisMockFactory
{
protected $redisCommands = array(
'bitcount',
'setbit',
'getbit',
'append',
'auth',
'bgrewriteaof',
Expand Down Expand Up @@ -213,7 +216,7 @@ public function {{method}}({{signature}})
public function __construct()
{
}
CONSTRUCTOR;

Expand Down
35 changes: 35 additions & 0 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 46acdc1

Please sign in to comment.