diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 5bd0c1bd..b1bc49a2 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -110,6 +110,7 @@ use Predis\Command\CommandInterface; * @method $this zcard($key) * @method $this zcount($key, $min, $max) * @method $this zdiff(array $keys, bool $withScores = false) + * @method $this zdiffstore(string $destination, array $keys) * @method $this zincrby($key, $increment, $member) * @method $this zinterstore($destination, array|string $keys, array $options = null) * @method $this zmscore(string $key, string ...$member) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index b246b831..f8872357 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -126,6 +126,7 @@ use Predis\Response\Status; * @method int zcard(string $key) * @method string zcount(string $key, int|string $min, int|string $max) * @method array zdiff(array $keys, bool $withScores = false) + * @method int zdiffstore(string $destination, array $keys) * @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) diff --git a/src/Command/Redis/ZDIFFSTORE.php b/src/Command/Redis/ZDIFFSTORE.php new file mode 100644 index 00000000..44c85e48 --- /dev/null +++ b/src/Command/Redis/ZDIFFSTORE.php @@ -0,0 +1,40 @@ +setNumkeys($arguments); + $arguments = $this->getArguments(); + $this->unpackKeysArray(self::$keysArgumentPositionOffset + 1, $arguments); + parent::setArguments($arguments); + } +} diff --git a/src/Command/Redis/ZMSCORE.php b/src/Command/Redis/ZMSCORE.php index 49a1dea0..5c8c13ed 100644 --- a/src/Command/Redis/ZMSCORE.php +++ b/src/Command/Redis/ZMSCORE.php @@ -12,7 +12,6 @@ use Predis\Command\Command as RedisCommand; * * For every member that does not exist in the sorted set, a null value is returned. * - * @version >= 6.2.0 */ class ZMSCORE extends RedisCommand { diff --git a/src/Command/Redis/ZRANDMEMBER.php b/src/Command/Redis/ZRANDMEMBER.php index b0044ad8..faa83d05 100644 --- a/src/Command/Redis/ZRANDMEMBER.php +++ b/src/Command/Redis/ZRANDMEMBER.php @@ -15,7 +15,6 @@ use Predis\Command\Traits\WithScores; * 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 { diff --git a/tests/Predis/Command/Redis/ZDIFFSTORE_Test.php b/tests/Predis/Command/Redis/ZDIFFSTORE_Test.php new file mode 100644 index 00000000..2d36349a --- /dev/null +++ b/tests/Predis/Command/Redis/ZDIFFSTORE_Test.php @@ -0,0 +1,95 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSame($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @dataProvider sortedSetsProvider + * @param array $firstSetDictionary + * @param array $secondSetDictionary + * @param array $expectedResponse + * @param int $expectedResultingElements + * @return void + * @requiresRedisVersion >= 6.2.0 + */ + public function testStoresDifferenceBetweenSortedSets( + array $firstSetDictionary, + array $secondSetDictionary, + array $expectedResponse, + int $expectedResultingElements + ): void { + $redis = $this->getClient(); + + $redis->zadd('test-zset-1', ...$firstSetDictionary); + $redis->zadd('test-zset-2', ...$secondSetDictionary); + $actualResponse = $redis->zdiffstore('zdiffstore', ['test-zset-1', 'test-zset-2']); + + $this->assertSame($expectedResultingElements, $actualResponse); + $this->assertSame($expectedResponse, $redis->zrange('zdiffstore', 0, -1)); + } + + + public function sortedSetsProvider(): array + { + return [ + 'no intersection' => [ + [1, 'member1', 2, 'member2', 3, 'member3'], + [1, 'member4', 2, 'member5', 3, 'member6'], + ['member1', 'member2', 'member3'], + 3 + ], + 'partial intersection' => [ + [1, 'member1', 2, 'member2', 3, 'member3'], + [1, 'member1', 2, 'member2', 3, 'member4'], + ['member3'], + 1 + ], + 'full intersection' => [ + [1, 'member1', 2, 'member2', 3, 'member3'], + [1, 'member1', 2, 'member2', 3, 'member3'], + [], + 0 + ], + ]; + } +}