|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace WonderNetwork\SlimKernel\Cli; |
| 5 | + |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use RuntimeException; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\ArrayInput; |
| 10 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Throwable; |
| 13 | + |
| 14 | +class FingersCrossedHandlerTest extends TestCase { |
| 15 | + private BufferedOutput $spy; |
| 16 | + private FingersCrossedHandler $sut; |
| 17 | + |
| 18 | + protected function setUp(): void { |
| 19 | + $this->spy = new BufferedOutput(); |
| 20 | + $this->sut = FingersCrossedHandler::of(new ArrayInput([]), $this->spy); |
| 21 | + } |
| 22 | + |
| 23 | + public function testHidesNormalOutputOnSuccess(): void { |
| 24 | + $result = $this->sut->run( |
| 25 | + function (InputParams $input, FingersCrossedOutput $output) { |
| 26 | + $output->writeln("Hello world"); |
| 27 | + return Command::SUCCESS; |
| 28 | + }, |
| 29 | + ); |
| 30 | + self::assertSame(0, $result); |
| 31 | + self::assertSame("", $this->spy->fetch()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testDisplaysAllOutputOnFailure(): void { |
| 35 | + $result = $this->sut->run( |
| 36 | + function (InputParams $input, FingersCrossedOutput $output) { |
| 37 | + $output->writeln("Hello world"); |
| 38 | + return Command::FAILURE; |
| 39 | + }, |
| 40 | + ); |
| 41 | + self::assertSame(1, $result); |
| 42 | + self::assertSame("Hello world\n", $this->spy->fetch()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testCorrectlyFormatsAllOutput(): void { |
| 46 | + $this->sut->run( |
| 47 | + function (InputParams $input, FingersCrossedOutput $output) { |
| 48 | + $output->writeln("Alpha"); |
| 49 | + $output->writeln("Bravo"); |
| 50 | + return Command::FAILURE; |
| 51 | + }, |
| 52 | + ); |
| 53 | + self::assertSame("Alpha\nBravo\n", $this->spy->fetch()); |
| 54 | + } |
| 55 | + |
| 56 | + public function testDisplaysAllOutputWhenThrows(): void { |
| 57 | + $e = null; |
| 58 | + try { |
| 59 | + $this->sut->run( |
| 60 | + function (InputParams $input, FingersCrossedOutput $output) { |
| 61 | + $output->writeln("Hello world"); |
| 62 | + throw new RuntimeException(); |
| 63 | + }, |
| 64 | + ); |
| 65 | + } catch (Throwable $e) {} |
| 66 | + self::assertSame("Hello world\n", $this->spy->fetch()); |
| 67 | + self::assertInstanceOf(Throwable::class, $e); |
| 68 | + } |
| 69 | + |
| 70 | + public function testDisplaysAllOutputWhenVerbose(): void { |
| 71 | + $this->spy->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
| 72 | + $this->sut->run( |
| 73 | + function (InputParams $input, FingersCrossedOutput $output) { |
| 74 | + $output->writeln("Hello world"); |
| 75 | + return Command::SUCCESS; |
| 76 | + }, |
| 77 | + ); |
| 78 | + self::assertSame("Hello world\n", $this->spy->fetch()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testOutputIsInOrderWhenVerbositiIsIncreased(): void { |
| 82 | + $this->sut->run( |
| 83 | + function (InputParams $input, FingersCrossedOutput $output) { |
| 84 | + $output->writeln("Alpha"); |
| 85 | + $this->spy->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
| 86 | + $output->writeln("Bravo"); |
| 87 | + return Command::SUCCESS; |
| 88 | + }, |
| 89 | + ); |
| 90 | + self::assertSame("Alpha\nBravo\n", $this->spy->fetch()); |
| 91 | + } |
| 92 | +} |
0 commit comments