diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index cd358521..761d7977 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -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) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index c4110ded..aec8a059 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -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) diff --git a/src/Command/Redis/SMISMEMBER.php b/src/Command/Redis/SMISMEMBER.php new file mode 100644 index 00000000..41d34674 --- /dev/null +++ b/src/Command/Redis/SMISMEMBER.php @@ -0,0 +1,18 @@ + + * + * 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], + ], + ]; + } +}