Skip to content

Commit fb4d928

Browse files
bug symfony#31261 [Console] Commands with an alias should not be recognized as ambiguous when using register (Simperfit)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Commands with an alias should not be recognized as ambiguous when using register | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#25355 | License | MIT | Doc PR | I think when passing an alias, it should not be treated as a ambiguous command since it's configured to response to it. I've [pushed a commit](Simperfit/symfony-reproducer@2f5209a) that reproduce the bug and with this patch it does work. Commits ------- ae7ee46 [Console] Commands with an alias should not be recognized as ambiguous
2 parents 284c216 + ae7ee46 commit fb4d928

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,15 @@ public function find($name)
596596
$this->init();
597597

598598
$aliases = [];
599+
600+
foreach ($this->commands as $command) {
601+
foreach ($command->getAliases() as $alias) {
602+
if (!$this->has($alias)) {
603+
$this->commands[$alias] = $command;
604+
}
605+
}
606+
}
607+
599608
$allCommands = $this->commandLoader ? array_merge($this->commandLoader->getNames(), array_keys($this->commands)) : array_keys($this->commands);
600609
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
601610
$commands = preg_grep('{^'.$expr.'}', $allCommands);

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public static function setUpBeforeClass()
7272
require_once self::$fixturesPath.'/BarBucCommand.php';
7373
require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
7474
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
75-
require_once self::$fixturesPath.'/TestTiti.php';
76-
require_once self::$fixturesPath.'/TestToto.php';
75+
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering.php';
76+
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering2.php';
7777
}
7878

7979
protected function normalizeLineBreaks($text)
@@ -164,6 +164,27 @@ public function testRegister()
164164
$this->assertEquals('foo', $command->getName(), '->register() registers a new command');
165165
}
166166

167+
public function testRegisterAmbiguous()
168+
{
169+
$code = function (InputInterface $input, OutputInterface $output) {
170+
$output->writeln('It works!');
171+
};
172+
173+
$application = new Application();
174+
$application
175+
->register('test-foo')
176+
->setAliases(['test'])
177+
->setCode($code);
178+
179+
$application
180+
->register('test-bar')
181+
->setCode($code);
182+
183+
$tester = new ApplicationTester($application);
184+
$tester->run(['test']);
185+
$this->assertContains('It works!', $tester->getDisplay(true));
186+
}
187+
167188
public function testAdd()
168189
{
169190
$application = new Application();
@@ -303,9 +324,9 @@ public function testFindAmbiguousNamespace()
303324
public function testFindNonAmbiguous()
304325
{
305326
$application = new Application();
306-
$application->add(new \TestTiti());
307-
$application->add(new \TestToto());
308-
$this->assertEquals('test-toto', $application->find('test')->getName());
327+
$application->add(new \TestAmbiguousCommandRegistering());
328+
$application->add(new \TestAmbiguousCommandRegistering2());
329+
$this->assertEquals('test-ambiguous', $application->find('test')->getName());
309330
}
310331

311332
/**

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(['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)