Constraint
2 years ago
ApplicationTester.php
1 year ago
CommandCompletionTester.php
2 years ago
CommandTester.php
1 year ago
TesterTrait.php
1 year ago
TesterTrait.php
168 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\Console\Tester; |
| 12 | |
| 13 | use PHPUnit\Framework\Assert; |
| 14 | use Matomo\Dependencies\Symfony\Component\Console\Input\InputInterface; |
| 15 | use Matomo\Dependencies\Symfony\Component\Console\Output\ConsoleOutput; |
| 16 | use Matomo\Dependencies\Symfony\Component\Console\Output\OutputInterface; |
| 17 | use Matomo\Dependencies\Symfony\Component\Console\Output\StreamOutput; |
| 18 | use Matomo\Dependencies\Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful; |
| 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 | /** @var InputInterface */ |
| 29 | private $input; |
| 30 | /** @var int */ |
| 31 | private $statusCode; |
| 32 | /** |
| 33 | * Gets the display returned by the last execution of the command or application. |
| 34 | * |
| 35 | * @return string |
| 36 | * |
| 37 | * @throws \RuntimeException If it's called before the execute method |
| 38 | */ |
| 39 | public function getDisplay(bool $normalize = \false) |
| 40 | { |
| 41 | if (null === $this->output) { |
| 42 | throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?'); |
| 43 | } |
| 44 | rewind($this->output->getStream()); |
| 45 | $display = stream_get_contents($this->output->getStream()); |
| 46 | if ($normalize) { |
| 47 | $display = str_replace(\PHP_EOL, "\n", $display); |
| 48 | } |
| 49 | return $display; |
| 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 | rewind($this->output->getErrorOutput()->getStream()); |
| 64 | $display = stream_get_contents($this->output->getErrorOutput()->getStream()); |
| 65 | if ($normalize) { |
| 66 | $display = str_replace(\PHP_EOL, "\n", $display); |
| 67 | } |
| 68 | return $display; |
| 69 | } |
| 70 | /** |
| 71 | * Gets the input instance used by the last execution of the command or application. |
| 72 | * |
| 73 | * @return InputInterface |
| 74 | */ |
| 75 | public function getInput() |
| 76 | { |
| 77 | return $this->input; |
| 78 | } |
| 79 | /** |
| 80 | * Gets the output instance used by the last execution of the command or application. |
| 81 | * |
| 82 | * @return OutputInterface |
| 83 | */ |
| 84 | public function getOutput() |
| 85 | { |
| 86 | return $this->output; |
| 87 | } |
| 88 | /** |
| 89 | * Gets the status code returned by the last execution of the command or application. |
| 90 | * |
| 91 | * @return int |
| 92 | * |
| 93 | * @throws \RuntimeException If it's called before the execute method |
| 94 | */ |
| 95 | public function getStatusCode() |
| 96 | { |
| 97 | if (null === $this->statusCode) { |
| 98 | throw new \RuntimeException('Status code not initialized, did you execute the command before requesting the status code?'); |
| 99 | } |
| 100 | return $this->statusCode; |
| 101 | } |
| 102 | public function assertCommandIsSuccessful(string $message = '') : void |
| 103 | { |
| 104 | Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message); |
| 105 | } |
| 106 | /** |
| 107 | * Sets the user inputs. |
| 108 | * |
| 109 | * @param array $inputs An array of strings representing each input |
| 110 | * passed to the command input stream |
| 111 | * |
| 112 | * @return $this |
| 113 | */ |
| 114 | public function setInputs(array $inputs) |
| 115 | { |
| 116 | $this->inputs = $inputs; |
| 117 | return $this; |
| 118 | } |
| 119 | /** |
| 120 | * Initializes the output property. |
| 121 | * |
| 122 | * Available options: |
| 123 | * |
| 124 | * * decorated: Sets the output decorated flag |
| 125 | * * verbosity: Sets the output verbosity flag |
| 126 | * * capture_stderr_separately: Make output of stdOut and stdErr separately available |
| 127 | */ |
| 128 | private function initOutput(array $options) |
| 129 | { |
| 130 | $this->captureStreamsIndependently = \array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately']; |
| 131 | if (!$this->captureStreamsIndependently) { |
| 132 | $this->output = new StreamOutput(fopen('php://memory', 'w', \false)); |
| 133 | if (isset($options['decorated'])) { |
| 134 | $this->output->setDecorated($options['decorated']); |
| 135 | } |
| 136 | if (isset($options['verbosity'])) { |
| 137 | $this->output->setVerbosity($options['verbosity']); |
| 138 | } |
| 139 | } else { |
| 140 | $this->output = new ConsoleOutput($options['verbosity'] ?? ConsoleOutput::VERBOSITY_NORMAL, $options['decorated'] ?? null); |
| 141 | $errorOutput = new StreamOutput(fopen('php://memory', 'w', \false)); |
| 142 | $errorOutput->setFormatter($this->output->getFormatter()); |
| 143 | $errorOutput->setVerbosity($this->output->getVerbosity()); |
| 144 | $errorOutput->setDecorated($this->output->isDecorated()); |
| 145 | $reflectedOutput = new \ReflectionObject($this->output); |
| 146 | $strErrProperty = $reflectedOutput->getProperty('stderr'); |
| 147 | $strErrProperty->setAccessible(\true); |
| 148 | $strErrProperty->setValue($this->output, $errorOutput); |
| 149 | $reflectedParent = $reflectedOutput->getParentClass(); |
| 150 | $streamProperty = $reflectedParent->getProperty('stream'); |
| 151 | $streamProperty->setAccessible(\true); |
| 152 | $streamProperty->setValue($this->output, fopen('php://memory', 'w', \false)); |
| 153 | } |
| 154 | } |
| 155 | /** |
| 156 | * @return resource |
| 157 | */ |
| 158 | private static function createStream(array $inputs) |
| 159 | { |
| 160 | $stream = fopen('php://memory', 'r+', \false); |
| 161 | foreach ($inputs as $input) { |
| 162 | fwrite($stream, $input . \PHP_EOL); |
| 163 | } |
| 164 | rewind($stream); |
| 165 | return $stream; |
| 166 | } |
| 167 | } |
| 168 |