Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f04b1bd

Browse files
committed
Sort alternatives alphabetically when a command is not found
1 parent e7c12d3 commit f04b1bd

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ private function findAlternatives($name, $collection)
10191019
}
10201020

10211021
$alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; });
1022-
asort($alternatives);
1022+
ksort($alternatives);
10231023

10241024
return array_keys($alternatives);
10251025
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,36 @@ public function testFindAlternativeNamespace()
477477
}
478478
}
479479

480+
public function testFindAlternativesOutput()
481+
{
482+
$application = new Application();
483+
484+
$application->add(new \FooCommand());
485+
$application->add(new \Foo1Command());
486+
$application->add(new \Foo2Command());
487+
$application->add(new \Foo3Command());
488+
489+
$expectedAlternatives = array(
490+
'afoobar',
491+
'afoobar1',
492+
'afoobar2',
493+
'foo1:bar',
494+
'foo3:bar',
495+
'foo:bar',
496+
'foo:bar1',
497+
);
498+
499+
try {
500+
$application->find('foo');
501+
$this->fail('->find() throws a CommandNotFoundException if command is not defined');
502+
} catch (\Exception $e) {
503+
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command is not defined');
504+
$this->assertSame($expectedAlternatives, $e->getAlternatives());
505+
506+
$this->assertRegExp('/Command "foo" is not defined\..*Did you mean one of these\?.*/Ums', $e->getMessage());
507+
}
508+
}
509+
480510
public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces()
481511
{
482512
$application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getNamespaces'))->getMock();

0 commit comments

Comments
 (0)