diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Application.php b/src/Symfony/Bundle/FrameworkBundle/Console/Application.php index 2be849db17..c210050a55 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Application.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Application.php @@ -11,6 +11,7 @@ namespace Symfony\Bundle\FrameworkBundle\Console; +use Symfony\Component\Console\Command\ListCommand; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Debug\Exception\FatalThrowableError; @@ -79,11 +80,23 @@ class Application extends BaseApplication */ protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { - if ($this->registrationErrors) { - $this->renderRegistrationErrors($input, $output); + if (!$command instanceof ListCommand) { + if ($this->registrationErrors) { + $this->renderRegistrationErrors($input, $output); + $this->registrationErrors = array(); + } + + return parent::doRunCommand($command, $input, $output); } - return parent::doRunCommand($command, $input, $output); + $returnCode = parent::doRunCommand($command, $input, $output); + + if ($this->registrationErrors) { + $this->renderRegistrationErrors($input, $output); + $this->registrationErrors = array(); + } + + return $returnCode; } /** @@ -192,7 +205,5 @@ class Application extends BaseApplication foreach ($this->registrationErrors as $error) { $this->doRenderException($error, $output); } - - $this->registrationErrors = array(); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php index c977bb13a1..7dced9e648 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php @@ -192,6 +192,35 @@ class ApplicationTest extends TestCase $this->assertContains('Command "fine" is not defined.', $output); } + public function testRunOnlyWarnsOnUnregistrableCommandAtTheEnd() + { + $container = new ContainerBuilder(); + $container->register('event_dispatcher', EventDispatcher::class); + $container->register(ThrowingCommand::class, ThrowingCommand::class); + $container->setParameter('console.command.ids', array(ThrowingCommand::class => ThrowingCommand::class)); + + $kernel = $this->getMockBuilder(KernelInterface::class)->getMock(); + $kernel + ->method('getBundles') + ->willReturn(array($this->createBundleMock( + array((new Command('fine'))->setCode(function (InputInterface $input, OutputInterface $output) { $output->write('fine'); })) + ))); + $kernel + ->method('getContainer') + ->willReturn($container); + + $application = new Application($kernel); + $application->setAutoExit(false); + + $tester = new ApplicationTester($application); + $tester->run(array('command' => 'list')); + + $this->assertSame(0, $tester->getStatusCode()); + $display = explode('Lists commands', $tester->getDisplay()); + + $this->assertContains(trim('[WARNING] Some commands could not be registered:'), trim($display[1])); + } + private function getKernel(array $bundles, $useDispatcher = false) { $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();