From 90da582efc88d5168fa126f1c3527733d81690eb Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Wed, 21 Jun 2023 12:00:58 +0300 Subject: [PATCH] Fixed bug with incorrect multiple words processing (#1325) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed bug with incorrect multiple words processing * Convert subcommand string to lower case * Update SubcommandStrategyResolver.php * Added test coverage * Codestyle fixes --------- Co-authored-by: Till Krüss --- src/Command/Redis/FUNCTIONS.php | 2 +- .../Strategy/SubcommandStrategyResolver.php | 19 ++++++++++-- .../SubcommandStrategyResolverTest.php | 30 +++++++++++-------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/Command/Redis/FUNCTIONS.php b/src/Command/Redis/FUNCTIONS.php index 69465901..7f4fde79 100644 --- a/src/Command/Redis/FUNCTIONS.php +++ b/src/Command/Redis/FUNCTIONS.php @@ -41,7 +41,7 @@ class FUNCTIONS extends RedisCommand public function setArguments(array $arguments) { - $strategy = $this->strategyResolver->resolve('functions', $arguments[0]); + $strategy = $this->strategyResolver->resolve('functions', strtolower($arguments[0])); $arguments = $strategy->processArguments($arguments); parent::setArguments($arguments); diff --git a/src/Command/Strategy/SubcommandStrategyResolver.php b/src/Command/Strategy/SubcommandStrategyResolver.php index 6d1c2016..cda84268 100644 --- a/src/Command/Strategy/SubcommandStrategyResolver.php +++ b/src/Command/Strategy/SubcommandStrategyResolver.php @@ -18,13 +18,28 @@ 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 = ucfirst(strtolower($subcommandId)) . 'Strategy'; - $commandDirectoryName = ucfirst(strtolower($commandId)); + $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 diff --git a/tests/Predis/Command/Strategy/SubcommandStrategyResolverTest.php b/tests/Predis/Command/Strategy/SubcommandStrategyResolverTest.php index 3532809c..e4497d49 100644 --- a/tests/Predis/Command/Strategy/SubcommandStrategyResolverTest.php +++ b/tests/Predis/Command/Strategy/SubcommandStrategyResolverTest.php @@ -19,23 +19,27 @@ use Predis\Command\Strategy\ContainerCommands\Functions\LoadStrategy; class SubcommandStrategyResolverTest extends TestCase { /** - * @var StrategyResolverInterface - */ - private $resolver; - - protected function setUp(): void - { - $this->resolver = new SubcommandStrategyResolver(); - } - - /** + * @group disconnected * @return void */ public function testResolveCorrectStrategy(): void { + $resolver = new SubcommandStrategyResolver(); $expectedStrategy = new LoadStrategy(); - $this->assertEquals($expectedStrategy, $this->resolver->resolve('functions', 'load')); + $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_')); } /** @@ -43,9 +47,11 @@ class SubcommandStrategyResolverTest extends TestCase */ public function testResolveThrowsExceptionOnNonExistingStrategy(): void { + $resolver = new SubcommandStrategyResolver(); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Non-existing container command given'); - $this->resolver->resolve('foo', 'bar'); + $resolver->resolve('foo', 'bar'); } }