Fix incorrect order of arguments when added out of sequence.

Fixes #3385
This commit is contained in:
mark_story 2012-11-20 22:42:44 -05:00
parent 04d4abfa53
commit 587c7071b4
2 changed files with 21 additions and 1 deletions

View file

@ -345,6 +345,7 @@ class ConsoleOptionParser {
$arg = new ConsoleInputArgument($options); $arg = new ConsoleInputArgument($options);
} }
$this->_args[$index] = $arg; $this->_args[$index] = $arg;
ksort($this->_args);
return $this; return $this;
} }

View file

@ -314,10 +314,29 @@ class ConsoleOptionParserTest extends CakeTestCase {
$parser = new ConsoleOptionParser('test', false); $parser = new ConsoleOptionParser('test', false);
$parser->addArgument(new ConsoleInputArgument('test')); $parser->addArgument(new ConsoleInputArgument('test'));
$result = $parser->arguments(); $result = $parser->arguments();
$this->assertEquals(1, count($result)); $this->assertCount(1, $result);
$this->assertEquals('test', $result[0]->name()); $this->assertEquals('test', $result[0]->name());
} }
/**
* Test adding arguments out of order.
*
* @return void
*/
public function testAddArgumentOutOfOrder() {
$parser = new ConsoleOptionParser('test', false);
$parser->addArgument('name', array('index' => 1, 'help' => 'first argument'))
->addArgument('bag', array('index' => 2, 'help' => 'second argument'))
->addArgument('other', array('index' => 0, 'help' => 'Zeroth argument'));
$result = $parser->arguments();
$this->assertCount(3, $result);
$this->assertEquals('other', $result[0]->name());
$this->assertEquals('name', $result[1]->name());
$this->assertEquals('bag', $result[2]->name());
$this->assertSame(array(0, 1, 2), array_keys($result));
}
/** /**
* test overwriting positional arguments. * test overwriting positional arguments.
* *