Skip to content

Commit

Permalink
Merge pull request #10846 from cakephp/buildcommand-event
Browse files Browse the repository at this point in the history
3.next Add Console.buildCommands event
  • Loading branch information
lorenzo committed Jul 4, 2017
2 parents 3fc705f + ee31059 commit 3a52dfc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Console/CommandRunner.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Console\ConsoleIo;
use Cake\Console\Exception\StopException;
use Cake\Console\Shell;
use Cake\Event\EventManagerTrait;
use Cake\Http\BaseApplication;
use Cake\Shell\HelpShell;
use Cake\Shell\VersionShell;
Expand All @@ -29,6 +30,8 @@
*/
class CommandRunner
{
use EventManagerTrait;

/**
* The application console commands are being run for.
*
Expand Down Expand Up @@ -93,6 +96,13 @@ public function setAliases(array $aliases)
/**
* Run the command contained in $argv.
*
* Use the application to do the following:
*
* - Bootstrap the application
* - Create the CommandCollection using the console() hook on the application.
* - Trigger the `Console.buildCommands` event of auto-wiring plugins.
* - Run the requested command.
*
* @param array $argv The arguments from the CLI environment.
* @param \Cake\Console\ConsoleIo $io The ConsoleIo instance. Used primarily for testing.
* @return int The exit code of the command.
Expand All @@ -114,6 +124,8 @@ public function run(array $argv, ConsoleIo $io = null)
" Got '{$type}' instead."
);
}
$this->dispatchEvent('Console.buildCommands', ['commands' => $commands]);

if (empty($argv) || $argv[0] !== $this->root) {
$command = empty($argv) ? '' : " `{$argv[0]}`";
throw new RuntimeException(
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/Console/CommandRunnerTest.php
Expand Up @@ -29,6 +29,13 @@
*/
class CommandRunnerTest extends TestCase
{
/**
* Tracking property for event triggering
*
* @var bool
*/
protected $eventTriggered = false;

/**
* setup
*
Expand Down Expand Up @@ -265,6 +272,28 @@ public function testRunRootNamePropagates()
$this->assertNotContains('cake sample [-h]', $result);
}

/**
* Test that run() fires off the buildCommands event.
*
* @return void
*/
public function testRunTriggersBuildCommandsEvent()
{
$app = $this->getMockBuilder(BaseApplication::class)
->setMethods(['middleware', 'bootstrap'])
->setConstructorArgs([$this->config])
->getMock();

$output = new ConsoleOutput();
$runner = new CommandRunner($app, 'cake');
$runner->getEventManager()->on('Console.buildCommands', function ($event, $commands) {
$this->assertInstanceOf(CommandCollection::class, $commands);
$this->eventTriggered = true;
});
$result = $runner->run(['cake', '--version'], $this->getMockIo($output));
$this->assertTrue($this->eventTriggered, 'Should have triggered event.');
}

protected function getMockIo($output)
{
$io = $this->getMockBuilder(ConsoleIo::class)
Expand Down

0 comments on commit 3a52dfc

Please sign in to comment.