Added support for SISMEMBER command (#871)

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2022-12-23 21:18:50 +02:00
committed by GitHub
parent 9c543ef5b1
commit e540ca062b
4 changed files with 155 additions and 0 deletions
+1
View File
@@ -107,6 +107,7 @@ use Predis\Command\CommandInterface;
* @method $this sinterstore($destination, array|string $keys)
* @method $this sismember($key, $member)
* @method $this smembers($key)
* @method $this smismember(string $key, string ...$members)
* @method $this smove($source, $destination, $member)
* @method $this spop($key, $count = null)
* @method $this srandmember($key, $count = null)
+1
View File
@@ -116,6 +116,7 @@ use Predis\Response\Status;
* @method int sinterstore(string $destination, array|string $keys)
* @method int sismember(string $key, string $member)
* @method string[] smembers(string $key)
* @method array smismember(string $key, string ...$members)
* @method int smove(string $source, string $destination, string $member)
* @method string|array|null spop(string $key, int $count = null)
* @method string|null srandmember(string $key, int $count = null)
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Predis\Command\Redis;
use Predis\Command\Command as RedisCommand;
/**
* @link https://redis.io/commands/smismember/
*
* Returns whether each member is a member of the set stored at key.
*/
class SMISMEMBER extends RedisCommand
{
public function getId()
{
return 'SMISMEMBER';
}
}
@@ -0,0 +1,135 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis;
use Predis\Response\ServerException;
/**
* @group commands
* @group realm-hash
*/
class SMISMEMBER_Test extends PredisCommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand(): string
{
return SMISMEMBER::class;
}
/**
* {@inheritdoc}
*/
protected function getExpectedId(): string
{
return 'SMISMEMBER';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$arguments = ['key', 'member1', 'member2'];
$expected = ['key', 'member1', 'member2'];
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$command = $this->getCommand();
$this->assertSame(1, $command->parseResponse(1));
}
/**
* @group connected
* @dataProvider membersProvider
* @param array $set
* @param string $key
* @param array $members
* @param array $expectedResponse
* @return void
* @requiresRedisVersion >= 6.2.0
*/
public function testReturnsCorrectResponseIfMemberBelongsToSet(
array $set,
string $key,
array $members,
array $expectedResponse
): void {
$redis = $this->getClient();
$redis->sadd(...$set);
$this->assertSame($expectedResponse, $redis->smismember($key, ...$members));
}
/**
* @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->sismember('foo', 'member1');
}
public function membersProvider(): array
{
return [
'with one member - belongs to set' => [
['key', 'member1'],
'key',
['member1'],
[1]
],
'with one member - does not belongs to set' => [
['key', 'member1'],
'key',
['member2'],
[0]
],
'with multiple members - belongs to set' => [
['key', 'member1', 'member2'],
'key',
['member1', 'member2'],
[1, 1],
],
'with multiple members - partially belongs to set' => [
['key', 'member1', 'member2'],
'key',
['member1', 'member3'],
[1, 0],
],
'with multiple members - does not belongs to set' => [
['key', 'member1', 'member2'],
'key',
['member3', 'member4'],
[0, 0],
],
];
}
}