8000 [FrameworkBundle]: Fail properly on unregistrable command · simPod/symfony@c3d83ca · GitHub
[go: up one dir, main page]

Skip to content

Commit c3d83ca

Browse files
committed
[FrameworkBundle]: Fail properly on unregistrable command
1 parent 2468bae commit c3d83ca

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\HttpKernel\Bundle\Bundle;
2525
use Symfony\Component\HttpKernel\Kernel;
2626
use Symfony\Component\HttpKernel\KernelInterface;
27+
use function max;
2728

2829
/**
2930
* @author Fabien Potencier <fabien@symfony.com>
@@ -68,13 +69,16 @@ public function doRun(InputInterface $input, OutputInterface $output): int
6869
{
6970
$this->registerCommands();
7071

72+
$statusCode = Command::SUCCESS;
7173
if ($this->registrationErrors) {
74+
$statusCode = Command::FAILURE;
75+
7276
$this->renderRegistrationErrors($input, $output);
7377
}
7478

7579
$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
7680

77-
return parent::doRun($input, $output);
81+
return max($statusCode, parent::doRun($input, $output));
7882
}
7983

8084
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output): int
@@ -83,7 +87,9 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
8387
$renderRegistrationErrors = true;
8488

8589
if (!$command instanceof ListCommand) {
90+
$statusCode = Command::SUCCESS;
8691
if ($this->registrationErrors) {
92+
$statusCode = Command::FAILURE;
8793
$this->renderRegistrationErrors($input, $output);
8894
$this->registrationErrors = [];
8995
$renderRegistrationErrors = false;
@@ -120,7 +126,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
120126
}
121127

122128
try {
123-
$returnCode = parent::doRunCommand($command, $input, $output);
129+
$returnCode = max($statusCode ?? 0, parent::doRunCommand($command, $input, $output));
124130
} finally {
125131
$requestStack?->pop();
126132
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function testBundleCommandCanOverriddeAPreExistingCommandWithTheSameName(
124124
$this->assertSame($newCommand, $application->get('example'));
125125
}
126126

127-
public function testRunOnlyWarnsOnUnregistrableCommand()
127+
public function testUnregistrableCommandsAreConsideredFailure()
128128
{
129129
$container = new ContainerBuilder();
130130
$container->register('event_dispatcher', EventDispatcher::class);
@@ -148,7 +148,7 @@ public function testRunOnlyWarnsOnUnregistrableCommand()
148148
$tester->run(['command' => 'fine']);
149149
$output = $tester->getDisplay();
150150

151-
$tester->assertCommandIsSuccessful();
151+
$this->assertSame(1, $tester->getStatusCode());
152152
$this->assertStringContainsString('Some commands could not be registered:', $output);
153153
$this->assertStringContainsString('throwing', $output);
154154
$this->assertStringContainsString('fine', $output);
@@ -181,7 +181,7 @@ public function testRegistrationErrorsAreDisplayedOnCommandNotFound()
181181
$this->assertStringContainsString('Command "fine" is not defined.', $output);
182182
}
183183

184-
public function testRunOnlyWarnsOnUnregistrableCommandAtTheEnd()
184+
public function testRunFailsOnUnregistrableCommandAtTheEnd()
185185
{
186186
$container = new ContainerBuilder();
187187
$container->register('event_dispatcher', EventDispatcher::class);
@@ -205,7 +205,7 @@ public function testRunOnlyWarnsOnUnregistrableCommandAtTheEnd()
205205
$tester = new ApplicationTester($application);
206206
$tester->run(['command' => 'list']);
207207

208-
$tester->assertCommandIsSuccessful();
208+
$this->assertSame(1, $tester->getStatusCode());
209209
$display = explode('List commands', $tester->getDisplay());
210210

211211
$this->assertStringContainsString(trim('[WARNING] Some commands could not be registered:'), trim($display[1]));

0 commit comments

Comments
 (0)
0