From 6af5394a890b153bcd02fa17ded47de7f73acc3a Mon Sep 17 00:00:00 2001 From: Michal Lulco Date: Mon, 16 May 2022 17:00:06 +0200 Subject: [PATCH] Added ZPOPMIN and ZPOPMAX (#758) * Added ZPOPMIN and ZPOPMAX * Added ZPOPMIN and ZPOPMAX * Applied patch --- src/ClientInterface.php | 2 + src/Command/Redis/ZPOPMAX.php | 44 +++++++++ src/Command/Redis/ZPOPMIN.php | 44 +++++++++ tests/Predis/Command/Redis/ZPOPMAX_Test.php | 102 ++++++++++++++++++++ tests/Predis/Command/Redis/ZPOPMIN_Test.php | 101 +++++++++++++++++++ 5 files changed, 293 insertions(+) create mode 100644 src/Command/Redis/ZPOPMAX.php create mode 100644 src/Command/Redis/ZPOPMIN.php create mode 100644 tests/Predis/Command/Redis/ZPOPMAX_Test.php create mode 100644 tests/Predis/Command/Redis/ZPOPMIN_Test.php diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 8249d295..9f1fff18 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -120,6 +120,8 @@ 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 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) * @method array zrangebyscore(string $key, int|string $min, int|string $max, array $options = null) * @method int|null zrank(string $key, string $member) diff --git a/src/Command/Redis/ZPOPMAX.php b/src/Command/Redis/ZPOPMAX.php new file mode 100644 index 00000000..e4309de5 --- /dev/null +++ b/src/Command/Redis/ZPOPMAX.php @@ -0,0 +1,44 @@ + + * + * 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\Command\Command as RedisCommand; + +/** + * @link http://redis.io/commands/zpopmax + * + * @author Daniele Alessandri + */ +class ZPOPMAX extends RedisCommand +{ + /** + * {@inheritdoc} + */ + public function getId() + { + return 'ZPOPMAX'; + } + + /** + * {@inheritdoc} + */ + public function parseResponse($data) + { + $result = array(); + + for ($i = 0; $i < count($data); ++$i) { + $result[$data[$i]] = $data[++$i]; + } + + return $result; + } +} diff --git a/src/Command/Redis/ZPOPMIN.php b/src/Command/Redis/ZPOPMIN.php new file mode 100644 index 00000000..64daab4b --- /dev/null +++ b/src/Command/Redis/ZPOPMIN.php @@ -0,0 +1,44 @@ + + * + * 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\Command\Command as RedisCommand; + +/** + * @link http://redis.io/commands/zpopmin + * + * @author Daniele Alessandri + */ +class ZPOPMIN extends RedisCommand +{ + /** + * {@inheritdoc} + */ + public function getId() + { + return 'ZPOPMIN'; + } + + /** + * {@inheritdoc} + */ + public function parseResponse($data) + { + $result = array(); + + for ($i = 0; $i < count($data); ++$i) { + $result[$data[$i]] = $data[++$i]; + } + + return $result; + } +} diff --git a/tests/Predis/Command/Redis/ZPOPMAX_Test.php b/tests/Predis/Command/Redis/ZPOPMAX_Test.php new file mode 100644 index 00000000..fce674ca --- /dev/null +++ b/tests/Predis/Command/Redis/ZPOPMAX_Test.php @@ -0,0 +1,102 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Command\Redis; + +/** + * @group commands + * @group realm-zset + */ +class ZPOPMAX_Test extends PredisCommandTestCase +{ + + /** + * {@inheritdoc} + */ + protected function getExpectedCommand(): string + { + return 'Predis\Command\Redis\ZPOPMAX'; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedId(): string + { + return 'ZPOPMAX'; + } + + /** + * @requiresRedisVersion >= 5.0.0 + * + * @group disconnected + */ + public function testFilterArguments(): void + { + $arguments = array('zset', 2); + $expected = array('zset', 2); + + $command = $this->getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @requiresRedisVersion >= 5.0.0 + * + * @group disconnected + */ + public function testParseResponse(): void + { + $raw = array('element1', '1', 'element2', '2', 'element3', '3'); + $expected = array('element1' => '1', 'element2' => '2', 'element3' => '3'); + + $command = $this->getCommand(); + + $this->assertSame($expected, $command->parseResponse($raw)); + } + + /** + * @requiresRedisVersion >= 5.0.0 + * + * @group connected + */ + public function testReturnsElements(): void + { + $redis = $this->getClient(); + + $this->assertSame(array(), $redis->zpopmax('letters')); + $this->assertSame(array(), $redis->zpopmax('letters', 3)); + + $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f'); + + $this->assertSame(array('f' => '30'), $redis->zpopmax('letters')); + $this->assertSame(array('e' => '20', 'd' => '20', 'c' => '10'), $redis->zpopmax('letters', 3)); + $this->assertSame(array('b' => '0', 'a' => '-10'), $redis->zpopmax('letters', 3)); + } + + /** + * @requiresRedisVersion >= 5.0.0 + * + * @group connected + */ + public function testThrowsExceptionOnWrongType(): void + { + $this->expectException('Predis\Response\ServerException'); + $this->expectExceptionMessage('Operation against a key holding the wrong kind of value'); + + $redis = $this->getClient(); + + $redis->set('foo', 'bar'); + $redis->zpopmax('foo'); + } +} diff --git a/tests/Predis/Command/Redis/ZPOPMIN_Test.php b/tests/Predis/Command/Redis/ZPOPMIN_Test.php new file mode 100644 index 00000000..37705ca8 --- /dev/null +++ b/tests/Predis/Command/Redis/ZPOPMIN_Test.php @@ -0,0 +1,101 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Command\Redis; + +/** + * @group commands + * @group realm-zset + */ +class ZPOPMIN_Test extends PredisCommandTestCase +{ + /** + * {@inheritdoc} + */ + protected function getExpectedCommand(): string + { + return 'Predis\Command\Redis\ZPOPMIN'; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedId(): string + { + return 'ZPOPMIN'; + } + + /** + * @requiresRedisVersion >= 5.0.0 + * + * @group disconnected + */ + public function testFilterArguments(): void + { + $arguments = array('zset', 2); + $expected = array('zset', 2); + + $command = $this->getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @requiresRedisVersion >= 5.0.0 + * + * @group disconnected + */ + public function testParseResponse(): void + { + $raw = array('element1', '1', 'element2', '2', 'element3', '3'); + $expected = array('element1' => '1', 'element2' => '2', 'element3' => '3'); + + $command = $this->getCommand(); + + $this->assertSame($expected, $command->parseResponse($raw)); + } + + /** + * @requiresRedisVersion >= 5.0.0 + * + * @group connected + */ + public function testReturnsElements(): void + { + $redis = $this->getClient(); + + $this->assertSame(array(), $redis->zpopmin('letters')); + $this->assertSame(array(), $redis->zpopmin('letters', 3)); + + $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f'); + + $this->assertSame(array('a' => '-10'), $redis->zpopmin('letters')); + $this->assertSame(array('b' => '0', 'c' => '10', 'd' => '20'), $redis->zpopmin('letters', 3)); + $this->assertSame(array('e' => '20', 'f' => '30'), $redis->zpopmin('letters', 3)); + } + + /** + * @requiresRedisVersion >= 5.0.0 + * + * @group connected + */ + public function testThrowsExceptionOnWrongType(): void + { + $this->expectException('Predis\Response\ServerException'); + $this->expectExceptionMessage('Operation against a key holding the wrong kind of value'); + + $redis = $this->getClient(); + + $redis->set('foo', 'bar'); + $redis->zpopmin('foo'); + } +}