diff --git a/Application.php b/Application.php index f3914bb78..80bbf876b 100644 --- a/Application.php +++ b/Application.php @@ -494,6 +494,11 @@ public function get(string $name) 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/Tests/ApplicationTest.php b/Tests/ApplicationTest.php index 38b7bf76c..8d25a88f3 100644 --- a/Tests/ApplicationTest.php +++ b/Tests/ApplicationTest.php @@ -1788,6 +1788,20 @@ public function testThrowingErrorListener() $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