Skip to content

Commit c1958d5

Browse files
committed
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.
1 parent dbccf7d commit c1958d5

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/Console/CommandRunner.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,12 @@ public function run(array $argv, ConsoleIo $io = null)
138138
'help' => HelpCommand::class,
139139
]);
140140
$commands = $this->app->console($commands);
141+
$this->checkCollection($commands, 'console');
142+
141143
if ($this->app instanceof PluginApplicationInterface) {
142144
$commands = $this->app->pluginConsole($commands);
143145
}
144-
145-
if (!($commands instanceof CommandCollection)) {
146-
$type = getTypeName($commands);
147-
throw new RuntimeException(
148-
"The application's `console` method did not return a CommandCollection." .
149-
" Got '{$type}' instead."
150-
);
151-
}
146+
$this->checkCollection($commands, 'pluginConsole');
152147
$this->dispatchEvent('Console.buildCommands', ['commands' => $commands]);
153148

154149
if (empty($argv)) {
@@ -196,6 +191,26 @@ protected function bootstrap()
196191
}
197192
}
198193

194+
/**
195+
* Check the created CommandCollection
196+
*
197+
* @param mixed $commands The CommandCollection to check, could be anything though.
198+
* @param string $method The method that was used.
199+
* @return void
200+
* @throws \RuntimeException
201+
* @deprecated 3.6.0 This method should be replaced with return types in 4.x
202+
*/
203+
protected function checkCollection($commands, $method)
204+
{
205+
if (!($commands instanceof CommandCollection)) {
206+
$type = getTypeName($commands);
207+
throw new RuntimeException(
208+
"The application's `{$method}` method did not return a CommandCollection." .
209+
" Got '{$type}' instead."
210+
);
211+
}
212+
}
213+
199214
/**
200215
* Get the application's event manager or the global one.
201216
*

tests/TestCase/Console/CommandCollectionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ public function testAutoDiscoverCore()
245245
*/
246246
public function testDiscoverPluginUnknown()
247247
{
248-
$this->assertSame([], $collection = new CommandCollection());
248+
$collection = new CommandCollection();
249+
$this->assertSame([], $collection->discoverPlugin('Nope'));
249250
}
250251

251252
/**

tests/TestCase/Console/CommandRunnerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ public function testRunConsoleHookFailure()
132132
$runner->run(['cake', '-h']);
133133
}
134134

135+
/**
136+
* Test that the console hook not returning a command collection
137+
* raises an error.
138+
*
139+
* @return void
140+
*/
141+
public function testRunPluginConsoleHookFailure()
142+
{
143+
$this->expectException(\RuntimeException::class);
144+
$this->expectExceptionMessage('The application\'s `pluginConsole` method did not return a CommandCollection.');
145+
$app = $this->getMockBuilder(BaseApplication::class)
146+
->setMethods(['pluginConsole', 'middleware', 'bootstrap'])
147+
->setConstructorArgs([$this->config])
148+
->getMock();
149+
$runner = new CommandRunner($app);
150+
$runner->run(['cake', '-h']);
151+
}
152+
135153
/**
136154
* Test that running with empty argv fails
137155
*

0 commit comments

Comments
 (0)