diff --git a/CHANGELOG.md b/CHANGELOG.md index fec7bee1..6ec3ae2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Added support for `XCLAIM` command (#1557) - Added support for `XPENDING` command (#1558) - Added support for `XSETID` command (#1559) +- Added validation and support for the new `BITOP` command operations (#1566) ### Changed - Handle and retry `LOADING` errors from Sentinel replicas (#1536) diff --git a/src/Command/Redis/BITOP.php b/src/Command/Redis/BITOP.php index 8dc6881a..b0a1d737 100644 --- a/src/Command/Redis/BITOP.php +++ b/src/Command/Redis/BITOP.php @@ -12,6 +12,7 @@ namespace Predis\Command\Redis; +use InvalidArgumentException; use Predis\Command\PrefixableCommand as RedisCommand; /** @@ -19,6 +20,8 @@ use Predis\Command\PrefixableCommand as RedisCommand; */ class BITOP extends RedisCommand { + private const VALID_OPERATIONS = ['AND', 'OR', 'XOR', 'NOT', 'DIFF', 'DIFF1', 'ANDOR', 'ONE']; + /** * {@inheritdoc} */ @@ -38,6 +41,13 @@ class BITOP extends RedisCommand array_unshift($arguments, $operation, $destination); } + if (!empty($arguments)) { + $operation = strtoupper($arguments[0]); + if (!in_array($operation, self::VALID_OPERATIONS, false)) { + throw new InvalidArgumentException('BITOP operation must be one of: AND, OR, XOR, NOT, DIFF, DIFF1, ANDOR, ONE'); + } + } + parent::setArguments($arguments); } diff --git a/tests/Predis/Command/Redis/BITOP_Test.php b/tests/Predis/Command/Redis/BITOP_Test.php index 3c3b83eb..fb6b6f9c 100644 --- a/tests/Predis/Command/Redis/BITOP_Test.php +++ b/tests/Predis/Command/Redis/BITOP_Test.php @@ -12,6 +12,7 @@ namespace Predis\Command\Redis; +use InvalidArgumentException; use Predis\Command\PrefixableCommand; /** @@ -84,9 +85,9 @@ class BITOP_Test extends PredisCommandTestCase { /** @var PrefixableCommand $command */ $command = $this->getCommand(); - $actualArguments = ['arg1', 'arg2', 'arg3', 'arg4']; + $actualArguments = ['AND', 'arg1', 'arg2', 'arg3', 'arg4']; $prefix = 'prefix:'; - $expectedArguments = ['arg1', 'prefix:arg2', 'prefix:arg3', 'prefix:arg4']; + $expectedArguments = ['AND', 'prefix:arg1', 'prefix:arg2', 'prefix:arg3', 'prefix:arg4']; $command->setArguments($actualArguments); $command->prefixKeys($prefix); @@ -154,6 +155,69 @@ class BITOP_Test extends PredisCommandTestCase $this->assertSame(":\x80", $redis->get('key:dst')); } + /** + * @group connected + * @requiresRedisVersion >= 8.2.0 + */ + public function testCanPerformBitwiseONE(): void + { + $redis = $this->getClient(); + + $redis->set('key:src:1', "\x01"); + $redis->set('key:src:2', "\x02"); + + $this->assertSame(1, $redis->bitop('ONE', 'key:dst', 'key:src:1', 'key:src:2')); + $this->assertSame("\x03", $redis->get('key:dst')); + } + + /** + * @group connected + * @requiresRedisVersion >= 8.2.0 + */ + public function testCanPerformBitwiseDIFF(): void + { + $redis = $this->getClient(); + + $redis->set('key:src:1', "\x01"); + $redis->set('key:src:2', "\x00"); + $redis->set('key:src:3', "\x04"); + + $this->assertSame(1, $redis->bitop('DIFF', 'key:dst', 'key:src:1', 'key:src:2', 'key:src:3')); + $this->assertSame("\x01", $redis->get('key:dst')); + } + + /** + * @group connected + * @requiresRedisVersion >= 8.2.0 + */ + public function testCanPerformBitwiseDIFF1(): void + { + $redis = $this->getClient(); + + $redis->set('key:src:1', "\x01"); + $redis->set('key:src:2', "\x00"); + $redis->set('key:src:3', "\x04"); + + $this->assertSame(1, $redis->bitop('DIFF1', 'key:dst', 'key:src:1', 'key:src:2', 'key:src:3')); + $this->assertSame("\x04", $redis->get('key:dst')); + } + + /** + * @group connected + * @requiresRedisVersion >= 8.2.0 + */ + public function testCanPerformBitwiseANDOR(): void + { + $redis = $this->getClient(); + + $redis->set('key:src:1', "\x03"); + $redis->set('key:src:2', "\x02"); + $redis->set('key:src:3', "\x04"); + + $this->assertSame(1, $redis->bitop('ANDOR', 'key:dst', 'key:src:1', 'key:src:2', 'key:src:3')); + $this->assertSame("\x02", $redis->get('key:dst')); + } + /** * @group connected * @requiresRedisVersion >= 2.6.0 @@ -181,15 +245,15 @@ class BITOP_Test extends PredisCommandTestCase } /** - * @group connected - * @requiresRedisVersion >= 2.6.0 + * @group disconnected */ public function testThrowsExceptionOnInvalidOperation(): void { - $this->expectException('Predis\Response\ServerException'); - $this->expectExceptionMessage('ERR syntax error'); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('BITOP operation must be one of: AND, OR, XOR, NOT, DIFF, DIFF1, ANDOR, ONE'); - $this->getClient()->bitop('NOOP', 'key:dst', 'key:src:1', 'key:src:2'); + $command = $this->getCommand(); + $command->setArguments(['NOOP', 'key:dst', 'key:src:1', 'key:src:2']); } /**