Skip to content

Commit 260d24e

Browse files
Merge branch '4.2' into 4.3
* 4.2: [Routing] Fixed unexpected 404 NoConfigurationException [DI] Removes number of elements information in debug mode [Contracts] Simplify implementation declarations Update PR template for 4.3 [Intl] Add FallbackTrait for data generation [Console] Commands with an alias should not be recognized as ambiguous clarify the possible class/interface of the cache
2 parents 5c693cb + 7a293c9 commit 260d24e

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

Application.php

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

616616
$aliases = [];
617+
618+
foreach ($this->commands as $command) {
619+
foreach ($command->getAliases() as $alias) {
620+
if (!$this->has($alias)) {
621+
$this->commands[$alias] = $command;
622+
}
623+
}
624+
}
625+
617626
$allCommands = $this->commandLoader ? array_merge($this->commandLoader->getNames(), array_keys($this->commands)) : array_keys($this->commands);
618627
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
619628
$commands = preg_grep('{^'.$expr.'}', $allCommands);

Tests/ApplicationTest.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public static function setUpBeforeClass()
7373
require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
7474
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
7575
require_once self::$fixturesPath.'/FooWithoutAliasCommand.php';
76-
require_once self::$fixturesPath.'/TestTiti.php';
77-
require_once self::$fixturesPath.'/TestToto.php';
76+
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering.php';
77+
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering2.php';
7878
}
7979

8080
protected function normalizeLineBreaks($text)
@@ -165,6 +165,28 @@ public function testRegister()
165165
$this->assertEquals('foo', $command->getName(), '->register() registers a new command');
166166
}
167167

168+
public function testRegisterAmbiguous()
169+
{
170+
$code = function (InputInterface $input, OutputInterface $output) {
171+
$output->writeln('It works!');
172+
};
173+
174+
$application = new Application();
175+
$application->setAutoExit(false);
176+
$application
177+
->register('test-foo')
178+
->setAliases(['test'])
179+
->setCode($code);
180+
181+
$application
182+
->register('test-bar')
183+
->setCode($code);
184+
185+
$tester = new ApplicationTester($application);
186+
$tester->run(['test']);
187+
$this->assertContains('It works!', $tester->getDisplay(true));
188+
}
189+
168190
public function testAdd()
169191
{
170192
$application = new Application();
@@ -304,9 +326,9 @@ public function testFindAmbiguousNamespace()
304326
public function testFindNonAmbiguous()
305327
{
306328
$application = new Application();
307-
$application->add(new \TestTiti());
308-
$application->add(new \TestToto());
309-
$this->assertEquals('test-toto', $application->find('test')->getName());
329+
$application->add(new \TestAmbiguousCommandRegistering());
330+
$application->add(new \TestAmbiguousCommandRegistering2());
331+
$this->assertEquals('test-ambiguous', $application->find('test')->getName());
310332
}
311333

312334
/**

Tests/Fixtures/TestToto.php renamed to 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
}

Tests/Fixtures/TestTiti.php renamed to 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)