Skip to content

Commit a66914d

Browse files
committed
[FrameworkBundle]: Fail properly on unregistrable command
1 parent f3a9a0e commit a66914d

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,29 @@ public function doRun(InputInterface $input, OutputInterface $output): int
6868
{
6969
$this->registerCommands();
7070

71+
$statusCode = Command::SUCCESS;
7172
if ($this->registrationErrors) {
73+
$statusCode = Command::FAILURE;
74+
7275
$this->renderRegistrationErrors($input, $output);
7376
}
7477

7578
$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
7679

77-
return parent::doRun($input, $output);
80+
return max($statusCode, parent::doRun($input, $output));
7881
}
7982

8083
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output): int
8184
{
8285
if (!$command instanceof ListCommand) {
86+
$statusCode = Command::SUCCESS;
8387
if ($this->registrationErrors) {
88+
$statusCode = Command::FAILURE;
8489
$this->renderRegistrationErrors($input, $output);
8590
$this->registrationErrors = [];
8691
}
8792

88-
return parent::doRunCommand($command, $input, $output);
93+
return max($statusCode, parent::doRunCommand($command, $input, $output));
8994
}
9095

9196
$returnCode = parent::doRunCommand($command, $input, $output);

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)