Skip to content

Commit

Permalink
Merge pull request #88 from Braxilior/zrevrank
Browse files Browse the repository at this point in the history
Implement zrevrank
  • Loading branch information
omansour authored Nov 4, 2019
2 parents 5f8821e + f3239cf commit 057ad8e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 057ad8e

Please sign in to comment.