From f04b1bd72f73abcbb54b742a8c3033947ec6b422 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 8 Sep 2016 11:20:46 +0200 Subject: [PATCH 1/2] Sort alternatives alphabetically when a command is not found --- src/Symfony/Component/Console/Application.php | 2 +- .../Console/Tests/ApplicationTest.php | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 210cd15ea4fbd..c1cd446486eed 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -1019,7 +1019,7 @@ private function findAlternatives($name, $collection) } $alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; }); - asort($alternatives); + ksort($alternatives); return array_keys($alternatives); } diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 47f0844e0beec..a1302da55ea3e 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -477,6 +477,36 @@ public function testFindAlternativeNamespace() } } + public function testFindAlternativesOutput() + { + $application = new Application(); + + $application->add(new \FooCommand()); + $application->add(new \Foo1Command()); + $application->add(new \Foo2Command()); + $application->add(new \Foo3Command()); + + $expectedAlternatives = array( + 'afoobar', + 'afoobar1', + 'afoobar2', + 'foo1:bar', + 'foo3:bar', + 'foo:bar', + 'foo:bar1', + ); + + try { + $application->find('foo'); + $this->fail('->find() throws a CommandNotFoundException if command is not defined'); + } catch (\Exception $e) { + $this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command is not defined'); + $this->assertSame($expectedAlternatives, $e->getAlternatives()); + + $this->assertRegExp('/Command "foo" is not defined\..*Did you mean one of these\?.*/Ums', $e->getMessage()); + } + } + public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces() { $application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getNamespaces'))->getMock(); From ba6c9464eaa9f2c75971fd061ed2cc926261c0ca Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 8 Sep 2016 11:37:22 +0200 Subject: [PATCH 2/2] Sort commands like a human would do --- src/Symfony/Component/Console/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index c1cd446486eed..fecf3911a2a5c 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -1019,7 +1019,7 @@ private function findAlternatives($name, $collection) } $alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; }); - ksort($alternatives); + ksort($alternatives, SORT_NATURAL | SORT_FLAG_CASE); return array_keys($alternatives); }