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 diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index b5261af..0bf4195 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -1048,6 +1048,18 @@ 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();