| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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\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 |
* @author Amrouche Hamza <[email protected]> |
| 23 |
*/ |
| 24 |
trait TesterTrait |
| 25 |
{ |
| 26 |
private StreamOutput $output; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var list<string> |
| 30 |
*/ |
| 31 |
private array $inputs = []; |
| 32 |
private bool $captureStreamsIndependently = false; |
| 33 |
private InputInterface $input; |
| 34 |
private int $statusCode; |
| 35 |
|
| 36 |
/** |
| 37 |
* Gets the display returned by the last execution of the command or application. |
| 38 |
* |
| 39 |
* @throws \RuntimeException If it's called before the execute method |
| 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 |
* Gets the output written to STDERR by the application. |
| 60 |
* |
| 61 |
* @param bool $normalize Whether to normalize end of lines to \n or not |
| 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 |
* Gets the input instance used by the last execution of the command or application. |
| 82 |
*/ |
| 83 |
public function getInput(): InputInterface |
| 84 |
{ |
| 85 |
return $this->input; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Gets the output instance used by the last execution of the command or application. |
| 90 |
*/ |
| 91 |
public function getOutput(): OutputInterface |
| 92 |
{ |
| 93 |
return $this->output; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Gets the status code returned by the last execution of the command or application. |
| 98 |
* |
| 99 |
* @throws \RuntimeException If it's called before the execute method |
| 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 |
* Sets the user inputs. |
| 113 |
* |
| 114 |
* @param list<string> $inputs An array of strings representing each input |
| 115 |
* passed to the command input stream |
| 116 |
* |
| 117 |
* @return $this |
| 118 |
*/ |
| 119 |
public function setInputs(array $inputs): static |
| 120 |
{ |
| 121 |
$this->inputs = $inputs; |
| 122 |
|
| 123 |
return $this; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Initializes the output property. |
| 128 |
* |
| 129 |
* Available options: |
| 130 |
* |
| 131 |
* * decorated: Sets the output decorated flag |
| 132 |
* * verbosity: Sets the output verbosity flag |
| 133 |
* * capture_stderr_separately: Make output of stdOut and stdErr separately available |
| 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 |
* @param list<string> $inputs |
| 169 |
* |
| 170 |
* @return resource |
| 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 |
} |