From 99221975b11774438150e1baa3b543e19bb62977 Mon Sep 17 00:00:00 2001 From: Ronald Chaplin Date: Sat, 17 Mar 2018 21:40:11 -0400 Subject: [PATCH] ConsoleOptionParser addSubcommand Sorting - #11838 Implemented an optional sort parameter to \Cake\Console\ConsoleOptionParser::addSubcommand Added unit test for the new parameter. --- src/Console/ConsoleOptionParser.php | 7 +++++-- tests/TestCase/Console/ConsoleOptionParserTest.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Console/ConsoleOptionParser.php b/src/Console/ConsoleOptionParser.php index 702e68524fd..94375004024 100644 --- a/src/Console/ConsoleOptionParser.php +++ b/src/Console/ConsoleOptionParser.php @@ -576,9 +576,10 @@ public function addOptions(array $options) * * @param \Cake\Console\ConsoleInputSubcommand|string $name Name of the subcommand. Will also accept an instance of ConsoleInputSubcommand * @param array $options Array of params, see above. + * @param boolean $sort * @return $this */ - public function addSubcommand($name, array $options = []) + public function addSubcommand($name, array $options = [], $sort = true) { if ($name instanceof ConsoleInputSubcommand) { $command = $name; @@ -595,7 +596,9 @@ public function addSubcommand($name, array $options = []) $command = new ConsoleInputSubcommand($options); } $this->_subcommands[$name] = $command; - asort($this->_subcommands); + if ($sort) { + asort($this->_subcommands); + } return $this; } diff --git a/tests/TestCase/Console/ConsoleOptionParserTest.php b/tests/TestCase/Console/ConsoleOptionParserTest.php index 03c582a181d..ecd8a400f2b 100644 --- a/tests/TestCase/Console/ConsoleOptionParserTest.php +++ b/tests/TestCase/Console/ConsoleOptionParserTest.php @@ -651,6 +651,20 @@ public function testAddSubcommandObject() $this->assertEquals('test', $result['test']->name()); } + /** + * test addSubcommand without sorting applied. + */ + public function testAddSubcommandSort() + { + $parser = new ConsoleOptionParser('test', false); + $parser->addSubcommand(new ConsoleInputSubcommand('betaTest'), [], false); + $parser->addSubcommand(new ConsoleInputSubcommand('alphaTest'), [], false); + $result = $parser->subcommands(); + $this->assertCount(2, $result); + $firstResult = key($result); + $this->assertEquals('betaTest', $firstResult); + } + /** * test removeSubcommand with an object. *