diff --git a/src/command/src/Command.php b/src/command/src/Command.php index e38e80d48..682f6bef8 100644 --- a/src/command/src/Command.php +++ b/src/command/src/Command.php @@ -429,6 +429,8 @@ abstract class Command extends SymfonyCommand throw $exception; } + $this->output && $this->error($exception->getMessage()); + $this->eventDispatcher->dispatch(new Event\FailToHandle($this, $exception)); return $this->exitCode = $exception->getCode(); } finally { diff --git a/src/command/src/Listener/FailToHandleListener.php b/src/command/src/Listener/FailToHandleListener.php new file mode 100644 index 000000000..234644a1c --- /dev/null +++ b/src/command/src/Listener/FailToHandleListener.php @@ -0,0 +1,41 @@ +container->has(StdoutLoggerInterface::class) && $logger = $this->container->get(StdoutLoggerInterface::class)) { + $logger->error((string) $event->getThrowable()); + } + } +} diff --git a/src/command/tests/CommandTest.php b/src/command/tests/CommandTest.php index 013bde5f3..f1c344a01 100644 --- a/src/command/tests/CommandTest.php +++ b/src/command/tests/CommandTest.php @@ -57,17 +57,20 @@ class CommandTest extends TestCase $command = new ClassInvoker(new FooExceptionCommand('foo')); $input = Mockery::mock(InputInterface::class); $input->shouldReceive('getOption')->andReturnFalse(); - $exitCode = $command->execute($input, Mockery::mock(OutputInterface::class)); + $output = Mockery::mock(OutputInterface::class); + $output->shouldReceive('writeln')->withAnyArgs()->andReturnNull(); + + $exitCode = $command->execute($input, $output); $this->assertSame(99, $exitCode); /** @var FooExitCommand $command */ $command = new ClassInvoker(new FooExitCommand()); - $exitCode = $command->execute($input, Mockery::mock(OutputInterface::class)); + $exitCode = $command->execute($input, $output); $this->assertSame(11, $exitCode); /** @var FooCommand $command */ $command = new ClassInvoker(new FooCommand()); - $exitCode = $command->execute($input, Mockery::mock(OutputInterface::class)); + $exitCode = $command->execute($input, $output); $this->assertSame(0, $exitCode); }