From 09f9abff377ae118efbae4ebf6e49142930d7844 Mon Sep 17 00:00:00 2001 From: Benjamin Dos Santos Date: Wed, 11 Sep 2019 11:26:43 +0200 Subject: [PATCH 1/3] Add zrevrank --- src/M6Web/Component/RedisMock/RedisMock.php | 11 +++++++++++ tests/units/RedisMock.php | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index b5261af..04d770b 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -1048,6 +1048,17 @@ public function zrank($key, $member) return $this->returnPipedInfo($rank); } + public function zrevrank($key, $member) + { + $rank = $this->zrank($key, $member); + if ($rank === null) { + return $this->returnPipedInfo(null); + } + + $revRank = count (self::$dataValues[$this->storage][$key]) - $rank - 1; + return $this->returnPipedInfo($revRank); + } + public function zremrangebyscore($key, $min, $max) { if (!isset(self::$dataValues[$this->storage][$key]) || $this->deleteOnTtlExpired($key)) { return $this->returnPipedInfo(0); diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 8b6b17c..b16556f 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -812,6 +812,27 @@ public function testZRank() $this->assert->variable($redisMock->zrank('invalid', 'whatever'))->isEqualTo(null); } + public function testZRevRank() + { + $redisMock = new Redis(); + $redisMock->zadd('test', 4, 'test1'); + $redisMock->zadd('test', 15, 'test2'); + $redisMock->zadd('test', 2, 'test3'); + $redisMock->zadd('test', 1, 'test4'); + $redisMock->zadd('test', 30, 'test5'); + $redisMock->zadd('test', 0, 'test6'); + + $this->assert->variable($redisMock->zrevrank('test', 'test5'))->isEqualTo(0); + $this->assert->variable($redisMock->zrevrank('test', 'test2'))->isEqualTo(1); + $this->assert->variable($redisMock->zrevrank('test', 'test1'))->isEqualTo(2); + $this->assert->variable($redisMock->zrevrank('test', 'test3'))->isEqualTo(3); + $this->assert->variable($redisMock->zrevrank('test', 'test4'))->isEqualTo(4); + $this->assert->variable($redisMock->zrevrank('test', 'test6'))->isEqualTo(5); + + $this->assert->variable($redisMock->zrevrank('test', 'invalid'))->isEqualTo(null); + $this->assert->variable($redisMock->zrevrank('invalid', 'whatever'))->isEqualTo(null); + } + public function testZRange() { $redisMock = new Redis(); From 332d41328c69056a48350c900f806dcb88544b62 Mon Sep 17 00:00:00 2001 From: Benjamin Dos Santos Date: Wed, 11 Sep 2019 11:32:48 +0200 Subject: [PATCH 2/3] update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 92c3c0d..a75ed7e 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,8 @@ Redis command | Description **ZCARD** *key* | Returns the sorted set cardinality (number of elements) of the sorted set stored at *key* **ZRANGE** *key* *start* *stop* *[withscores]* | Returns the specified range of members in a sorted set **ZRANGEBYSCORE** *key* *min* *max* *options* | Returns a range of members in a sorted set, by score -**ZRANK** *key* *member* | Returns the rank of *member* in the sorted set stored at *key* +**ZRANK** *key* *member* | Returns the rank of *member* in the sorted set stored at *key*, with the scores ordered from low to high +**ZREVRANK** *key* *member* | Returns the rank of *member* in the sorted set stored at *key*, with the scores ordered from high to low **ZREM** *key* *member* | Removes one membner from a sorted set **ZREMRANGEBYSCORE** *key* *min* *max* | Removes all members in a sorted set within the given scores **ZREVRANGE** *key* *start* *stop* *[withscores]*| Returns the specified range of members in a sorted set, with scores ordered from high to low From f3239cfc59a9f43cccd4e9eb0993bcc39eff63f8 Mon Sep 17 00:00:00 2001 From: Benjamin Dos Santos Date: Wed, 11 Sep 2019 17:30:29 +0200 Subject: [PATCH 3/3] code style --- src/M6Web/Component/RedisMock/RedisMock.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index 04d770b..0bf4195 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -1055,7 +1055,8 @@ public function zrevrank($key, $member) return $this->returnPipedInfo(null); } - $revRank = count (self::$dataValues[$this->storage][$key]) - $rank - 1; + $revRank = count(self::$dataValues[$this->storage][$key]) - $rank - 1; + return $this->returnPipedInfo($revRank); }