diff --git a/src/Command/Redis/FUNCTIONS.php b/src/Command/Redis/FUNCTIONS.php index 7f4fde79..5fa528dd 100644 --- a/src/Command/Redis/FUNCTIONS.php +++ b/src/Command/Redis/FUNCTIONS.php @@ -13,8 +13,6 @@ namespace Predis\Command\Redis; use Predis\Command\Command as RedisCommand; -use Predis\Command\Strategy\StrategyResolverInterface; -use Predis\Command\Strategy\SubcommandStrategyResolver; /** * @see https://redis.io/commands/?name=function @@ -24,16 +22,6 @@ use Predis\Command\Strategy\SubcommandStrategyResolver; */ class FUNCTIONS extends RedisCommand { - /** - * @var StrategyResolverInterface - */ - private $strategyResolver; - - public function __construct() - { - $this->strategyResolver = new SubcommandStrategyResolver(); - } - public function getId() { return 'FUNCTION'; @@ -41,10 +29,102 @@ class FUNCTIONS extends RedisCommand public function setArguments(array $arguments) { - $strategy = $this->strategyResolver->resolve('functions', strtolower($arguments[0])); - $arguments = $strategy->processArguments($arguments); + switch ($arguments[0]) { + case 'FLUSH': + $this->setFlushArguments($arguments); + break; + + case 'LIST': + $this->setListArguments($arguments); + break; + + case 'LOAD': + $this->setLoadArguments($arguments); + break; + + case 'RESTORE': + $this->setRestoreArguments($arguments); + break; + + default: + parent::setArguments($arguments); + } - parent::setArguments($arguments); $this->filterArguments(); } + + /** + * @param array $arguments + * @return void + */ + private function setFlushArguments(array $arguments): void + { + $processedArguments = [$arguments[0]]; + + if (array_key_exists(1, $arguments) && null !== $arguments[1]) { + $processedArguments[] = strtoupper($arguments[1]); + } + + parent::setArguments($processedArguments); + } + + /** + * @param array $arguments + * @return void + */ + private function setListArguments(array $arguments): void + { + $processedArguments = [$arguments[0]]; + + if (array_key_exists(1, $arguments) && null !== $arguments[1]) { + array_push($processedArguments, 'LIBRARYNAME', $arguments[1]); + } + + if (array_key_exists(2, $arguments) && true === $arguments[2]) { + $processedArguments[] = 'WITHCODE'; + } + + parent::setArguments($processedArguments); + } + + /** + * @param array $arguments + * @return void + */ + private function setLoadArguments(array $arguments): void + { + if (count($arguments) <= 2) { + parent::setArguments($arguments); + + return; + } + + $processedArguments = [$arguments[0]]; + $replace = array_pop($arguments); + + if (is_bool($replace) && $replace) { + $processedArguments[] = 'REPLACE'; + } elseif (!is_bool($replace)) { + $processedArguments[] = $replace; + } + + $processedArguments[] = $arguments[1]; + + parent::setArguments($processedArguments); + } + + /** + * @param array $arguments + * @return void + */ + private function setRestoreArguments(array $arguments): void + { + $processedArguments = [$arguments[0], $arguments[1]]; + + if (array_key_exists(2, $arguments) && null !== $arguments[2]) { + $processedArguments[] = strtoupper($arguments[2]); + } + + parent::setArguments($processedArguments); + } } diff --git a/src/Command/Strategy/ContainerCommands/Functions/DeleteStrategy.php b/src/Command/Strategy/ContainerCommands/Functions/DeleteStrategy.php deleted file mode 100644 index 250ae744..00000000 --- a/src/Command/Strategy/ContainerCommands/Functions/DeleteStrategy.php +++ /dev/null @@ -1,26 +0,0 @@ -separator = $separator; - } - - /** - * {@inheritDoc} - */ - public function resolve(string $commandId, string $subcommandId): SubcommandStrategyInterface - { - $subcommandStrategyClass = ucwords($subcommandId) . 'Strategy'; - $commandDirectoryName = ucwords($commandId); - - if (!is_null($this->separator)) { - $subcommandStrategyClass = str_replace($this->separator, '', $subcommandStrategyClass); - $commandDirectoryName = str_replace($this->separator, '', $commandDirectoryName); - } - - if (class_exists( - $containerCommandClass = self::CONTAINER_COMMANDS_NAMESPACE . '\\' . $commandDirectoryName . '\\' . $subcommandStrategyClass - )) { - return new $containerCommandClass(); - } - - throw new InvalidArgumentException('Non-existing container command given'); - } -} diff --git a/tests/Predis/Command/Strategy/ContainerCommands/Functions/DeleteStrategyTest.php b/tests/Predis/Command/Strategy/ContainerCommands/Functions/DeleteStrategyTest.php deleted file mode 100644 index 6169f8b0..00000000 --- a/tests/Predis/Command/Strategy/ContainerCommands/Functions/DeleteStrategyTest.php +++ /dev/null @@ -1,33 +0,0 @@ -strategy = new DeleteStrategy(); - } - - public function testProcessArguments(): void - { - $this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2'])); - } -} diff --git a/tests/Predis/Command/Strategy/ContainerCommands/Functions/DumpStrategyTest.php b/tests/Predis/Command/Strategy/ContainerCommands/Functions/DumpStrategyTest.php deleted file mode 100644 index 20b8fb96..00000000 --- a/tests/Predis/Command/Strategy/ContainerCommands/Functions/DumpStrategyTest.php +++ /dev/null @@ -1,37 +0,0 @@ -strategy = new DumpStrategy(); - } - - /** - * @group disconnected - * @return void - */ - public function testProcessArguments(): void - { - $this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2'])); - } -} diff --git a/tests/Predis/Command/Strategy/ContainerCommands/Functions/FlushStrategyTest.php b/tests/Predis/Command/Strategy/ContainerCommands/Functions/FlushStrategyTest.php deleted file mode 100644 index d5d8ba46..00000000 --- a/tests/Predis/Command/Strategy/ContainerCommands/Functions/FlushStrategyTest.php +++ /dev/null @@ -1,54 +0,0 @@ -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'], - ], - ]; - } -} diff --git a/tests/Predis/Command/Strategy/ContainerCommands/Functions/KillStrategyTest.php b/tests/Predis/Command/Strategy/ContainerCommands/Functions/KillStrategyTest.php deleted file mode 100644 index 96da8c5f..00000000 --- a/tests/Predis/Command/Strategy/ContainerCommands/Functions/KillStrategyTest.php +++ /dev/null @@ -1,37 +0,0 @@ -strategy = new KillStrategy(); - } - - /** - * @group disconnected - * @return void - */ - public function testProcessArguments(): void - { - $this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2'])); - } -} diff --git a/tests/Predis/Command/Strategy/ContainerCommands/Functions/ListStrategyTest.php b/tests/Predis/Command/Strategy/ContainerCommands/Functions/ListStrategyTest.php deleted file mode 100644 index 8e30c129..00000000 --- a/tests/Predis/Command/Strategy/ContainerCommands/Functions/ListStrategyTest.php +++ /dev/null @@ -1,62 +0,0 @@ -strategy = new ListStrategy(); - } - - /** - * @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' => [ - ['LIST', null, false], - ['LIST'], - ], - 'with LIBRARYNAME modifier' => [ - ['LIST', 'libraryname', false], - ['LIST', 'LIBRARYNAME', 'libraryname'], - ], - 'with WITHCODE modifier' => [ - ['LIST', null, true], - ['LIST', 'WITHCODE'], - ], - 'with all arguments' => [ - ['LIST', 'libraryname', true], - ['LIST', 'LIBRARYNAME', 'libraryname', 'WITHCODE'], - ], - ]; - } -} diff --git a/tests/Predis/Command/Strategy/ContainerCommands/Functions/LoadStrategyTest.php b/tests/Predis/Command/Strategy/ContainerCommands/Functions/LoadStrategyTest.php deleted file mode 100644 index 7d08daee..00000000 --- a/tests/Predis/Command/Strategy/ContainerCommands/Functions/LoadStrategyTest.php +++ /dev/null @@ -1,59 +0,0 @@ -strategy = new LoadStrategy(); - } - - /** - * @dataProvider argumentsProvider - * @param array $actualArguments - * @param array $expectedArguments - * @return void - */ - public function testProcessArgumentsReturnsCorrectArguments( - array $actualArguments, - array $expectedArguments - ): void { - $this->assertSame($expectedArguments, $this->strategy->processArguments($actualArguments)); - } - - public function argumentsProvider(): array - { - return [ - 'with less then or equal 2 arguments' => [ - ['arg1', 'arg2'], - ['arg1', 'arg2'], - ], - 'with last argument equals true' => [ - ['arg1', 'arg2', true], - ['arg1', 'REPLACE', 'arg2'], - ], - 'with last argument equals false' => [ - ['arg1', 'arg2', false], - ['arg1', 'arg2'], - ], - ]; - } -} diff --git a/tests/Predis/Command/Strategy/ContainerCommands/Functions/RestoreStrategyTest.php b/tests/Predis/Command/Strategy/ContainerCommands/Functions/RestoreStrategyTest.php deleted file mode 100644 index 839f14e8..00000000 --- a/tests/Predis/Command/Strategy/ContainerCommands/Functions/RestoreStrategyTest.php +++ /dev/null @@ -1,54 +0,0 @@ -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'], - ], - ]; - } -} diff --git a/tests/Predis/Command/Strategy/ContainerCommands/Functions/StatsStrategyTest.php b/tests/Predis/Command/Strategy/ContainerCommands/Functions/StatsStrategyTest.php deleted file mode 100644 index 6d68539b..00000000 --- a/tests/Predis/Command/Strategy/ContainerCommands/Functions/StatsStrategyTest.php +++ /dev/null @@ -1,37 +0,0 @@ -strategy = new StatsStrategy(); - } - - /** - * @group disconnected - * @return void - */ - public function testProcessArguments(): void - { - $this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2'])); - } -} diff --git a/tests/Predis/Command/Strategy/SubcommandStrategyResolverTest.php b/tests/Predis/Command/Strategy/SubcommandStrategyResolverTest.php deleted file mode 100644 index e4497d49..00000000 --- a/tests/Predis/Command/Strategy/SubcommandStrategyResolverTest.php +++ /dev/null @@ -1,57 +0,0 @@ -assertEquals($expectedStrategy, $resolver->resolve('functions', 'load')); - } - - /** - * @group disconnected - * @return void - */ - public function testResolveCorrectlyResolvesStrategyWithGivenWordSeparator(): void - { - $resolver = new SubcommandStrategyResolver('_'); - $expectedStrategy = new LoadStrategy(); - - $this->assertEquals($expectedStrategy, $resolver->resolve('functions_', 'load_')); - } - - /** - * @return void - */ - public function testResolveThrowsExceptionOnNonExistingStrategy(): void - { - $resolver = new SubcommandStrategyResolver(); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Non-existing container command given'); - - $resolver->resolve('foo', 'bar'); - } -}