Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 c1958d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/Console/CommandRunner.php
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Console/CommandCollectionTest.php
Expand Up @@ -245,7 +245,8 @@ public function testAutoDiscoverCore()
*/
public function testDiscoverPluginUnknown()
{
$this->assertSame([], $collection = new CommandCollection());
$collection = new CommandCollection();
$this->assertSame([], $collection->discoverPlugin('Nope'));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/Console/CommandRunnerTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit c1958d5

Please sign in to comment.