Skip to content

Commit 587c707

Browse files
committed
Fix incorrect order of arguments when added out of sequence.
Fixes #3385
1 parent 04d4abf commit 587c707

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/Cake/Console/ConsoleOptionParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ public function addArgument($name, $params = array()) {
345345
$arg = new ConsoleInputArgument($options);
346346
}
347347
$this->_args[$index] = $arg;
348+
ksort($this->_args);
348349
return $this;
349350
}
350351

lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,29 @@ public function testAddArgumentObject() {
314314
$parser = new ConsoleOptionParser('test', false);
315315
$parser->addArgument(new ConsoleInputArgument('test'));
316316
$result = $parser->arguments();
317-
$this->assertEquals(1, count($result));
317+
$this->assertCount(1, $result);
318318
$this->assertEquals('test', $result[0]->name());
319319
}
320320

321+
/**
322+
* Test adding arguments out of order.
323+
*
324+
* @return void
325+
*/
326+
public function testAddArgumentOutOfOrder() {
327+
$parser = new ConsoleOptionParser('test', false);
328+
$parser->addArgument('name', array('index' => 1, 'help' => 'first argument'))
329+
->addArgument('bag', array('index' => 2, 'help' => 'second argument'))
330+
->addArgument('other', array('index' => 0, 'help' => 'Zeroth argument'));
331+
332+
$result = $parser->arguments();
333+
$this->assertCount(3, $result);
334+
$this->assertEquals('other', $result[0]->name());
335+
$this->assertEquals('name', $result[1]->name());
336+
$this->assertEquals('bag', $result[2]->name());
337+
$this->assertSame(array(0, 1, 2), array_keys($result));
338+
}
339+
321340
/**
322341
* test overwriting positional arguments.
323342
*

0 commit comments

Comments
 (0)