Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Symfony\Component\Console\Exception\RunCommandFailedException;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Messenger\Exception\RecoverableExceptionInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface;

/**
* @author Kevin Bond <kevinbond@gmail.com>
Expand All @@ -35,6 +37,8 @@ public function __invoke(RunCommandMessage $message): RunCommandContext

try {
$exitCode = $this->application->run($input, $output);
} catch (UnrecoverableExceptionInterface|RecoverableExceptionInterface $e) {
throw $e;
} catch (\Throwable $e) {
throw new RunCommandFailedException($e, new RunCommandContext($message, Command::FAILURE, $output->fetch()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
use Symfony\Component\Console\Messenger\RunCommandMessage;
use Symfony\Component\Console\Messenger\RunCommandMessageHandler;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Messenger\Exception\RecoverableExceptionInterface;
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;

/**
* @author Kevin Bond <kevinbond@gmail.com>
Expand Down Expand Up @@ -81,6 +85,38 @@ public function testThrowOnNonSuccess()
$this->fail('Exception not thrown.');
}

public function testExecutesCommandThatThrownUnrecoverableException()
{
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());

try {
$handler(new RunCommandMessage('test:command --throw-unrecoverable'));
} catch (UnrecoverableExceptionInterface $e) {
$this->assertSame('Unrecoverable exception message', $e->getMessage());
$this->assertNull($e->getPrevious());

return;
}

$this->fail('Exception not thrown.');
}

public function testExecutesCommandThatThrownRecoverableException()
{
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());

try {
$handler(new RunCommandMessage('test:command --throw-recoverable'));
} catch (RecoverableExceptionInterface $e) {
$this->assertSame('Recoverable exception message', $e->getMessage());
$this->assertNull($e->getPrevious());

return;
}

$this->fail('Exception not thrown.');
}

private function createApplicationWithCommand(): Application
{
$application = new Application();
Expand All @@ -92,6 +128,8 @@ public function configure(): void
$this
->setName('test:command')
->addOption('throw')
->addOption('throw-unrecoverable')
->addOption('throw-recoverable')
->addOption('exit', null, InputOption::VALUE_REQUIRED, 0)
;
}
Expand All @@ -100,6 +138,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->write('some message');

if ($input->getOption('throw-unrecoverable')) {
throw new UnrecoverableMessageHandlingException('Unrecoverable exception message');
}

if ($input->getOption('throw-recoverable')) {
throw new RecoverableMessageHandlingException('Recoverable exception message');
}

if ($input->getOption('throw')) {
throw new \RuntimeException('exception message');
}
Expand Down