Skip to content

Commit ecefeec

Browse files
author
Amrouche Hamza
committed
[FrameworkBundle] [Console] add a warning when command is not found
1 parent 4271fec commit ecefeec

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Console;
1313

14+
use Symfony\Component\Console\Exception\CommandNotFoundException;
15+
use Symfony\Component\Console\Exception\CommandNotRegisteredException;
1416
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1517
use Symfony\Component\Console\Style\SymfonyStyle;
1618
use Symfony\Component\Debug\Exception\FatalThrowableError;
@@ -69,7 +71,15 @@ public function doRun(InputInterface $input, OutputInterface $output)
6971
$this->renderRegistrationErrors($input, $output);
7072
}
7173

72-
return parent::doRun($input, $output);
74+
try {
75+
return parent::doRun($input, $output);
76+
} catch (CommandNotFoundException $e) {
77+
if ($this->registrationErrors) {
78+
$this->renderRegistrationErrors($input, $output);
79+
}
80+
81+
throw $e;
82+
}
7383
}
7484

7585
/**

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,37 @@ public function testRunOnlyWarnsOnUnregistrableCommand()
165165
$this->assertContains('fine', $output);
166166
}
167167

168+
public function testRunOnlyWarnsOnNoName()
169+
{
170+
$container = new ContainerBuilder();
171+
$container->register('event_dispatcher', EventDispatcher::class);
172+
$container->register(ThrowingCommand::class, ThrowingCommand::class);
173+
$container->setParameter('console.command.ids', array(ThrowingCommand::class => ThrowingCommand::class));
174+
175+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
176+
$kernel
177+
->method('getBundles')
178+
->willReturn(array($this->createBundleMock(
179+
array((new Command(null))->setCode(function (InputInterface $input, OutputInterface $output) { $output->write('fine'); }))
180+
)));
181+
$kernel
182+
->method('getContainer')
183+
->willReturn($container);
184+
185+
$application = new Application($kernel);
186+
$application->setAutoExit(false);
187+
188+
$tester = new ApplicationTester($application);
189+
$tester->run(array('command' => 'fine'));
190+
$tester->getOutput();
191+
$output = $tester->getDisplay();
192+
193+
$this->assertSame(1, $tester->getStatusCode());
194+
$this->assertContains('Some commands could not be registered:', $output);
195+
$this->assertContains('throwing', $output);
196+
$this->assertContains('fine', $output);
197+
}
198+
168199
private function getKernel(array $bundles, $useDispatcher = false)
169200
{
170201
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
/**
15+
* Represents a non-registered command.
16+
*
17+
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
18+
*/
19+
class CommandNotRegisteredException extends CommandNotFoundException
20+
{
21+
/**
22+
* @param string $message Exception message to throw
23+
* @param int $code Exception code
24+
* @param \Exception $previous Previous exception used for the exception chaining
25+
*/
26+
public function __construct($message, $code = 0, \Exception $previous = null)
27+
{
28+
$message = sprintf("[WARNING] Some commands could not be registered: \n %s", $message);
29+
parent::__construct($message, $previous instanceof CommandNotFoundException ? $previous->getAlternatives() : array(), $code, $previous);
30+
}
31+
}

0 commit comments

Comments
 (0)