Browse Source

Merge pull request #10846 from cakephp/buildcommand-event

3.next Add Console.buildCommands event
José Lorenzo Rodríguez 8 years ago
parent
commit
3a52dfcbe0
2 changed files with 41 additions and 0 deletions
  1. 12 0
      src/Console/CommandRunner.php
  2. 29 0
      tests/TestCase/Console/CommandRunnerTest.php

+ 12 - 0
src/Console/CommandRunner.php

@@ -19,6 +19,7 @@ use Cake\Console\CommandCollectionAwareInterface;
 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;
@@ -29,6 +30,8 @@ use RuntimeException;
  */
 class CommandRunner
 {
+    use EventManagerTrait;
+
     /**
      * The application console commands are being run for.
      *
@@ -93,6 +96,13 @@ class CommandRunner
     /**
      * 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.
@@ -114,6 +124,8 @@ class CommandRunner
                 " Got '{$type}' instead."
             );
         }
+        $this->dispatchEvent('Console.buildCommands', ['commands' => $commands]);
+
         if (empty($argv) || $argv[0] !== $this->root) {
             $command = empty($argv) ? '' : " `{$argv[0]}`";
             throw new RuntimeException(

+ 29 - 0
tests/TestCase/Console/CommandRunnerTest.php

@@ -30,6 +30,13 @@ use TestApp\Shell\SampleShell;
 class CommandRunnerTest extends TestCase
 {
     /**
+     * Tracking property for event triggering
+     *
+     * @var bool
+     */
+    protected $eventTriggered = false;
+
+    /**
      * setup
      *
      * @return void
@@ -265,6 +272,28 @@ class CommandRunnerTest extends TestCase
         $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)