Skip to content

Commit

Permalink
fix: ensure unknown commands are caught before retrieving anything fr…
Browse files Browse the repository at this point in the history
…om container

Updates `AbstractContainerCommandLoader::hasCommand()` to test if the `$name` exists in the `$commandMap` before attempting to check the container.
If not, it immediately returns false.

Fixes #64

Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
  • Loading branch information
weierophinney committed Feb 26, 2021
1 parent 91d5c70 commit d3fd1e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/AbstractContainerCommandLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Webmozart\Assert\Assert;

use function array_key_exists;
use function array_keys;
use function sprintf;

Expand Down Expand Up @@ -61,6 +62,10 @@ protected function getCommand(string $name): Command

protected function hasCommand(string $name): bool
{
if (! array_key_exists($name, $this->commandMap)) {
return false;
}

if ($this->container->has($this->commandMap[$name])) {
return true;
}
Expand Down
11 changes: 11 additions & 0 deletions test/ContainerCommandLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,15 @@ public function testAnExceptionIsThrownWhenACommandAbsentFromTheContainerCannotB

$this->fail('An exception was not thrown');
}

public function testLoaderReturnsFalseWhenTestingCommandThatDoesNotExist(): void
{
$container = $this->createMock(ContainerInterface::class);
$container->expects(self::never())
->method('has');

$loader = new ContainerCommandLoader($container, []);

$this->assertFalse($loader->has('my:command'));
}
}

0 comments on commit d3fd1e7

Please sign in to comment.