Skip to content

Commit

Permalink
Merge pull request #81 from magnetik/zadd
Browse files Browse the repository at this point in the history
Fix usage of zadd
  • Loading branch information
omansour authored Jul 1, 2019
2 parents 493ad01 + dc066e6 commit f753ad6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,9 +943,18 @@ public function zrevrangebyscore($key, $max, $min, array $options = array())
}


public function zadd($key, $score, $member) {
if (func_num_args() > 3) {
throw new UnsupportedException('In RedisMock, `zadd` command can not set more than one member at once.');
public function zadd($key, ...$args) {
if (count($args) === 1) {
if (count($args[0]) > 1) {
throw new UnsupportedException('In RedisMock, `zadd` used with an array cannot be used to set more than one element.');
}
$score = reset($args[0]);
$member = key($args[0]);
} elseif (count($args) === 2) {
$score = $args[0];
$member = $args[1];
} else {
throw new UnsupportedException('In RedisMock, `zadd` command can either take two arguments (score and member), or one associative array with member as key and score as value');
}

$this->deleteOnTtlExpired($key);
Expand Down
11 changes: 11 additions & 0 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,17 @@ public function testZCard()
->isEqualTo(0);
}

public function testZAddWithArray()
{
$redisMock = new Redis();
$redisMock->zadd('test', ['test1' => 1]);
$redisMock->zadd('test', ['test2' => 10]);

$this->assert
->integer($redisMock->zcard('test'))
->isEqualTo(2);
}

public function testZRank()
{
$redisMock = new Redis();
Expand Down

0 comments on commit f753ad6

Please sign in to comment.