Skip to content

Commit

Permalink
ConsoleOptionParser addSubcommand Sorting - #11838
Browse files Browse the repository at this point in the history
Implemented an optional sort parameter to \Cake\Console\ConsoleOptionParser::addSubcommand
Added unit test for the new parameter.
  • Loading branch information
t73biz committed Mar 18, 2018
1 parent 235c724 commit 9922197
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Console/ConsoleOptionParser.php
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Console/ConsoleOptionParserTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 9922197

Please sign in to comment.