Extend Sorted Set support by implementing ZRANDMEMBER command (#825)

* Added support for ZRANDMEMBER, added test coverage

* Changed key to more specific on exception testing

Co-authored-by: Vladislav <vladislav@Admins-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2022-11-21 18:37:04 +02:00
committed by GitHub
parent 94bd54b2af
commit 169604bf74
4 changed files with 172 additions and 0 deletions
+1
View File
@@ -112,6 +112,7 @@ use Predis\Command\CommandInterface;
* @method $this zincrby($key, $increment, $member)
* @method $this zinterstore($destination, array|string $keys, array $options = null)
* @method $this zmscore(string $key, string ...$member)
* @method $this zrandmember(string $key, int $count = 1, bool $withScores = false)
* @method $this zrange($key, $start, $stop, array $options = null)
* @method $this zrangebyscore($key, $min, $max, array $options = null)
* @method $this zrank($key, $member)
+1
View File
@@ -130,6 +130,7 @@ use Predis\Response\Status;
* @method array zmscore(string $key, string ...$member)
* @method array zpopmin(string $key, int $count = 1)
* @method array zpopmax(string $key, int $count = 1)
* @method mixed zrandmember(string $key, int $count = 1, bool $withScores = false)
* @method array zrange(string $key, int|string $start, int|string $stop, array $options = null)
* @method array zrangebyscore(string $key, int|string $min, int|string $max, array $options = null)
* @method int|null zrank(string $key, string $member)
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace Predis\Command\Redis;
use Predis\Command\Command as RedisCommand;
/**
* @link https://redis.io/commands/zrandmember/
*
* Return a random element from the sorted set value stored at key.
*
* If the provided count argument is positive, return an array of distinct elements.
*
* If called with a negative count, the behavior changes and the command
* is allowed to return the same element multiple times.
*
* @version >= 6.2.0
*/
class ZRANDMEMBER extends RedisCommand
{
public function getId()
{
return 'ZRANDMEMBER';
}
public function setArguments(array $arguments)
{
$withScores = (count($arguments) === 3)
? array_pop($arguments)
: false;
if (is_bool($withScores) && $withScores) {
$arguments[] = 'WITHSCORES';
}
parent::setArguments($arguments);
}
}
@@ -0,0 +1,131 @@
<?php
namespace Predis\Command\Redis;
use Predis\Response\ServerException;
class ZRANDMEMBER_test extends PredisCommandTestCase
{
/**
* @inheritDoc
*/
protected function getExpectedCommand(): string
{
return ZRANDMEMBER::class;
}
/**
* @inheritDoc
*/
protected function getExpectedId(): string
{
return 'ZRANDMEMBER';
}
/**
* @group disconnected
* @dataProvider argumentsProvider
*/
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
{
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSame($expectedArguments, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$this->assertSame(1, $this->getCommand()->parseResponse(1));
}
/**
* @group connected
* @param string $key
* @param int $count
* @param array $membersDictionary
* @param array $expectedResponse
* @param bool $withScores
* @return void
* @dataProvider membersProvider
* @requiresRedisVersion >= 6.2.0
*/
public function testReturnsRandomMembersFromSortedSet(
string $key,
int $count,
array $membersDictionary,
array $expectedResponse,
bool $withScores
): void {
$redis = $this->getClient();
$notExpectedKey = 'not_expected';
$redis->zadd($key, ...$membersDictionary);
$this->assertSameValues($redis->zrandmember($key, $count, $withScores), $expectedResponse);
$this->assertNull($redis->zrandmember($notExpectedKey));
}
/**
* @group connected
* @requiresRedisVersion >= 6.2.0
*/
public function testThrowsExceptionOnWrongType(): void
{
$this->expectException(ServerException::class);
$this->expectExceptionMessage('Operation against a key holding the wrong kind of value');
$redis = $this->getClient();
$redis->set('zrandmember_foo', 'bar');
$redis->zrandmember('zrandmember_foo', 1, true);
}
public function argumentsProvider(): array
{
return [
'with scores' => [['zset', 5, 'withScores' => true], ['zset', 5, 'WITHSCORES']],
'without scores' => [['zset', 5], ['zset', 5]],
'without scores - false value' => [['zset', 5, 'withScores' => false], ['zset', 5]],
];
}
public function membersProvider(): array
{
return [
'one member - without score' => ['test-zset', 1, [1, 'member1'], ['member1'], false],
'multiple members - positive count - without score' => [
'test-zset',
2,
[1, 'member1', 2, 'member2'],
['member1', 'member2'],
false
],
'multiple members - negative count - without score' => [
'test-zset',
-2,
[1, 'member1'],
['member1', 'member1'],
false
],
'one member - with score' => ['test-zset', 1, [1, 'member1'], ['member1', 1], true],
'multiple members - positive count - with score' => [
'test-zset',
2,
[1, 'member1', 2, 'member2'],
['member1', 1, 'member2', 2],
true
],
'multiple members - negative count - with score' => [
'test-zset',
-2,
[1, 'member1'],
['member1', 1, 'member1', 1],
true
],
];
}
}