diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 9cdcef88cd042..84eaec66a3c24 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -1147,7 +1147,7 @@ private function findAlternatives(string $name, iterable $collection): array */ public function setDefaultCommand(string $commandName, bool $isSingleCommand = false) { - $this->defaultCommand = $commandName; + $this->defaultCommand = explode('|', ltrim($commandName, '|'))[0]; if ($isSingleCommand) { // Ensure the command exist diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 839beac3f0145..81d4b9bba95e3 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -422,6 +422,25 @@ public function testCommandAttribute() $this->assertTrue($command->isHidden()); $this->assertSame(['f'], $command->getAliases()); } + + /** + * @requires PHP 8 + */ + public function testDefaultCommand() + { + $apl = new Application(); + $apl->setDefaultCommand(Php8Command::getDefaultName()); + $property = new \ReflectionProperty($apl, 'defaultCommand'); + $property->setAccessible(true); + + $this->assertEquals('foo', $property->getValue($apl)); + + $apl->setDefaultCommand(Php8Command2::getDefaultName()); + $property = new \ReflectionProperty($apl, 'defaultCommand'); + $property->setAccessible(true); + + $this->assertEquals('foo2', $property->getValue($apl)); + } } // In order to get an unbound closure, we should create it outside a class @@ -437,3 +456,8 @@ function createClosure() class Php8Command extends Command { } + +#[AsCommand(name: 'foo2', description: 'desc2', hidden: true)] +class Php8Command2 extends Command +{ +}