Skip to content

Commit 6c5cc3e

Browse files
authored
Improve error message of command exceptions (#6962)
1 parent 3e5255a commit 6c5cc3e

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
188188
throw $exception;
189189
}
190190

191-
$this->output?->error((string) $exception);
191+
$this->getApplication()?->renderThrowable($exception, $this->output);
192192

193193
$this->exitCode = self::FAILURE;
194194

tests/CommandTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
use PHPUnit\Framework\TestCase;
2828
use Psr\Container\ContainerInterface;
2929
use Psr\EventDispatcher\EventDispatcherInterface;
30+
use Symfony\Component\Console\Application as ConsoleApplication;
3031
use Symfony\Component\Console\Input\InputInterface;
3132
use Symfony\Component\Console\Output\OutputInterface;
33+
use Throwable;
3234

3335
/**
3436
* @internal
@@ -57,27 +59,40 @@ public function testExitCodeWhenThrowException()
5759
ApplicationContext::setContainer($container = Mockery::mock(ContainerInterface::class));
5860
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturnFalse();
5961

62+
$output = Mockery::mock(OutputInterface::class);
63+
$application = Mockery::mock(ConsoleApplication::class);
64+
$application->shouldReceive('renderThrowable')
65+
->with(Mockery::type(Throwable::class), $output)
66+
->times(1);
67+
$application->shouldReceive('getHelperSet');
68+
6069
/** @var FooExceptionCommand $command */
6170
$command = new ClassInvoker(new FooExceptionCommand('foo'));
71+
$command->setApplication($application);
72+
$command->setOutput($output);
6273
$input = Mockery::mock(InputInterface::class);
6374
$input->shouldReceive('getOption')->andReturnFalse();
64-
$output = Mockery::mock(OutputInterface::class);
65-
$output->shouldReceive('writeln')->withAnyArgs()->andReturnNull();
6675

6776
$exitCode = $command->execute($input, $output);
6877
$this->assertSame(1, $exitCode);
6978

7079
/** @var FooExitCommand $command */
7180
$command = new ClassInvoker(new FooExitCommand());
81+
$command->setApplication($application);
82+
$command->setOutput($output);
7283
$exitCode = $command->execute($input, $output);
7384
$this->assertSame(11, $exitCode);
7485

7586
/** @var FooCommand $command */
7687
$command = new ClassInvoker(new FooCommand());
88+
$command->setApplication($application);
89+
$command->setOutput($output);
7790
$exitCode = $command->execute($input, $output);
7891
$this->assertSame(0, $exitCode);
7992

8093
$command = new FooTraitCommand();
94+
$command->setApplication($application);
95+
$command->setOutput($output);
8196
$this->assertArrayHasKey(Foo::class, (fn () => $this->setUpTraits($input, $output))->call($command));
8297
$this->assertSame('foo', (fn () => $this->propertyFoo)->call($command));
8398
}

0 commit comments

Comments
 (0)