Moved code from strategies to command (#1345)

This commit is contained in:
Vladyslav Vildanov
2023-07-26 15:32:39 +03:00
committed by GitHub
parent 9318232e8d
commit 7225b0af34
21 changed files with 95 additions and 791 deletions
+95 -15
View File
@@ -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);
}
}
@@ -1,26 +0,0 @@
<?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 DeleteStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
return $arguments;
}
}
@@ -1,26 +0,0 @@
<?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;
}
}
@@ -1,32 +0,0 @@
<?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;
}
}
@@ -1,26 +0,0 @@
<?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 KillStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
return $arguments;
}
}
@@ -1,36 +0,0 @@
<?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 ListStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
$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';
}
return $processedArguments;
}
}
@@ -1,41 +0,0 @@
<?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 LoadStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritdoc}
*/
public function processArguments(array $arguments): array
{
if (count($arguments) <= 2) {
return $arguments;
}
$processedArguments = [$arguments[0]];
$replace = array_pop($arguments);
if (is_bool($replace) && $replace) {
$processedArguments[] = 'REPLACE';
} elseif (!is_bool($replace)) {
$processedArguments[] = $replace;
}
$processedArguments[] = $arguments[1];
return $processedArguments;
}
}
@@ -1,32 +0,0 @@
<?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;
}
}
@@ -1,26 +0,0 @@
<?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 StatsStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
return $arguments;
}
}
@@ -1,25 +0,0 @@
<?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;
interface StrategyResolverInterface
{
/**
* Resolves subcommand strategy.
*
* @param string $commandId
* @param string $subcommandId
* @return SubcommandStrategyInterface
*/
public function resolve(string $commandId, string $subcommandId): SubcommandStrategyInterface;
}
@@ -1,24 +0,0 @@
<?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;
interface SubcommandStrategyInterface
{
/**
* Process arguments for given subcommand.
*
* @param array $arguments
* @return array
*/
public function processArguments(array $arguments): array;
}
@@ -1,52 +0,0 @@
<?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;
use InvalidArgumentException;
class SubcommandStrategyResolver implements StrategyResolverInterface
{
private const CONTAINER_COMMANDS_NAMESPACE = 'Predis\Command\Strategy\ContainerCommands';
/**
* @var ?string
*/
private $separator;
public function __construct(string $separator = null)
{
$this->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');
}
}
@@ -1,33 +0,0 @@
<?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 DeleteStrategyTest extends PredisTestCase
{
/**
* @var DeleteStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new DeleteStrategy();
}
public function testProcessArguments(): void
{
$this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2']));
}
}
@@ -1,37 +0,0 @@
<?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']));
}
}
@@ -1,54 +0,0 @@
<?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'],
],
];
}
}
@@ -1,37 +0,0 @@
<?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 KillStrategyTest extends PredisTestCase
{
/**
* @var KillStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new KillStrategy();
}
/**
* @group disconnected
* @return void
*/
public function testProcessArguments(): void
{
$this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2']));
}
}
@@ -1,62 +0,0 @@
<?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 ListStrategyTest extends PredisTestCase
{
/**
* @var ListStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->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'],
],
];
}
}
@@ -1,59 +0,0 @@
<?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 LoadStrategyTest extends PredisTestCase
{
/**
* @var LoadStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->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'],
],
];
}
}
@@ -1,54 +0,0 @@
<?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'],
],
];
}
}
@@ -1,37 +0,0 @@
<?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 StatsStrategyTest extends PredisTestCase
{
/**
* @var StatsStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new StatsStrategy();
}
/**
* @group disconnected
* @return void
*/
public function testProcessArguments(): void
{
$this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2']));
}
}
@@ -1,57 +0,0 @@
<?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;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Predis\Command\Strategy\ContainerCommands\Functions\LoadStrategy;
class SubcommandStrategyResolverTest extends TestCase
{
/**
* @group disconnected
* @return void
*/
public function testResolveCorrectStrategy(): void
{
$resolver = new SubcommandStrategyResolver();
$expectedStrategy = new LoadStrategy();
$this->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');
}
}