|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Console\Tests\Messenger; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Console\Application; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Exception\RunCommandFailedException; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Input\InputOption; |
| 20 | +use Symfony\Component\Console\Messenger\RunCommandMessage; |
| 21 | +use Symfony\Component\Console\Messenger\RunCommandMessageHandler; |
| 22 | +use Symfony\Component\Console\Output\OutputInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Kevin Bond <kevinbond@gmail.com> |
| 26 | + */ |
| 27 | +final class RunCommandMessageHandlerTest extends TestCase |
| 28 | +{ |
| 29 | + public function testExecutesCommand() |
| 30 | + { |
| 31 | + $handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); |
| 32 | + $context = $handler(new RunCommandMessage('test:command')); |
| 33 | + |
| 34 | + $this->assertSame(0, $context->exitCode); |
| 35 | + $this->assertStringContainsString('some message', $context->output); |
| 36 | + } |
| 37 | + |
| 38 | + public function testExecutesCommandThatThrowsException() |
| 39 | + { |
| 40 | + $handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); |
| 41 | + |
| 42 | + try { |
| 43 | + $handler(new RunCommandMessage('test:command --throw')); |
| 44 | + } catch (RunCommandFailedException $e) { |
| 45 | + $this->assertSame(1, $e->context->exitCode); |
| 46 | + $this->assertStringContainsString('some message', $e->context->output); |
| 47 | + $this->assertInstanceOf(\RuntimeException::class, $e->getPrevious()); |
| 48 | + $this->assertSame('exception message', $e->getMessage()); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $this->fail('Exception not thrown.'); |
| 54 | + } |
| 55 | + |
| 56 | + public function testExecutesCommandThatCatchesThrownException() |
| 57 | + { |
| 58 | + $handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); |
| 59 | + $context = $handler(new RunCommandMessage('test:command --throw -v', throwOnFailure: false, catchExceptions: true)); |
| 60 | + |
| 61 | + $this->assertSame(1, $context->exitCode); |
| 62 | + $this->assertStringContainsString('[RuntimeException]', $context->output); |
| 63 | + $this->assertStringContainsString('exception message', $context->output); |
| 64 | + } |
| 65 | + |
| 66 | + public function testThrowOnNonSuccess() |
| 67 | + { |
| 68 | + $handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); |
| 69 | + |
| 70 | + try { |
| 71 | + $handler(new RunCommandMessage('test:command --exit=1')); |
| 72 | + } catch (RunCommandFailedException $e) { |
| 73 | + $this->assertSame(1, $e->context->exitCode); |
| 74 | + $this->assertStringContainsString('some message', $e->context->output); |
| 75 | + $this->assertSame('Command "test:command --exit=1" exited with code "1".', $e->getMessage()); |
| 76 | + $this->assertNull($e->getPrevious()); |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + $this->fail('Exception not thrown.'); |
| 82 | + } |
| 83 | + |
| 84 | + private function createApplicationWithCommand(): Application |
| 85 | + { |
| 86 | + $application = new Application(); |
| 87 | + $application->setAutoExit(false); |
| 88 | + $application->addCommands([ |
| 89 | + new class() extends Command { |
| 90 | + public function configure(): void |
| 91 | + { |
| 92 | + $this |
| 93 | + ->setName('test:command') |
| 94 | + ->addOption('throw') |
| 95 | + ->addOption('exit', null, InputOption::VALUE_REQUIRED, 0) |
| 96 | + ; |
| 97 | + } |
| 98 | + |
| 99 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 100 | + { |
| 101 | + $output->write('some message'); |
| 102 | + |
| 103 | + if ($input->getOption('throw')) { |
| 104 | + throw new \RuntimeException('exception message'); |
| 105 | + } |
| 106 | + |
| 107 | + return (int) $input->getOption('exit'); |
| 108 | + } |
| 109 | + }, |
| 110 | + ]); |
| 111 | + |
| 112 | + return $application; |
| 113 | + } |
| 114 | +} |
0 commit comments