Added support for FUNCTION DUMP, FUNCTION FLUSH, FUNCTION RESTORE commands (#1332)

This commit is contained in:
Vladyslav Vildanov
2023-07-03 10:41:32 +03:00
committed by GitHub
parent 90da582efc
commit dd64569e06
8 changed files with 368 additions and 5 deletions
@@ -17,6 +17,9 @@ use Predis\Response\Status;
/**
* @method string load(string $functionCode, bool $replace = 'false')
* @method Status delete(string $libraryName)
* @method string dump()
* @method Status flush(?string $mode = null)
* @method Status restore(string $value, ?string $policy = null)
*/
class FunctionContainer extends AbstractContainer
{
@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use Predis\Command\Strategy\SubcommandStrategyInterface;
class DumpStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
return $arguments;
}
}
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use Predis\Command\Strategy\SubcommandStrategyInterface;
class FlushStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
$processedArguments = [$arguments[0]];
if (array_key_exists(1, $arguments) && null !== $arguments[1]) {
$processedArguments[] = strtoupper($arguments[1]);
}
return $processedArguments;
}
}
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use Predis\Command\Strategy\SubcommandStrategyInterface;
class RestoreStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
$processedArguments = [$arguments[0], $arguments[1]];
if (array_key_exists(2, $arguments) && null !== $arguments[2]) {
$processedArguments[] = strtoupper($arguments[2]);
}
return $processedArguments;
}
}
+130 -5
View File
@@ -70,6 +70,44 @@ class FUNCTIONS_Test extends PredisCommandTestCase
$this->assertSameValues($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testDumpFilterArguments(): void
{
$arguments = ['DUMP'];
$expected = ['DUMP'];
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSameValues($expected, $command->getArguments());
}
/**
* @dataProvider flushArgumentsProvider
* @group disconnected
*/
public function testFlushFilterArguments(array $actualArguments, array $expectedResponse): void
{
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($expectedResponse, $command->getArguments());
}
/**
* @dataProvider restoreArgumentsProvider
* @group disconnected
*/
public function testRestoreFilterArguments(array $actualArguments, array $expectedResponse): void
{
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($expectedResponse, $command->getArguments());
}
/**
* @group disconnected
*/
@@ -86,7 +124,7 @@ class FUNCTIONS_Test extends PredisCommandTestCase
public function testLoadFunctionAddFunctionIntoGivenLibrary(): void
{
$redis = $this->getClient();
$redis->executeRaw(['FUNCTION', 'FLUSH']);
$redis->function->flush();
$actualResponse = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
@@ -105,7 +143,7 @@ class FUNCTIONS_Test extends PredisCommandTestCase
public function testLoadFunctionOverridesExistingFunctionWithReplaceArgumentGiven(): void
{
$redis = $this->getClient();
$redis->executeRaw(['FUNCTION', 'FLUSH']);
$redis->function->flush();
$actualResponse = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
@@ -132,7 +170,7 @@ class FUNCTIONS_Test extends PredisCommandTestCase
public function testLoadFunctionThrowsErrorOnAlreadyExistingLibraryGiven(): void
{
$redis = $this->getClient();
$redis->executeRaw(['FUNCTION', 'FLUSH']);
$redis->function->flush();
$actualResponse = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
@@ -160,7 +198,7 @@ class FUNCTIONS_Test extends PredisCommandTestCase
public function testDeleteFunctionRemovesAlreadyExistingLibrary(): void
{
$redis = $this->getClient();
$redis->executeRaw(['FUNCTION', 'FLUSH']);
$redis->function->flush();
$actualResponse = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
@@ -170,6 +208,65 @@ class FUNCTIONS_Test extends PredisCommandTestCase
$this->assertEquals('OK', $redis->function->delete($this->libName));
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 7.0.0
*/
public function testDumpReturnsSerializedPayloadOfLibrary(): void
{
$redis = $this->getClient();
$redis->function->flush();
$libName = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
);
$this->assertSame($this->libName, $libName);
$this->assertStringContainsString($libName, $redis->function->dump());
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 7.0.0
*/
public function testFlushRemovesAllLibraries(): void
{
$redis = $this->getClient();
$redis->function->flush();
$libName = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
);
$this->assertEquals($this->libName, $libName);
$this->assertEquals('OK', $redis->function->flush());
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 7.0.0
*/
public function testRestoresLibraryFromSerializedPayload(): void
{
$redis = $this->getClient();
$redis->function->flush();
$libName = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
);
$this->assertEquals($this->libName, $libName);
$serializedPayload = $redis->function->dump();
$this->assertStringContainsString($libName, $serializedPayload);
$redis->function->flush();
$this->assertEquals('OK', $redis->function->restore($serializedPayload));
}
/**
* @group connected
* @return void
@@ -178,11 +275,39 @@ class FUNCTIONS_Test extends PredisCommandTestCase
public function testDeleteFunctionThrowsErrorOnNonExistingLibrary(): void
{
$redis = $this->getClient();
$redis->executeRaw(['FUNCTION', 'FLUSH']);
$redis->function->flush();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('ERR Library not found');
$redis->function->delete($this->libName);
}
public function flushArgumentsProvider(): array
{
return [
'with default arguments' => [
['FLUSH', null],
['FLUSH'],
],
'with mode argument' => [
['FLUSH', 'sync'],
['FLUSH', 'SYNC'],
],
];
}
public function restoreArgumentsProvider(): array
{
return [
'with default arguments' => [
['RESTORE', 'value', null],
['RESTORE', 'value'],
],
'with mode argument' => [
['RESTORE', 'value', 'append'],
['RESTORE', 'value', 'APPEND'],
],
];
}
}
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use PredisTestCase;
class DumpStrategyTest extends PredisTestCase
{
/**
* @var DumpStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new DumpStrategy();
}
/**
* @group disconnected
* @return void
*/
public function testProcessArguments(): void
{
$this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2']));
}
}
@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use PredisTestCase;
class FlushStrategyTest extends PredisTestCase
{
/**
* @var FlushStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new FlushStrategy();
}
/**
* @dataProvider argumentsProvider
* @group disconnected
* @param array $actualArguments
* @param array $expectedResponse
* @return void
*/
public function testProcessArguments(array $actualArguments, array $expectedResponse): void
{
$this->assertSame($expectedResponse, $this->strategy->processArguments($actualArguments));
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['FLUSH', null],
['FLUSH'],
],
'with mode argument' => [
['FLUSH', 'sync'],
['FLUSH', 'SYNC'],
],
];
}
}
@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use PredisTestCase;
class RestoreStrategyTest extends PredisTestCase
{
/**
* @var RestoreStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new RestoreStrategy();
}
/**
* @dataProvider argumentsProvider
* @group disconnected
* @param array $actualArguments
* @param array $expectedResponse
* @return void
*/
public function testProcessArguments(array $actualArguments, array $expectedResponse): void
{
$this->assertSame($expectedResponse, $this->strategy->processArguments($actualArguments));
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['RESTORE', 'value', null],
['RESTORE', 'value'],
],
'with mode argument' => [
['RESTORE', 'value', 'append'],
['RESTORE', 'value', 'APPEND'],
],
];
}
}