diff --git a/src/Console/CommandRunner.php b/src/Console/CommandRunner.php index c737768aee1..de66afbd3f5 100644 --- a/src/Console/CommandRunner.php +++ b/src/Console/CommandRunner.php @@ -138,17 +138,12 @@ public function run(array $argv, ConsoleIo $io = null) 'help' => HelpCommand::class, ]); $commands = $this->app->console($commands); + $this->checkCollection($commands, 'console'); + if ($this->app instanceof PluginApplicationInterface) { $commands = $this->app->pluginConsole($commands); } - - if (!($commands instanceof CommandCollection)) { - $type = getTypeName($commands); - throw new RuntimeException( - "The application's `console` method did not return a CommandCollection." . - " Got '{$type}' instead." - ); - } + $this->checkCollection($commands, 'pluginConsole'); $this->dispatchEvent('Console.buildCommands', ['commands' => $commands]); if (empty($argv)) { @@ -196,6 +191,26 @@ protected function bootstrap() } } + /** + * Check the created CommandCollection + * + * @param mixed $commands The CommandCollection to check, could be anything though. + * @param string $method The method that was used. + * @return void + * @throws \RuntimeException + * @deprecated 3.6.0 This method should be replaced with return types in 4.x + */ + protected function checkCollection($commands, $method) + { + if (!($commands instanceof CommandCollection)) { + $type = getTypeName($commands); + throw new RuntimeException( + "The application's `{$method}` method did not return a CommandCollection." . + " Got '{$type}' instead." + ); + } + } + /** * Get the application's event manager or the global one. * diff --git a/tests/TestCase/Console/CommandCollectionTest.php b/tests/TestCase/Console/CommandCollectionTest.php index a52cc455065..4a6747a275f 100644 --- a/tests/TestCase/Console/CommandCollectionTest.php +++ b/tests/TestCase/Console/CommandCollectionTest.php @@ -245,7 +245,8 @@ public function testAutoDiscoverCore() */ public function testDiscoverPluginUnknown() { - $this->assertSame([], $collection = new CommandCollection()); + $collection = new CommandCollection(); + $this->assertSame([], $collection->discoverPlugin('Nope')); } /** diff --git a/tests/TestCase/Console/CommandRunnerTest.php b/tests/TestCase/Console/CommandRunnerTest.php index e6df124428f..ffed6405616 100644 --- a/tests/TestCase/Console/CommandRunnerTest.php +++ b/tests/TestCase/Console/CommandRunnerTest.php @@ -132,6 +132,24 @@ public function testRunConsoleHookFailure() $runner->run(['cake', '-h']); } + /** + * Test that the console hook not returning a command collection + * raises an error. + * + * @return void + */ + public function testRunPluginConsoleHookFailure() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('The application\'s `pluginConsole` method did not return a CommandCollection.'); + $app = $this->getMockBuilder(BaseApplication::class) + ->setMethods(['pluginConsole', 'middleware', 'bootstrap']) + ->setConstructorArgs([$this->config]) + ->getMock(); + $runner = new CommandRunner($app); + $runner->run(['cake', '-h']); + } + /** * Test that running with empty argv fails *