Skip to content

Commit e126231

Browse files
programming-servicestworzeniewebOliboy50
authored
Added RedisMock::bitcount, RedisMock::getbit, RedisMock::setbit commands (#106)
* Added RedisMock::bitcount, RedisMock::getbit, RedisMock::setbit commands Co-authored-by: Luke Adamczewski <[email protected]> Co-authored-by: Oliver THEBAULT <[email protected]>
1 parent 19fbee7 commit e126231

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/M6Web/Component/RedisMock/RedisMock.php

+47
Original file line numberDiff line numberDiff line change
@@ -1316,4 +1316,51 @@ public function eval($script, $numberOfKeys, ...$arguments)
13161316
{
13171317
return;
13181318
}
1319+
1320+
/**
1321+
* Mock the `bitcount` command
1322+
* @see https://redis.io/commands/bitcount
1323+
*
1324+
* @param string $key
1325+
* @return int
1326+
*/
1327+
public function bitcount($key)
1328+
{
1329+
return count(self::$dataValues[$this->storage][$key] ?? []);
1330+
}
1331+
1332+
/**
1333+
* Mock the `setbit` command
1334+
* @see https://redis.io/commands/setbit
1335+
*
1336+
* @param string $key
1337+
* @param int $offset
1338+
* @param int $value
1339+
* @return int original value before the update
1340+
*/
1341+
public function setbit($key, $offset, $value)
1342+
{
1343+
if (!isset(self::$dataValues[$this->storage][$key])) {
1344+
self::$dataValues[$this->storage][$key] = [];
1345+
}
1346+
1347+
$originalValue = self::$dataValues[$this->storage][$key][$offset] ?? 0;
1348+
1349+
self::$dataValues[$this->storage][$key][$offset] = $value;
1350+
1351+
return $originalValue;
1352+
}
1353+
1354+
/**
1355+
* Mock the `getbit` command
1356+
* @see https://redis.io/commands/getbit
1357+
*
1358+
* @param string $key
1359+
* @param int $offset
1360+
* @return int
1361+
*/
1362+
public function getbit($key, $offset)
1363+
{
1364+
return self::$dataValues[$this->storage][$key][$offset] ?? 0;
1365+
}
13191366
}

src/M6Web/Component/RedisMock/RedisMockFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
/**
66
* Adapter allowing to setup a Redis Mock inheriting of an arbitrary class
7-
*
7+
*
88
* WARNING ! RedisMock doesn't implement all Redis features and commands.
99
* The mock can have undesired behavior if your parent class uses unsupported features.
10-
*
10+
*
1111
* @author Adrien Samson <[email protected]>
1212
* @author Florent Dubost <[email protected]>
1313
*/

tests/units/RedisMock.php

+33
Original file line numberDiff line numberDiff line change
@@ -1918,4 +1918,37 @@ public function testScanCommand()
19181918
->isEqualTo([0, []]);
19191919

19201920
}
1921+
1922+
public function testBitcountCommand()
1923+
{
1924+
$redisMock = new Redis();
1925+
$redisMock->setbit('myKey', 0, 0);
1926+
$redisMock->setbit('myKey', 1, 1);
1927+
$redisMock->setbit('myKey', 2, 1);
1928+
1929+
$this->assert->variable($redisMock->bitcount('myKey'))->isEqualTo(3);
1930+
$this->assert->variable($redisMock->bitcount('otherKey'))->isEqualTo(0);
1931+
}
1932+
1933+
public function testGetbitCommand()
1934+
{
1935+
$redisMock = new Redis();
1936+
1937+
$this->assert->variable($redisMock->getbit('myKey', 0))->isEqualTo(0);
1938+
1939+
$redisMock->setbit('myKey', 0, 1);
1940+
$this->assert->variable($redisMock->getbit('myKey', 0))->isEqualTo(1);
1941+
}
1942+
1943+
public function testSetbitCommand()
1944+
{
1945+
$redisMock = new Redis();
1946+
1947+
$this->assert->variable($redisMock->getbit('myKey', 0))->isEqualTo(0);
1948+
1949+
$returnValue = $redisMock->setbit('myKey', 0, 1);
1950+
$this->assert->variable($returnValue)->isEqualTo(0);
1951+
$returnValue = $redisMock->setbit('myKey', 0, 0);
1952+
$this->assert->variable($returnValue)->isEqualTo(1);
1953+
}
19211954
}

0 commit comments

Comments
 (0)