Skip to content

Commit

Permalink
Merge branch '3.4' into 4.2
Browse files Browse the repository at this point in the history
* 3.4:
  [DI] Removes number of elements information in debug mode
  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
  • Loading branch information
nicolas-grekas committed May 9, 2019
2 parents 28e7360 + 8e1d1e4 commit 7a293c9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
9 changes: 9 additions & 0 deletions Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,15 @@ public function find($name)
$this->init();

$aliases = [];

foreach ($this->commands as $command) {
foreach ($command->getAliases() as $alias) {
if (!$this->has($alias)) {
$this->commands[$alias] = $command;
}
}
}

$allCommands = $this->commandLoader ? array_merge($this->commandLoader->getNames(), array_keys($this->commands)) : array_keys($this->commands);
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
$commands = preg_grep('{^'.$expr.'}', $allCommands);
Expand Down
32 changes: 27 additions & 5 deletions Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public static function setUpBeforeClass()
require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
require_once self::$fixturesPath.'/FooWithoutAliasCommand.php';
require_once self::$fixturesPath.'/TestTiti.php';
require_once self::$fixturesPath.'/TestToto.php';
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering.php';
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering2.php';
}

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

public function testRegisterAmbiguous()
{
$code = function (InputInterface $input, OutputInterface $output) {
$output->writeln('It works!');
};

$application = new Application();
$application->setAutoExit(false);
$application
->register('test-foo')
->setAliases(['test'])
->setCode($code);

$application
->register('test-bar')
->setCode($code);

$tester = new ApplicationTester($application);
$tester->run(['test']);
$this->assertContains('It works!', $tester->getDisplay(true));
}

public function testAdd()
{
$application = new Application();
Expand Down Expand Up @@ -304,9 +326,9 @@ public function testFindAmbiguousNamespace()
public function testFindNonAmbiguous()
{
$application = new Application();
$application->add(new \TestTiti());
$application->add(new \TestToto());
$this->assertEquals('test-toto', $application->find('test')->getName());
$application->add(new \TestAmbiguousCommandRegistering());
$application->add(new \TestAmbiguousCommandRegistering2());
$this->assertEquals('test-ambiguous', $application->find('test')->getName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestToto extends Command
class TestAmbiguousCommandRegistering extends Command
{
protected function configure()
{
$this
->setName('test-toto')
->setDescription('The test-toto command')
->setName('test-ambiguous')
->setDescription('The test-ambiguous command')
->setAliases(['test'])
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->write('test-toto');
$output->write('test-ambiguous');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestTiti extends Command
class TestAmbiguousCommandRegistering2 extends Command
{
protected function configure()
{
$this
->setName('test-titi')
->setDescription('The test:titi command')
->setName('test-ambiguous2')
->setDescription('The test-ambiguous2 command')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->write('test-titi');
$output->write('test-ambiguous2');
}
}

0 comments on commit 7a293c9

Please sign in to comment.