diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 199ed6804c..81e8f9656d 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -513,6 +513,11 @@ class Application implements ResetInterface throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name)); } + // When the command has a different name than the one used at the command loader level + if (!isset($this->commands[$name])) { + throw new CommandNotFoundException(sprintf('The "%s" command cannot be found because it is registered under multiple names. Make sure you don\'t set a different name via constructor or "setName()".', $name)); + } + $command = $this->commands[$name]; if ($this->wantHelps) { diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 247158d043..b3d33629da 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -1802,6 +1802,20 @@ class ApplicationTest extends TestCase $tester = new ApplicationTester($application); $tester->run(['command' => 'foo']); } + + public function testCommandNameMismatchWithCommandLoaderKeyThrows() + { + $this->expectException(CommandNotFoundException::class); + $this->expectExceptionMessage('The "test" command cannot be found because it is registered under multiple names. Make sure you don\'t set a different name via constructor or "setName()".'); + + $app = new Application(); + $loader = new FactoryCommandLoader([ + 'test' => static function () { return new Command('test-command'); }, + ]); + + $app->setCommandLoader($loader); + $app->get('test'); + } } class CustomApplication extends Application