Skip to content
Permalink
Browse files

Fix missing check.

We need to check the collection generated by both the app and plugin
hooks, as we don't yet have return types.
  • Loading branch information...
markstory committed Mar 9, 2018
1 parent dbccf7d commit c1958d51005f768122caba6bcb0be9e58d31d28c
@@ -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.
*
@@ -245,7 +245,8 @@ public function testAutoDiscoverCore()
*/
public function testDiscoverPluginUnknown()
{
$this->assertSame([], $collection = new CommandCollection());
$collection = new CommandCollection();
$this->assertSame([], $collection->discoverPlugin('Nope'));
}
/**
@@ -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
*

0 comments on commit c1958d5

Please sign in to comment.
You can’t perform that action at this time.