| 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\Tester; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Input\InputInterface; |
| 15 |
use Symfony\Component\Console\Output\ConsoleOutput; |
| 16 |
use Symfony\Component\Console\Output\OutputInterface; |
| 17 |
use Symfony\Component\Console\Output\StreamOutput; |
| 18 |
|
| 19 |
/** |
| 20 |
* @author Amrouche Hamza <hamza.simperfit@gmail.com> |
| 21 |
*/ |
| 22 |
trait TesterTrait |
| 23 |
{ |
| 24 |
/** @var StreamOutput */ |
| 25 |
private $output; |
| 26 |
private $inputs = []; |
| 27 |
private $captureStreamsIndependently = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* Gets the display returned by the last execution of the command or application. |
| 31 |
* |
| 32 |
* @return string The display |
| 33 |
*/ |
| 34 |
public function getDisplay(bool $normalize = false) |
| 35 |
{ |
| 36 |
if (null === $this->output) { |
| 37 |
throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?'); |
| 38 |
} |
| 39 |
|
| 40 |
rewind($this->output->getStream()); |
| 41 |
|
| 42 |
$display = stream_get_contents($this->output->getStream()); |
| 43 |
|
| 44 |
if ($normalize) { |
| 45 |
$display = str_replace(\PHP_EOL, "\n", $display); |
| 46 |
} |
| 47 |
|
| 48 |
return $display; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Gets the output written to STDERR by the application. |
| 53 |
* |
| 54 |
* @param bool $normalize Whether to normalize end of lines to \n or not |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function getErrorOutput(bool $normalize = false) |
| 59 |
{ |
| 60 |
if (!$this->captureStreamsIndependently) { |
| 61 |
throw new \LogicException('The error output is not available when the tester is run without "capture_stderr_separately" option set.'); |
| 62 |
} |
| 63 |
|
| 64 |
rewind($this->output->getErrorOutput()->getStream()); |
| 65 |
|
| 66 |
$display = stream_get_contents($this->output->getErrorOutput()->getStream()); |
| 67 |
|
| 68 |
if ($normalize) { |
| 69 |
$display = str_replace(\PHP_EOL, "\n", $display); |
| 70 |
} |
| 71 |
|
| 72 |
return $display; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Gets the input instance used by the last execution of the command or application. |
| 77 |
* |
| 78 |
* @return InputInterface The current input instance |
| 79 |
*/ |
| 80 |
public function getInput() |
| 81 |
{ |
| 82 |
return $this->input; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Gets the output instance used by the last execution of the command or application. |
| 87 |
* |
| 88 |
* @return OutputInterface The current output instance |
| 89 |
*/ |
| 90 |
public function getOutput() |
| 91 |
{ |
| 92 |
return $this->output; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Gets the status code returned by the last execution of the command or application. |
| 97 |
* |
| 98 |
* @return int The status code |
| 99 |
*/ |
| 100 |
public function getStatusCode() |
| 101 |
{ |
| 102 |
return $this->statusCode; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Sets the user inputs. |
| 107 |
* |
| 108 |
* @param array $inputs An array of strings representing each input |
| 109 |
* passed to the command input stream |
| 110 |
* |
| 111 |
* @return $this |
| 112 |
*/ |
| 113 |
public function setInputs(array $inputs) |
| 114 |
{ |
| 115 |
$this->inputs = $inputs; |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Initializes the output property. |
| 122 |
* |
| 123 |
* Available options: |
| 124 |
* |
| 125 |
* * decorated: Sets the output decorated flag |
| 126 |
* * verbosity: Sets the output verbosity flag |
| 127 |
* * capture_stderr_separately: Make output of stdOut and stdErr separately available |
| 128 |
*/ |
| 129 |
private function initOutput(array $options) |
| 130 |
{ |
| 131 |
$this->captureStreamsIndependently = \array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately']; |
| 132 |
if (!$this->captureStreamsIndependently) { |
| 133 |
$this->output = new StreamOutput(fopen('php://memory', 'w', false)); |
| 134 |
if (isset($options['decorated'])) { |
| 135 |
$this->output->setDecorated($options['decorated']); |
| 136 |
} |
| 137 |
if (isset($options['verbosity'])) { |
| 138 |
$this->output->setVerbosity($options['verbosity']); |
| 139 |
} |
| 140 |
} else { |
| 141 |
$this->output = new ConsoleOutput( |
| 142 |
$options['verbosity'] ?? ConsoleOutput::VERBOSITY_NORMAL, |
| 143 |
$options['decorated'] ?? null |
| 144 |
); |
| 145 |
|
| 146 |
$errorOutput = new StreamOutput(fopen('php://memory', 'w', false)); |
| 147 |
$errorOutput->setFormatter($this->output->getFormatter()); |
| 148 |
$errorOutput->setVerbosity($this->output->getVerbosity()); |
| 149 |
$errorOutput->setDecorated($this->output->isDecorated()); |
| 150 |
|
| 151 |
$reflectedOutput = new \ReflectionObject($this->output); |
| 152 |
$strErrProperty = $reflectedOutput->getProperty('stderr'); |
| 153 |
$strErrProperty->setAccessible(true); |
| 154 |
$strErrProperty->setValue($this->output, $errorOutput); |
| 155 |
|
| 156 |
$reflectedParent = $reflectedOutput->getParentClass(); |
| 157 |
$streamProperty = $reflectedParent->getProperty('stream'); |
| 158 |
$streamProperty->setAccessible(true); |
| 159 |
$streamProperty->setValue($this->output, fopen('php://memory', 'w', false)); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @return resource |
| 165 |
*/ |
| 166 |
private static function createStream(array $inputs) |
| 167 |
{ |
| 168 |
$stream = fopen('php://memory', 'r+', false); |
| 169 |
|
| 170 |
foreach ($inputs as $input) { |
| 171 |
fwrite($stream, $input.\PHP_EOL); |
| 172 |
} |
| 173 |
|
| 174 |
rewind($stream); |
| 175 |
|
| 176 |
return $stream; |
| 177 |
} |
| 178 |
} |
| 179 |
|