Skip to content

Commit

Permalink
Added RedisMock::bitcount, RedisMock::getbit, RedisMock::setbit comma…
Browse files Browse the repository at this point in the history
…nds (#106)

* Added RedisMock::bitcount, RedisMock::getbit, RedisMock::setbit commands

Co-authored-by: Luke Adamczewski <[email protected]>
Co-authored-by: Oliver THEBAULT <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2022
1 parent 19fbee7 commit e126231
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
47 changes: 47 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -1316,4 +1316,51 @@ 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)
{
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;
}
}
4 changes: 2 additions & 2 deletions src/M6Web/Component/RedisMock/RedisMockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

/**
* 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>
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -1918,4 +1918,37 @@ 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);

$this->assert->variable($redisMock->bitcount('myKey'))->isEqualTo(3);
$this->assert->variable($redisMock->bitcount('otherKey'))->isEqualTo(0);
}

public function testGetbitCommand()
{
$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 testSetbitCommand()
{
$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 e126231

Please sign in to comment.