|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Fast Forward Development Tools for PHP projects. |
| 7 | + * |
| 8 | + * This file is part of fast-forward/dev-tools project. |
| 9 | + * |
| 10 | + * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | + * @license https://opensource.org/licenses/MIT MIT License |
| 12 | + * |
| 13 | + * @see https://github.com/php-fast-forward/ |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | + * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | + */ |
| 19 | + |
| 20 | +namespace FastForward\DevTools\Tests\Console\Input; |
| 21 | + |
| 22 | +use FastForward\DevTools\Console\Input\HasJsonOption; |
| 23 | +use FastForward\DevTools\Environment\RuntimeEnvironmentInterface; |
| 24 | +use PHPUnit\Framework\Attributes\CoversTrait; |
| 25 | +use PHPUnit\Framework\Attributes\Test; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 28 | +use Symfony\Component\Console\Input\InputInterface; |
| 29 | + |
| 30 | +use function Safe\putenv; |
| 31 | + |
| 32 | +#[CoversTrait(HasJsonOption::class)] |
| 33 | +final class HasJsonOptionTest extends TestCase |
| 34 | +{ |
| 35 | + use ProphecyTrait; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var array<string, mixed> |
| 39 | + */ |
| 40 | + private array $server; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var array<string, mixed> |
| 44 | + */ |
| 45 | + private array $environment; |
| 46 | + |
| 47 | + private string|false $composerTestsAreRunning; |
| 48 | + |
| 49 | + /** |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + protected function setUp(): void |
| 53 | + { |
| 54 | + $this->server = $_SERVER; |
| 55 | + $this->environment = $_ENV; |
| 56 | + $this->composerTestsAreRunning = getenv('COMPOSER_TESTS_ARE_RUNNING'); |
| 57 | + |
| 58 | + $_SERVER = []; |
| 59 | + $_ENV = []; |
| 60 | + putenv('COMPOSER_TESTS_ARE_RUNNING'); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + protected function tearDown(): void |
| 67 | + { |
| 68 | + $_SERVER = $this->server; |
| 69 | + $_ENV = $this->environment; |
| 70 | + |
| 71 | + if (false === $this->composerTestsAreRunning) { |
| 72 | + putenv('COMPOSER_TESTS_ARE_RUNNING'); |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + putenv('COMPOSER_TESTS_ARE_RUNNING=' . $this->composerTestsAreRunning); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + #[Test] |
| 84 | + public function isJsonOutputWillUseRuntimeEnvironmentWhenAvailable(): void |
| 85 | + { |
| 86 | + $runtimeEnvironment = $this->prophesize(RuntimeEnvironmentInterface::class); |
| 87 | + $runtimeEnvironment->isAgentPresent() |
| 88 | + ->willReturn(true); |
| 89 | + $runtimeEnvironment->isComposerTestRun() |
| 90 | + ->willReturn(false); |
| 91 | + |
| 92 | + $input = $this->prophesize(InputInterface::class); |
| 93 | + $input->getOption('pretty-json') |
| 94 | + ->willReturn(false); |
| 95 | + $input->getOption('json') |
| 96 | + ->willReturn(false); |
| 97 | + |
| 98 | + $command = new class ($runtimeEnvironment->reveal()) { |
| 99 | + use HasJsonOption; |
| 100 | + |
| 101 | + public function __construct( |
| 102 | + private readonly RuntimeEnvironmentInterface $runtimeEnvironment, |
| 103 | + ) {} |
| 104 | + |
| 105 | + public function isStructured(InputInterface $input): bool |
| 106 | + { |
| 107 | + return $this->isJsonOutput($input); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + self::assertTrue($command->isStructured($input->reveal())); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return void |
| 116 | + */ |
| 117 | + #[Test] |
| 118 | + public function isJsonOutputWillIgnoreFallbackAgentDetectionDuringPhpUnitRuns(): void |
| 119 | + { |
| 120 | + $_SERVER['CODEX_CI'] = '1'; |
| 121 | + |
| 122 | + $input = $this->prophesize(InputInterface::class); |
| 123 | + $input->getOption('pretty-json') |
| 124 | + ->willReturn(false); |
| 125 | + $input->getOption('json') |
| 126 | + ->willReturn(false); |
| 127 | + |
| 128 | + $command = new class { |
| 129 | + use HasJsonOption; |
| 130 | + |
| 131 | + public function isStructured(InputInterface $input): bool |
| 132 | + { |
| 133 | + return $this->isJsonOutput($input); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + self::assertFalse($command->isStructured($input->reveal())); |
| 138 | + } |
| 139 | +} |
0 commit comments