| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Tester; |
| 13 |
|
| 14 |
use PHPUnit\Framework\Assert; |
| 15 |
use Symfony\Component\Console\Input\InputInterface; |
| 16 |
use Symfony\Component\Console\Output\ConsoleOutput; |
| 17 |
use Symfony\Component\Console\Output\OutputInterface; |
| 18 |
use Symfony\Component\Console\Output\StreamOutput; |
| 19 |
use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
trait TesterTrait |
| 25 |
{ |
| 26 |
private StreamOutput $output; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private array $inputs = []; |
| 32 |
private bool $captureStreamsIndependently = false; |
| 33 |
private InputInterface $input; |
| 34 |
private int $statusCode; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function getDisplay(bool $normalize = false): string |
| 42 |
{ |
| 43 |
if (!isset($this->output)) { |
| 44 |
throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?'); |
| 45 |
} |
| 46 |
|
| 47 |
rewind($this->output->getStream()); |
| 48 |
|
| 49 |
$display = stream_get_contents($this->output->getStream()); |
| 50 |
|
| 51 |
if ($normalize) { |
| 52 |
$display = str_replace(\PHP_EOL, "\n", $display); |
| 53 |
} |
| 54 |
|
| 55 |
return $display; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function getErrorOutput(bool $normalize = false): string |
| 64 |
{ |
| 65 |
if (!$this->captureStreamsIndependently) { |
| 66 |
throw new \LogicException('The error output is not available when the tester is run without "capture_stderr_separately" option set.'); |
| 67 |
} |
| 68 |
|
| 69 |
rewind($this->output->getErrorOutput()->getStream()); |
| 70 |
|
| 71 |
$display = stream_get_contents($this->output->getErrorOutput()->getStream()); |
| 72 |
|
| 73 |
if ($normalize) { |
| 74 |
$display = str_replace(\PHP_EOL, "\n", $display); |
| 75 |
} |
| 76 |
|
| 77 |
return $display; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function getInput(): InputInterface |
| 84 |
{ |
| 85 |
return $this->input; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function getOutput(): OutputInterface |
| 92 |
{ |
| 93 |
return $this->output; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function getStatusCode(): int |
| 102 |
{ |
| 103 |
return $this->statusCode ?? throw new \RuntimeException('Status code not initialized, did you execute the command before requesting the status code?'); |
| 104 |
} |
| 105 |
|
| 106 |
public function assertCommandIsSuccessful(string $message = ''): void |
| 107 |
{ |
| 108 |
Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function setInputs(array $inputs): static |
| 120 |
{ |
| 121 |
$this->inputs = $inputs; |
| 122 |
|
| 123 |
return $this; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private function initOutput(array $options): void |
| 136 |
{ |
| 137 |
$this->captureStreamsIndependently = $options['capture_stderr_separately'] ?? false; |
| 138 |
if (!$this->captureStreamsIndependently) { |
| 139 |
$this->output = new StreamOutput(fopen('php://memory', 'w', false)); |
| 140 |
if (isset($options['decorated'])) { |
| 141 |
$this->output->setDecorated($options['decorated']); |
| 142 |
} |
| 143 |
if (isset($options['verbosity'])) { |
| 144 |
$this->output->setVerbosity($options['verbosity']); |
| 145 |
} |
| 146 |
} else { |
| 147 |
$this->output = new ConsoleOutput( |
| 148 |
$options['verbosity'] ?? ConsoleOutput::VERBOSITY_NORMAL, |
| 149 |
$options['decorated'] ?? null |
| 150 |
); |
| 151 |
|
| 152 |
$errorOutput = new StreamOutput(fopen('php://memory', 'w', false)); |
| 153 |
$errorOutput->setFormatter($this->output->getFormatter()); |
| 154 |
$errorOutput->setVerbosity($this->output->getVerbosity()); |
| 155 |
$errorOutput->setDecorated($this->output->isDecorated()); |
| 156 |
|
| 157 |
$reflectedOutput = new \ReflectionObject($this->output); |
| 158 |
$strErrProperty = $reflectedOutput->getProperty('stderr'); |
| 159 |
$strErrProperty->setValue($this->output, $errorOutput); |
| 160 |
|
| 161 |
$reflectedParent = $reflectedOutput->getParentClass(); |
| 162 |
$streamProperty = $reflectedParent->getProperty('stream'); |
| 163 |
$streamProperty->setValue($this->output, fopen('php://memory', 'w', false)); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private static function createStream(array $inputs) |
| 173 |
{ |
| 174 |
$stream = fopen('php://memory', 'r+', false); |
| 175 |
|
| 176 |
foreach ($inputs as $input) { |
| 177 |
fwrite($stream, $input); |
| 178 |
|
| 179 |
if (!str_ends_with($input, "\x4")) { |
| 180 |
fwrite($stream, \PHP_EOL); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
rewind($stream); |
| 185 |
|
| 186 |
return $stream; |
| 187 |
} |
| 188 |
} |