Extend Sorted Set support by implementing ZMSCORE (#823)

This commit is contained in:
vladvildanov
2022-11-17 17:48:16 +02:00
committed by GitHub
parent bdebad7d62
commit 94bd54b2af
4 changed files with 123 additions and 0 deletions
+1
View File
@@ -111,6 +111,7 @@ use Predis\Command\CommandInterface;
* @method $this zcount($key, $min, $max)
* @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 zrange($key, $start, $stop, array $options = null)
* @method $this zrangebyscore($key, $min, $max, array $options = null)
* @method $this zrank($key, $member)
+1
View File
@@ -127,6 +127,7 @@ use Predis\Response\Status;
* @method string zcount(string $key, int|string $min, int|string $max)
* @method string zincrby(string $key, int $increment, string $member)
* @method int zinterstore(string $destination, array|string $keys, array $options = null)
* @method array zmscore(string $key, string ...$member)
* @method array zpopmin(string $key, int $count = 1)
* @method array zpopmax(string $key, int $count = 1)
* @method array zrange(string $key, int|string $start, int|string $stop, array $options = null)
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace Predis\Command\Redis;
use Predis\Command\Command as RedisCommand;
/**
* @link https://redis.io/commands/zmscore/
*
* Returns the scores associated with the specified members
* in the sorted set stored at key.
*
* For every member that does not exist in the sorted set, a null value is returned.
*
* @version >= 6.2.0
*/
class ZMSCORE extends RedisCommand
{
/**
* @inheritDoc
*/
public function getId()
{
return 'ZMSCORE';
}
}
@@ -0,0 +1,95 @@
<?php
namespace Predis\Command\Redis;
use Predis\Response\ServerException;
/**
* @group commands
* @group realm-zset
*/
class ZMSCORE_Test extends PredisCommandTestCase
{
/**
* @inheritDoc
*/
protected function getExpectedCommand(): string
{
return ZMSCORE::class;
}
/**
* @inheritDoc
*/
protected function getExpectedId(): string
{
return 'ZMSCORE';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$arguments = ['zset', 'member1', 'member2', 'member3'];
$expected = ['zset', 'member1', 'member2', 'member3'];
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$this->assertSame(1, $this->getCommand()->parseResponse(1));
}
/**
* @group connected
* @dataProvider membersProvider
* @requiresRedisVersion >= 6.2.0
*/
public function testReturnsScoresAssociatedWithMembers(
string $key,
array $membersDictionary,
array $expectedResponse
): void {
$redis = $this->getClient();
$notExpectedMember = 'not_expected';
/** @var string[] $members */
$members = array_filter($membersDictionary, static function($item) {
return is_string($item);
});
$redis->zadd($key, ...$membersDictionary);
$this->assertSame($expectedResponse, $redis->zmscore($key, ...$members));
$this->assertNull($redis->zmscore($key, $notExpectedMember)[0]);
}
/**
* @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('foo', 'bar');
$redis->zmscore('foo', '');
}
public function membersProvider(): array
{
return [['test-zscore', [1, 'member1', 2, 'member2', 3, 'member3'], ['1', '2', '3']]];
}
}