Skip to content

Commit 4a38b3c

Browse files
Amrouche HamzaSimperfit
Amrouche Hamza
authored andcommitted
[Console] Commands with an alias should not be recognized as ambiguous
1 parent 78d86f8 commit 4a38b3c

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,21 @@ public function find($name)
494494
{
495495
$this->init();
496496
$aliases = array();
497+
498+
foreach ($this->commands as $command) {
499+
foreach ($command->getAliases() as $alias) {
500+
if (!isset($this->commands[$alias])) {
501+
$this->commands[$alias] = $command;
502+
}
503+
}
504+
}
505+
497506
$allCommands = array_keys($this->commands);
507+
508+
if (isset($this->commands[$name])) {
509+
return $this->get($name);
510+
}
511+
498512
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
499513
$commands = preg_grep('{^'.$expr.'}', $allCommands);
500514

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public static function setUpBeforeClass()
4949
require_once self::$fixturesPath.'/BarBucCommand.php';
5050
require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
5151
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
52-
require_once self::$fixturesPath.'/TestTiti.php';
53-
require_once self::$fixturesPath.'/TestToto.php';
52+
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering.php';
53+
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering2.php';
5454
}
5555

5656
protected function normalizeLineBreaks($text)
@@ -122,6 +122,27 @@ public function testRegister()
122122
$this->assertEquals('foo', $command->getName(), '->register() registers a new command');
123123
}
124124

125+
public function testRegisterAmbiguous()
126+
{
127+
$code = function (InputInterface $input, OutputInterface $output) {
128+
$output->writeln('It works!');
129+
};
130+
131+
$application = new Application();
132+
$application
133+
->register('test-foo')
134+
->setAliases(array('test'))
135+
->setCode($code);
136+
137+
$application
138+
->register('test-bar')
139+
->setCode($code);
140+
141+
$tester = new ApplicationTester($application);
142+
$tester->run(array('test'));
143+
$this->assertContains('It works!', $tester->getDisplay(true));
144+
}
145+
125146
public function testAdd()
126147
{
127148
$application = new Application();
@@ -231,9 +252,9 @@ public function testFindAmbiguousNamespace()
231252
public function testFindNonAmbiguous()
232253
{
233254
$application = new Application();
234-
$application->add(new \TestTiti());
235-
$application->add(new \TestToto());
236-
$this->assertEquals('test-toto', $application->find('test')->getName());
255+
$application->add(new \TestAmbiguousCommandRegistering());
256+
$application->add(new \TestAmbiguousCommandRegistering2());
257+
$this->assertEquals('test-ambiguous', $application->find('test')->getName());
237258
}
238259

239260
/**

src/Symfony/Component/Console/Tests/Fixtures/TestToto.php renamed to src/Symfony/Component/Console/Tests/Fixtures/TestAmbiguousCommandRegistering.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
use Symfony\Component\Console\Input\InputInterface;
55
use Symfony\Component\Console\Output\OutputInterface;
66

7-
class TestToto extends Command
7+
class TestAmbiguousCommandRegistering extends Command
88
{
99
protected function configure()
1010
{
1111
$this
12-
->setName('test-toto')
13-
->setDescription('The test-toto command')
12+
->setName('test-ambiguous')
13+
->setDescription('The test-ambiguous command')
1414
->setAliases(array('test'))
1515
;
1616
}
1717

1818
protected function execute(InputInterface $input, OutputInterface $output)
1919
{
20-
$output->write('test-toto');
20+
$output->write('test-ambiguous');
2121
}
2222
}

src/Symfony/Component/Console/Tests/Fixtures/TestTiti.php renamed to src/Symfony/Component/Console/Tests/Fixtures/TestAmbiguousCommandRegistering2.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
use Symfony\Component\Console\Input\InputInterface;
55
use Symfony\Component\Console\Output\OutputInterface;
66

7-
class TestTiti extends Command
7+
class TestAmbiguousCommandRegistering2 extends Command
88
{
99
protected function configure()
1010
{
1111
$this
12-
->setName('test-titi')
13-
->setDescription('The test:titi command')
12+
->setName('test-ambiguous2')
13+
->setDescription('The test-ambiguous2 command')
1414
;
1515
}
1616

1717
protected function execute(InputInterface $input, OutputInterface $output)
1818
{
19-
$output->write('test-titi');
19+
$output->write('test-ambiguous2');
2020
}
2121
}

0 commit comments

Comments
 (0)