BITOP added operation validation (#1566)

* BITOP added operation validation

* Ensure that the arguments are not an empty list before doing operation check

* Updated changelog, fixed linter errors

* added test for the new bitop operations

* fixed liniting

* update requiresRedisVersion

---------

Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com>
This commit is contained in:
Hristo Temelski
2025-06-21 11:03:17 +03:00
committed by GitHub
parent 5e59123f0a
commit e0a03c6590
3 changed files with 82 additions and 7 deletions
+1
View File
@@ -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)
+10
View File
@@ -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);
}
+71 -7
View File
@@ -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']);
}
/**