Fix prefixes for LMOVE and BLMOVE (#1455)

Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
This commit is contained in:
Markus Reinhold
2024-05-02 17:55:20 +02:00
committed by GitHub
parent 4dc72458e8
commit cbef71090b
2 changed files with 60 additions and 0 deletions
@@ -33,6 +33,7 @@ class KeyPrefixProcessor implements ProcessorInterface
$this->prefix = $prefix;
$prefixFirst = static::class . '::first';
$prefixFirstTwo = static::class . '::firstTwo';
$prefixAll = static::class . '::all';
$prefixInterleaved = static::class . '::interleaved';
$prefixSkipFirst = static::class . '::skipFirst';
@@ -198,6 +199,8 @@ class KeyPrefixProcessor implements ProcessorInterface
/* ---------------- Redis 6.2 ---------------- */
'GETDEL' => $prefixFirst,
'ZMSCORE' => $prefixFirst,
'LMOVE' => $prefixFirstTwo,
'BLMOVE' => $prefixFirstTwo,
'GEOSEARCH' => $prefixFirst,
/* ---------------- Redis 7.0 ---------------- */
@@ -396,6 +399,24 @@ class KeyPrefixProcessor implements ProcessorInterface
}
}
/**
* Applies the specified prefix only to the first two arguments.
*
* @param CommandInterface $command Command instance.
* @param string $prefix Prefix string.
*/
public static function firstTwo(CommandInterface $command, $prefix)
{
$arguments = $command->getArguments();
$length = min(count($arguments), 2);
for ($i = 0; $i < $length; $i++) {
$arguments[$i] = "$prefix{$arguments[$i]}";
}
$command->setRawArguments($arguments);
}
/**
* Applies the specified prefix to all the arguments.
*
@@ -120,6 +120,37 @@ class KeyPrefixProcessorTest extends PredisTestCase
$this->assertEmpty($command->getArguments());
}
/**
* @group disconnected
*/
public function testPrefixFirstTwo(): void
{
$arguments = ['1st', '2nd', '3rd', '4th'];
$expected = ['prefix:1st', 'prefix:2nd', '3rd', '4th'];
$command = $this->getMockForAbstractClass('Predis\Command\Command');
$command->setRawArguments($arguments);
KeyPrefixProcessor::firstTwo($command, 'prefix:');
$this->assertSame($expected, $command->getArguments());
// One argument
$arguments = ['1st'];
$expected = ['prefix:1st'];
$command = $this->getMockForAbstractClass('Predis\Command\Command');
$command->setRawArguments($arguments);
KeyPrefixProcessor::firstTwo($command, 'prefix:');
$this->assertSame($expected, $command->getArguments());
// Empty arguments
$command = $this->getMockForAbstractClass('Predis\Command\Command');
KeyPrefixProcessor::firstTwo($command, 'prefix:');
$this->assertEmpty($command->getArguments());
}
/**
* @group disconnected
*/
@@ -978,6 +1009,14 @@ class KeyPrefixProcessorTest extends PredisTestCase
['key'],
['prefix:key'],
],
['LMOVE',
['key:source', 'key:destination', 'left', 'right'],
['prefix:key:source', 'prefix:key:destination', 'left', 'right'],
],
['BLMOVE',
['key:source', 'key:destination', 'left', 'right', 10],
['prefix:key:source', 'prefix:key:destination', 'left', 'right', 10],
],
/* ---------------- Redis 7.0 ---------------- */
['EXPIRETIME',
['key'],