Skip to content

Commit

Permalink
feature #19887 Sort alternatives alphabetically when a command is not…
Browse files Browse the repository at this point in the history
… found (javiereguiluz)

This PR was squashed before being merged into the 3.3-dev branch (closes #19887).

Discussion
----------

Sort alternatives alphabetically when a command is not found

| Q | A |
| --- | --- |
| Branch? | master |
| Bug fix? | no |
| New feature? | yes |
| BC breaks? | no |
| Deprecations? | no |
| Tests pass? | yes |
| Fixed tickets | #10893 |
| License | MIT |
| Doc PR | - |

Commits
-------

ba6c946 Sort commands like a human would do
f04b1bd Sort alternatives alphabetically when a command is not found
  • Loading branch information
fabpot committed Mar 22, 2017
2 parents 59dd752 + ba6c946 commit 0a17358
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Application.php
Expand Up @@ -1023,7 +1023,7 @@ private function findAlternatives($name, $collection)
}

$alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; });
asort($alternatives);
ksort($alternatives, SORT_NATURAL | SORT_FLAG_CASE);

return array_keys($alternatives);
}
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -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();
Expand Down

0 comments on commit 0a17358

Please sign in to comment.