diff --git a/src/Console/CommandRunner.php b/src/Console/CommandRunner.php index a5b40e94ced..d57609fffa7 100644 --- a/src/Console/CommandRunner.php +++ b/src/Console/CommandRunner.php @@ -19,6 +19,7 @@ use Cake\Console\Exception\StopException; use Cake\Console\Shell; use Cake\Http\BaseApplication; +use Cake\Shell\VersionShell; use RuntimeException; /** @@ -40,6 +41,13 @@ class CommandRunner */ protected $root; + /** + * Alias mappings. + * + * @var array + */ + protected $aliases = []; + /** * Constructor * @@ -50,6 +58,26 @@ public function __construct(BaseApplication $app, $root = 'cake') { $this->app = $app; $this->root = $root; + $this->aliases = [ + '--version' => 'version' + ]; + } + + /** + * Replace the alias map for a runner. + * + * Aliases allow you to define alternate names for commands + * in the collection. This can be useful to add top level switches + * like `--version` or `-h` + * + * @param array $aliases The map of aliases to replace. + * @return $this + */ + public function setAliases(array $aliases) + { + $this->aliases = $aliases; + + return $this; } /** @@ -64,7 +92,10 @@ public function run(array $argv, ConsoleIo $io = null) { $this->app->bootstrap(); - $commands = $this->app->console(new CommandCollection()); + $commands = new CommandCollection([ + 'version' => VersionShell::class, + ]); + $commands = $this->app->console($commands); if (!($commands instanceof CommandCollection)) { $type = is_object($commands) ? get_class($commands) : gettype($commands); throw new RuntimeException( @@ -111,6 +142,9 @@ public function run(array $argv, ConsoleIo $io = null) */ protected function getShell(ConsoleIo $io, CommandCollection $commands, $name) { + if (isset($this->aliases[$name])) { + $name = $this->aliases[$name]; + } if (!$commands->has($name)) { throw new RuntimeException( "Unknown command `{$this->root} {$name}`." . diff --git a/src/Shell/VersionShell.php b/src/Shell/VersionShell.php new file mode 100644 index 00000000000..3da0597b406 --- /dev/null +++ b/src/Shell/VersionShell.php @@ -0,0 +1,35 @@ +out(Configure::version()); + } +} diff --git a/tests/TestCase/Console/CommandRunnerTest.php b/tests/TestCase/Console/CommandRunnerTest.php index b01c65bd79e..bad006d54c7 100644 --- a/tests/TestCase/Console/CommandRunnerTest.php +++ b/tests/TestCase/Console/CommandRunnerTest.php @@ -138,19 +138,17 @@ public function testRunHelpShortOption() * * @return void */ - public function testRunVersionLongOption() + public function testRunVersionAlias() { - $this->markTestIncomplete(); - } + $app = $this->getMockBuilder(BaseApplication::class) + ->setMethods(['middleware', 'bootstrap']) + ->setConstructorArgs([$this->config]) + ->getMock(); - /** - * Test using `cake -v` invokes the version command - * - * @return void - */ - public function testRunVersionShortOption() - { - $this->markTestIncomplete(); + $output = new ConsoleOutput(); + $runner = new CommandRunner($app, 'cake'); + $result = $runner->run(['cake', '--version'], $this->getMockIo($output)); + $this->assertContains(Configure::version(), $output->messages()[0]); } /**