PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / symfony / console / Tester / TesterTrait.php
matomo / app / vendor / prefixed / symfony / console / Tester Last commit date
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