Skip to content

Commit

Permalink
Fixed bug with incorrect multiple words processing (#1325)
Browse files Browse the repository at this point in the history
* 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 <tillkruss@users.noreply.github.com>
  • Loading branch information
vladvildanov and tillkruss authored Jun 21, 2023
1 parent 732abc8 commit 90da582
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Command/Redis/FUNCTIONS.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getId()

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);
Expand Down
19 changes: 17 additions & 2 deletions src/Command/Strategy/SubcommandStrategyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions tests/Predis/Command/Strategy/SubcommandStrategyResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,39 @@
class SubcommandStrategyResolverTest extends TestCase
{
/**
* @var StrategyResolverInterface
* @group disconnected
* @return void
*/
private $resolver;

protected function setUp(): void
public function testResolveCorrectStrategy(): void
{
$this->resolver = new SubcommandStrategyResolver();
$resolver = new SubcommandStrategyResolver();
$expectedStrategy = new LoadStrategy();

$this->assertEquals($expectedStrategy, $resolver->resolve('functions', 'load'));
}

/**
* @group disconnected
* @return void
*/
public function testResolveCorrectStrategy(): void
public function testResolveCorrectlyResolvesStrategyWithGivenWordSeparator(): void
{
$resolver = new SubcommandStrategyResolver('_');
$expectedStrategy = new LoadStrategy();

$this->assertEquals($expectedStrategy, $this->resolver->resolve('functions', 'load'));
$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');

$this->resolver->resolve('foo', 'bar');
$resolver->resolve('foo', 'bar');
}
}

0 comments on commit 90da582

Please sign in to comment.