From dc066e657509248ecef7e44e4b4270012e90aa65 Mon Sep 17 00:00:00 2001 From: Baptiste Lafontaine Date: Mon, 1 Jul 2019 15:10:28 +0200 Subject: [PATCH] Fix usage of zadd --- src/M6Web/Component/RedisMock/RedisMock.php | 15 ++++++++++++--- tests/units/RedisMock.php | 11 +++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index a89b76e..c5442b5 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -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); diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index db62e7b..ced52d3 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -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();