@@ -16,29 +16,29 @@ |
||
16 | 16 | |
17 | 17 | class TextTestListRenderer |
18 | 18 | { |
19 | - public function render(TestSuite $suite): string |
|
20 | - { |
|
21 | - $buffer = 'Available test(s):' . PHP_EOL; |
|
19 | + public function render(TestSuite $suite): string |
|
20 | + { |
|
21 | + $buffer = 'Available test(s):' . PHP_EOL; |
|
22 | 22 | |
23 | - foreach (new \RecursiveIteratorIterator($suite->getIterator()) as $test) { |
|
24 | - if ($test instanceof TestCase) { |
|
25 | - $name = \sprintf( |
|
26 | - '%s::%s', |
|
27 | - \get_class($test), |
|
28 | - \str_replace(' with data set ', '', $test->getName()) |
|
29 | - ); |
|
30 | - } elseif ($test instanceof PhptTestCase) { |
|
31 | - $name = $test->getName(); |
|
32 | - } else { |
|
33 | - continue; |
|
34 | - } |
|
23 | + foreach (new \RecursiveIteratorIterator($suite->getIterator()) as $test) { |
|
24 | + if ($test instanceof TestCase) { |
|
25 | + $name = \sprintf( |
|
26 | + '%s::%s', |
|
27 | + \get_class($test), |
|
28 | + \str_replace(' with data set ', '', $test->getName()) |
|
29 | + ); |
|
30 | + } elseif ($test instanceof PhptTestCase) { |
|
31 | + $name = $test->getName(); |
|
32 | + } else { |
|
33 | + continue; |
|
34 | + } |
|
35 | 35 | |
36 | - $buffer .= \sprintf( |
|
37 | - ' - %s' . PHP_EOL, |
|
38 | - $name |
|
39 | - ); |
|
40 | - } |
|
36 | + $buffer .= \sprintf( |
|
37 | + ' - %s' . PHP_EOL, |
|
38 | + $name |
|
39 | + ); |
|
40 | + } |
|
41 | 41 | |
42 | - return $buffer; |
|
43 | - } |
|
42 | + return $buffer; |
|
43 | + } |
|
44 | 44 | } |
@@ -16,65 +16,65 @@ |
||
16 | 16 | */ |
17 | 17 | class Fileloader |
18 | 18 | { |
19 | - /** |
|
20 | - * Checks if a PHP sourcefile is readable. |
|
21 | - * The sourcefile is loaded through the load() method. |
|
22 | - * |
|
23 | - * @param string $filename |
|
24 | - * |
|
25 | - * @return string |
|
26 | - * |
|
27 | - * @throws Exception |
|
28 | - */ |
|
29 | - public static function checkAndLoad($filename) |
|
30 | - { |
|
31 | - $includePathFilename = \stream_resolve_include_path($filename); |
|
19 | + /** |
|
20 | + * Checks if a PHP sourcefile is readable. |
|
21 | + * The sourcefile is loaded through the load() method. |
|
22 | + * |
|
23 | + * @param string $filename |
|
24 | + * |
|
25 | + * @return string |
|
26 | + * |
|
27 | + * @throws Exception |
|
28 | + */ |
|
29 | + public static function checkAndLoad($filename) |
|
30 | + { |
|
31 | + $includePathFilename = \stream_resolve_include_path($filename); |
|
32 | 32 | |
33 | - // As a fallback, PHP looks in the directory of the file executing the stream_resolve_include_path function. |
|
34 | - // We don't want to load the Test.php file here, so skip it if it found that. |
|
35 | - // PHP prioritizes the include_path setting, so if the current directory is in there, it will first look in the |
|
36 | - // current working directory. |
|
37 | - $localFile = __DIR__ . DIRECTORY_SEPARATOR . $filename; |
|
33 | + // As a fallback, PHP looks in the directory of the file executing the stream_resolve_include_path function. |
|
34 | + // We don't want to load the Test.php file here, so skip it if it found that. |
|
35 | + // PHP prioritizes the include_path setting, so if the current directory is in there, it will first look in the |
|
36 | + // current working directory. |
|
37 | + $localFile = __DIR__ . DIRECTORY_SEPARATOR . $filename; |
|
38 | 38 | |
39 | - // @see https://github.com/sebastianbergmann/phpunit/pull/2751 |
|
40 | - $isReadable = @\fopen($includePathFilename, 'r') !== false; |
|
39 | + // @see https://github.com/sebastianbergmann/phpunit/pull/2751 |
|
40 | + $isReadable = @\fopen($includePathFilename, 'r') !== false; |
|
41 | 41 | |
42 | - if (!$includePathFilename || !$isReadable || $includePathFilename === $localFile) { |
|
43 | - throw new Exception( |
|
44 | - \sprintf('Cannot open file "%s".' . "\n", $filename) |
|
45 | - ); |
|
46 | - } |
|
42 | + if (!$includePathFilename || !$isReadable || $includePathFilename === $localFile) { |
|
43 | + throw new Exception( |
|
44 | + \sprintf('Cannot open file "%s".' . "\n", $filename) |
|
45 | + ); |
|
46 | + } |
|
47 | 47 | |
48 | - self::load($includePathFilename); |
|
48 | + self::load($includePathFilename); |
|
49 | 49 | |
50 | - return $includePathFilename; |
|
51 | - } |
|
50 | + return $includePathFilename; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Loads a PHP sourcefile. |
|
55 | - * |
|
56 | - * @param string $filename |
|
57 | - * |
|
58 | - * @return mixed |
|
59 | - */ |
|
60 | - public static function load($filename) |
|
61 | - { |
|
62 | - $oldVariableNames = \array_keys(\get_defined_vars()); |
|
53 | + /** |
|
54 | + * Loads a PHP sourcefile. |
|
55 | + * |
|
56 | + * @param string $filename |
|
57 | + * |
|
58 | + * @return mixed |
|
59 | + */ |
|
60 | + public static function load($filename) |
|
61 | + { |
|
62 | + $oldVariableNames = \array_keys(\get_defined_vars()); |
|
63 | 63 | |
64 | - include_once $filename; |
|
64 | + include_once $filename; |
|
65 | 65 | |
66 | - $newVariables = \get_defined_vars(); |
|
67 | - $newVariableNames = \array_diff( |
|
68 | - \array_keys($newVariables), |
|
69 | - $oldVariableNames |
|
70 | - ); |
|
66 | + $newVariables = \get_defined_vars(); |
|
67 | + $newVariableNames = \array_diff( |
|
68 | + \array_keys($newVariables), |
|
69 | + $oldVariableNames |
|
70 | + ); |
|
71 | 71 | |
72 | - foreach ($newVariableNames as $variableName) { |
|
73 | - if ($variableName != 'oldVariableNames') { |
|
74 | - $GLOBALS[$variableName] = $newVariables[$variableName]; |
|
75 | - } |
|
76 | - } |
|
72 | + foreach ($newVariableNames as $variableName) { |
|
73 | + if ($variableName != 'oldVariableNames') { |
|
74 | + $GLOBALS[$variableName] = $newVariables[$variableName]; |
|
75 | + } |
|
76 | + } |
|
77 | 77 | |
78 | - return $filename; |
|
79 | - } |
|
78 | + return $filename; |
|
79 | + } |
|
80 | 80 | } |
@@ -15,29 +15,29 @@ |
||
15 | 15 | */ |
16 | 16 | class Type |
17 | 17 | { |
18 | - /** |
|
19 | - * @param string $type |
|
20 | - * |
|
21 | - * @return bool |
|
22 | - */ |
|
23 | - public static function isType($type) |
|
24 | - { |
|
25 | - return \in_array( |
|
26 | - $type, |
|
27 | - [ |
|
28 | - 'numeric', |
|
29 | - 'integer', |
|
30 | - 'int', |
|
31 | - 'float', |
|
32 | - 'string', |
|
33 | - 'boolean', |
|
34 | - 'bool', |
|
35 | - 'null', |
|
36 | - 'array', |
|
37 | - 'object', |
|
38 | - 'resource', |
|
39 | - 'scalar' |
|
40 | - ] |
|
41 | - ); |
|
42 | - } |
|
18 | + /** |
|
19 | + * @param string $type |
|
20 | + * |
|
21 | + * @return bool |
|
22 | + */ |
|
23 | + public static function isType($type) |
|
24 | + { |
|
25 | + return \in_array( |
|
26 | + $type, |
|
27 | + [ |
|
28 | + 'numeric', |
|
29 | + 'integer', |
|
30 | + 'int', |
|
31 | + 'float', |
|
32 | + 'string', |
|
33 | + 'boolean', |
|
34 | + 'bool', |
|
35 | + 'null', |
|
36 | + 'array', |
|
37 | + 'object', |
|
38 | + 'resource', |
|
39 | + 'scalar' |
|
40 | + ] |
|
41 | + ); |
|
42 | + } |
|
43 | 43 | } |
@@ -15,26 +15,26 @@ |
||
15 | 15 | */ |
16 | 16 | class Filesystem |
17 | 17 | { |
18 | - /** |
|
19 | - * @var array |
|
20 | - */ |
|
21 | - protected static $buffer = []; |
|
18 | + /** |
|
19 | + * @var array |
|
20 | + */ |
|
21 | + protected static $buffer = []; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Maps class names to source file names: |
|
25 | - * - PEAR CS: Foo_Bar_Baz -> Foo/Bar/Baz.php |
|
26 | - * - Namespace: Foo\Bar\Baz -> Foo/Bar/Baz.php |
|
27 | - * |
|
28 | - * @param string $className |
|
29 | - * |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - public static function classNameToFilename($className) |
|
33 | - { |
|
34 | - return \str_replace( |
|
35 | - ['_', '\\'], |
|
36 | - DIRECTORY_SEPARATOR, |
|
37 | - $className |
|
38 | - ) . '.php'; |
|
39 | - } |
|
23 | + /** |
|
24 | + * Maps class names to source file names: |
|
25 | + * - PEAR CS: Foo_Bar_Baz -> Foo/Bar/Baz.php |
|
26 | + * - Namespace: Foo\Bar\Baz -> Foo/Bar/Baz.php |
|
27 | + * |
|
28 | + * @param string $className |
|
29 | + * |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + public static function classNameToFilename($className) |
|
33 | + { |
|
34 | + return \str_replace( |
|
35 | + ['_', '\\'], |
|
36 | + DIRECTORY_SEPARATOR, |
|
37 | + $className |
|
38 | + ) . '.php'; |
|
39 | + } |
|
40 | 40 | } |
@@ -21,23 +21,23 @@ |
||
21 | 21 | */ |
22 | 22 | class WindowsPhpProcess extends DefaultPhpProcess |
23 | 23 | { |
24 | - protected $useTempFile = true; |
|
24 | + protected $useTempFile = true; |
|
25 | 25 | |
26 | - protected function getHandles() |
|
27 | - { |
|
28 | - if (false === $stdout_handle = \tmpfile()) { |
|
29 | - throw new Exception( |
|
30 | - 'A temporary file could not be created; verify that your TEMP environment variable is writable' |
|
31 | - ); |
|
32 | - } |
|
26 | + protected function getHandles() |
|
27 | + { |
|
28 | + if (false === $stdout_handle = \tmpfile()) { |
|
29 | + throw new Exception( |
|
30 | + 'A temporary file could not be created; verify that your TEMP environment variable is writable' |
|
31 | + ); |
|
32 | + } |
|
33 | 33 | |
34 | - return [ |
|
35 | - 1 => $stdout_handle |
|
36 | - ]; |
|
37 | - } |
|
34 | + return [ |
|
35 | + 1 => $stdout_handle |
|
36 | + ]; |
|
37 | + } |
|
38 | 38 | |
39 | - public function getCommand(array $settings, $file = null) |
|
40 | - { |
|
41 | - return '"' . parent::getCommand($settings, $file) . '"'; |
|
42 | - } |
|
39 | + public function getCommand(array $settings, $file = null) |
|
40 | + { |
|
41 | + return '"' . parent::getCommand($settings, $file) . '"'; |
|
42 | + } |
|
43 | 43 | } |
@@ -25,397 +25,397 @@ |
||
25 | 25 | */ |
26 | 26 | abstract class AbstractPhpProcess |
27 | 27 | { |
28 | - /** |
|
29 | - * @var Runtime |
|
30 | - */ |
|
31 | - protected $runtime; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var bool |
|
35 | - */ |
|
36 | - protected $stderrRedirection = false; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - protected $stdin = ''; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $args = ''; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var array<string, string> |
|
50 | - */ |
|
51 | - protected $env = []; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var int |
|
55 | - */ |
|
56 | - protected $timeout = 0; |
|
57 | - |
|
58 | - /** |
|
59 | - * Creates internal Runtime instance. |
|
60 | - */ |
|
61 | - public function __construct() |
|
62 | - { |
|
63 | - $this->runtime = new Runtime(); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Defines if should use STDERR redirection or not. |
|
68 | - * |
|
69 | - * Then $stderrRedirection is TRUE, STDERR is redirected to STDOUT. |
|
70 | - * |
|
71 | - * @throws Exception |
|
72 | - * |
|
73 | - * @param bool $stderrRedirection |
|
74 | - */ |
|
75 | - public function setUseStderrRedirection($stderrRedirection) |
|
76 | - { |
|
77 | - if (!\is_bool($stderrRedirection)) { |
|
78 | - throw InvalidArgumentHelper::factory(1, 'boolean'); |
|
79 | - } |
|
80 | - |
|
81 | - $this->stderrRedirection = $stderrRedirection; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Returns TRUE if uses STDERR redirection or FALSE if not. |
|
86 | - * |
|
87 | - * @return bool |
|
88 | - */ |
|
89 | - public function useStderrRedirection() |
|
90 | - { |
|
91 | - return $this->stderrRedirection; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Sets the input string to be sent via STDIN |
|
96 | - * |
|
97 | - * @param string $stdin |
|
98 | - */ |
|
99 | - public function setStdin($stdin) |
|
100 | - { |
|
101 | - $this->stdin = (string) $stdin; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Returns the input string to be sent via STDIN |
|
106 | - * |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function getStdin() |
|
110 | - { |
|
111 | - return $this->stdin; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Sets the string of arguments to pass to the php job |
|
116 | - * |
|
117 | - * @param string $args |
|
118 | - */ |
|
119 | - public function setArgs($args) |
|
120 | - { |
|
121 | - $this->args = (string) $args; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Returns the string of arguments to pass to the php job |
|
126 | - * |
|
127 | - * @retrun string |
|
128 | - */ |
|
129 | - public function getArgs() |
|
130 | - { |
|
131 | - return $this->args; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Sets the array of environment variables to start the child process with |
|
136 | - * |
|
137 | - * @param array<string, string> $env |
|
138 | - */ |
|
139 | - public function setEnv(array $env) |
|
140 | - { |
|
141 | - $this->env = $env; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Returns the array of environment variables to start the child process with |
|
146 | - * |
|
147 | - * @return array<string, string> |
|
148 | - */ |
|
149 | - public function getEnv() |
|
150 | - { |
|
151 | - return $this->env; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Sets the amount of seconds to wait before timing out |
|
156 | - * |
|
157 | - * @param int $timeout |
|
158 | - */ |
|
159 | - public function setTimeout($timeout) |
|
160 | - { |
|
161 | - $this->timeout = (int) $timeout; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Returns the amount of seconds to wait before timing out |
|
166 | - * |
|
167 | - * @return int |
|
168 | - */ |
|
169 | - public function getTimeout() |
|
170 | - { |
|
171 | - return $this->timeout; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @return AbstractPhpProcess |
|
176 | - */ |
|
177 | - public static function factory() |
|
178 | - { |
|
179 | - if (DIRECTORY_SEPARATOR == '\\') { |
|
180 | - return new WindowsPhpProcess; |
|
181 | - } |
|
182 | - |
|
183 | - return new DefaultPhpProcess; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Runs a single test in a separate PHP process. |
|
188 | - * |
|
189 | - * @param string $job |
|
190 | - * @param Test $test |
|
191 | - * @param TestResult $result |
|
192 | - * |
|
193 | - * @throws Exception |
|
194 | - */ |
|
195 | - public function runTestJob($job, Test $test, TestResult $result) |
|
196 | - { |
|
197 | - $result->startTest($test); |
|
198 | - |
|
199 | - $_result = $this->runJob($job); |
|
200 | - |
|
201 | - $this->processChildResult( |
|
202 | - $test, |
|
203 | - $result, |
|
204 | - $_result['stdout'], |
|
205 | - $_result['stderr'] |
|
206 | - ); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Returns the command based into the configurations. |
|
211 | - * |
|
212 | - * @param array $settings |
|
213 | - * @param string|null $file |
|
214 | - * |
|
215 | - * @return string |
|
216 | - */ |
|
217 | - public function getCommand(array $settings, $file = null) |
|
218 | - { |
|
219 | - $command = $this->runtime->getBinary(); |
|
220 | - $command .= $this->settingsToParameters($settings); |
|
221 | - |
|
222 | - if ('phpdbg' === PHP_SAPI) { |
|
223 | - $command .= ' -qrr '; |
|
224 | - |
|
225 | - if ($file) { |
|
226 | - $command .= '-e ' . \escapeshellarg($file); |
|
227 | - } else { |
|
228 | - $command .= \escapeshellarg(__DIR__ . '/eval-stdin.php'); |
|
229 | - } |
|
230 | - } elseif ($file) { |
|
231 | - $command .= ' -f ' . \escapeshellarg($file); |
|
232 | - } |
|
233 | - |
|
234 | - if ($this->args) { |
|
235 | - $command .= ' -- ' . $this->args; |
|
236 | - } |
|
237 | - |
|
238 | - if (true === $this->stderrRedirection) { |
|
239 | - $command .= ' 2>&1'; |
|
240 | - } |
|
241 | - |
|
242 | - return $command; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Runs a single job (PHP code) using a separate PHP process. |
|
247 | - * |
|
248 | - * @param string $job |
|
249 | - * @param array $settings |
|
250 | - * |
|
251 | - * @return array |
|
252 | - * |
|
253 | - * @throws Exception |
|
254 | - */ |
|
255 | - abstract public function runJob($job, array $settings = []); |
|
256 | - |
|
257 | - /** |
|
258 | - * @param array $settings |
|
259 | - * |
|
260 | - * @return string |
|
261 | - */ |
|
262 | - protected function settingsToParameters(array $settings) |
|
263 | - { |
|
264 | - $buffer = ''; |
|
265 | - |
|
266 | - foreach ($settings as $setting) { |
|
267 | - $buffer .= ' -d ' . $setting; |
|
268 | - } |
|
269 | - |
|
270 | - return $buffer; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Processes the TestResult object from an isolated process. |
|
275 | - * |
|
276 | - * @param Test $test |
|
277 | - * @param TestResult $result |
|
278 | - * @param string $stdout |
|
279 | - * @param string $stderr |
|
280 | - */ |
|
281 | - private function processChildResult(Test $test, TestResult $result, $stdout, $stderr) |
|
282 | - { |
|
283 | - $time = 0; |
|
284 | - |
|
285 | - if (!empty($stderr)) { |
|
286 | - $result->addError( |
|
287 | - $test, |
|
288 | - new Exception(\trim($stderr)), |
|
289 | - $time |
|
290 | - ); |
|
291 | - } else { |
|
292 | - \set_error_handler(function ($errno, $errstr, $errfile, $errline) { |
|
293 | - throw new ErrorException($errstr, $errno, $errno, $errfile, $errline); |
|
294 | - }); |
|
295 | - |
|
296 | - try { |
|
297 | - if (\strpos($stdout, "#!/usr/bin/env php\n") === 0) { |
|
298 | - $stdout = \substr($stdout, 19); |
|
299 | - } |
|
300 | - |
|
301 | - $childResult = \unserialize(\str_replace("#!/usr/bin/env php\n", '', $stdout)); |
|
302 | - \restore_error_handler(); |
|
303 | - } catch (ErrorException $e) { |
|
304 | - \restore_error_handler(); |
|
305 | - $childResult = false; |
|
306 | - |
|
307 | - $result->addError( |
|
308 | - $test, |
|
309 | - new Exception(\trim($stdout), 0, $e), |
|
310 | - $time |
|
311 | - ); |
|
312 | - } |
|
313 | - |
|
314 | - if ($childResult !== false) { |
|
315 | - if (!empty($childResult['output'])) { |
|
316 | - $output = $childResult['output']; |
|
317 | - } |
|
318 | - |
|
319 | - $test->setResult($childResult['testResult']); |
|
320 | - $test->addToAssertionCount($childResult['numAssertions']); |
|
321 | - |
|
322 | - /** @var TestResult $childResult */ |
|
323 | - $childResult = $childResult['result']; |
|
324 | - |
|
325 | - if ($result->getCollectCodeCoverageInformation()) { |
|
326 | - $result->getCodeCoverage()->merge( |
|
327 | - $childResult->getCodeCoverage() |
|
328 | - ); |
|
329 | - } |
|
330 | - |
|
331 | - $time = $childResult->time(); |
|
332 | - $notImplemented = $childResult->notImplemented(); |
|
333 | - $risky = $childResult->risky(); |
|
334 | - $skipped = $childResult->skipped(); |
|
335 | - $errors = $childResult->errors(); |
|
336 | - $warnings = $childResult->warnings(); |
|
337 | - $failures = $childResult->failures(); |
|
338 | - |
|
339 | - if (!empty($notImplemented)) { |
|
340 | - $result->addError( |
|
341 | - $test, |
|
342 | - $this->getException($notImplemented[0]), |
|
343 | - $time |
|
344 | - ); |
|
345 | - } elseif (!empty($risky)) { |
|
346 | - $result->addError( |
|
347 | - $test, |
|
348 | - $this->getException($risky[0]), |
|
349 | - $time |
|
350 | - ); |
|
351 | - } elseif (!empty($skipped)) { |
|
352 | - $result->addError( |
|
353 | - $test, |
|
354 | - $this->getException($skipped[0]), |
|
355 | - $time |
|
356 | - ); |
|
357 | - } elseif (!empty($errors)) { |
|
358 | - $result->addError( |
|
359 | - $test, |
|
360 | - $this->getException($errors[0]), |
|
361 | - $time |
|
362 | - ); |
|
363 | - } elseif (!empty($warnings)) { |
|
364 | - $result->addWarning( |
|
365 | - $test, |
|
366 | - $this->getException($warnings[0]), |
|
367 | - $time |
|
368 | - ); |
|
369 | - } elseif (!empty($failures)) { |
|
370 | - $result->addFailure( |
|
371 | - $test, |
|
372 | - $this->getException($failures[0]), |
|
373 | - $time |
|
374 | - ); |
|
375 | - } |
|
376 | - } |
|
377 | - } |
|
378 | - |
|
379 | - $result->endTest($test, $time); |
|
380 | - |
|
381 | - if (!empty($output)) { |
|
382 | - print $output; |
|
383 | - } |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Gets the thrown exception from a PHPUnit\Framework\TestFailure. |
|
388 | - * |
|
389 | - * @param TestFailure $error |
|
390 | - * |
|
391 | - * @return Exception |
|
392 | - * |
|
393 | - * @see https://github.com/sebastianbergmann/phpunit/issues/74 |
|
394 | - */ |
|
395 | - private function getException(TestFailure $error) |
|
396 | - { |
|
397 | - $exception = $error->thrownException(); |
|
398 | - |
|
399 | - if ($exception instanceof __PHP_Incomplete_Class) { |
|
400 | - $exceptionArray = []; |
|
401 | - foreach ((array) $exception as $key => $value) { |
|
402 | - $key = \substr($key, \strrpos($key, "\0") + 1); |
|
403 | - $exceptionArray[$key] = $value; |
|
404 | - } |
|
405 | - |
|
406 | - $exception = new SyntheticError( |
|
407 | - \sprintf( |
|
408 | - '%s: %s', |
|
409 | - $exceptionArray['_PHP_Incomplete_Class_Name'], |
|
410 | - $exceptionArray['message'] |
|
411 | - ), |
|
412 | - $exceptionArray['code'], |
|
413 | - $exceptionArray['file'], |
|
414 | - $exceptionArray['line'], |
|
415 | - $exceptionArray['trace'] |
|
416 | - ); |
|
417 | - } |
|
418 | - |
|
419 | - return $exception; |
|
420 | - } |
|
28 | + /** |
|
29 | + * @var Runtime |
|
30 | + */ |
|
31 | + protected $runtime; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var bool |
|
35 | + */ |
|
36 | + protected $stderrRedirection = false; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + protected $stdin = ''; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $args = ''; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var array<string, string> |
|
50 | + */ |
|
51 | + protected $env = []; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var int |
|
55 | + */ |
|
56 | + protected $timeout = 0; |
|
57 | + |
|
58 | + /** |
|
59 | + * Creates internal Runtime instance. |
|
60 | + */ |
|
61 | + public function __construct() |
|
62 | + { |
|
63 | + $this->runtime = new Runtime(); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Defines if should use STDERR redirection or not. |
|
68 | + * |
|
69 | + * Then $stderrRedirection is TRUE, STDERR is redirected to STDOUT. |
|
70 | + * |
|
71 | + * @throws Exception |
|
72 | + * |
|
73 | + * @param bool $stderrRedirection |
|
74 | + */ |
|
75 | + public function setUseStderrRedirection($stderrRedirection) |
|
76 | + { |
|
77 | + if (!\is_bool($stderrRedirection)) { |
|
78 | + throw InvalidArgumentHelper::factory(1, 'boolean'); |
|
79 | + } |
|
80 | + |
|
81 | + $this->stderrRedirection = $stderrRedirection; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Returns TRUE if uses STDERR redirection or FALSE if not. |
|
86 | + * |
|
87 | + * @return bool |
|
88 | + */ |
|
89 | + public function useStderrRedirection() |
|
90 | + { |
|
91 | + return $this->stderrRedirection; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Sets the input string to be sent via STDIN |
|
96 | + * |
|
97 | + * @param string $stdin |
|
98 | + */ |
|
99 | + public function setStdin($stdin) |
|
100 | + { |
|
101 | + $this->stdin = (string) $stdin; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Returns the input string to be sent via STDIN |
|
106 | + * |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function getStdin() |
|
110 | + { |
|
111 | + return $this->stdin; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Sets the string of arguments to pass to the php job |
|
116 | + * |
|
117 | + * @param string $args |
|
118 | + */ |
|
119 | + public function setArgs($args) |
|
120 | + { |
|
121 | + $this->args = (string) $args; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Returns the string of arguments to pass to the php job |
|
126 | + * |
|
127 | + * @retrun string |
|
128 | + */ |
|
129 | + public function getArgs() |
|
130 | + { |
|
131 | + return $this->args; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Sets the array of environment variables to start the child process with |
|
136 | + * |
|
137 | + * @param array<string, string> $env |
|
138 | + */ |
|
139 | + public function setEnv(array $env) |
|
140 | + { |
|
141 | + $this->env = $env; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Returns the array of environment variables to start the child process with |
|
146 | + * |
|
147 | + * @return array<string, string> |
|
148 | + */ |
|
149 | + public function getEnv() |
|
150 | + { |
|
151 | + return $this->env; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Sets the amount of seconds to wait before timing out |
|
156 | + * |
|
157 | + * @param int $timeout |
|
158 | + */ |
|
159 | + public function setTimeout($timeout) |
|
160 | + { |
|
161 | + $this->timeout = (int) $timeout; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Returns the amount of seconds to wait before timing out |
|
166 | + * |
|
167 | + * @return int |
|
168 | + */ |
|
169 | + public function getTimeout() |
|
170 | + { |
|
171 | + return $this->timeout; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @return AbstractPhpProcess |
|
176 | + */ |
|
177 | + public static function factory() |
|
178 | + { |
|
179 | + if (DIRECTORY_SEPARATOR == '\\') { |
|
180 | + return new WindowsPhpProcess; |
|
181 | + } |
|
182 | + |
|
183 | + return new DefaultPhpProcess; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Runs a single test in a separate PHP process. |
|
188 | + * |
|
189 | + * @param string $job |
|
190 | + * @param Test $test |
|
191 | + * @param TestResult $result |
|
192 | + * |
|
193 | + * @throws Exception |
|
194 | + */ |
|
195 | + public function runTestJob($job, Test $test, TestResult $result) |
|
196 | + { |
|
197 | + $result->startTest($test); |
|
198 | + |
|
199 | + $_result = $this->runJob($job); |
|
200 | + |
|
201 | + $this->processChildResult( |
|
202 | + $test, |
|
203 | + $result, |
|
204 | + $_result['stdout'], |
|
205 | + $_result['stderr'] |
|
206 | + ); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Returns the command based into the configurations. |
|
211 | + * |
|
212 | + * @param array $settings |
|
213 | + * @param string|null $file |
|
214 | + * |
|
215 | + * @return string |
|
216 | + */ |
|
217 | + public function getCommand(array $settings, $file = null) |
|
218 | + { |
|
219 | + $command = $this->runtime->getBinary(); |
|
220 | + $command .= $this->settingsToParameters($settings); |
|
221 | + |
|
222 | + if ('phpdbg' === PHP_SAPI) { |
|
223 | + $command .= ' -qrr '; |
|
224 | + |
|
225 | + if ($file) { |
|
226 | + $command .= '-e ' . \escapeshellarg($file); |
|
227 | + } else { |
|
228 | + $command .= \escapeshellarg(__DIR__ . '/eval-stdin.php'); |
|
229 | + } |
|
230 | + } elseif ($file) { |
|
231 | + $command .= ' -f ' . \escapeshellarg($file); |
|
232 | + } |
|
233 | + |
|
234 | + if ($this->args) { |
|
235 | + $command .= ' -- ' . $this->args; |
|
236 | + } |
|
237 | + |
|
238 | + if (true === $this->stderrRedirection) { |
|
239 | + $command .= ' 2>&1'; |
|
240 | + } |
|
241 | + |
|
242 | + return $command; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Runs a single job (PHP code) using a separate PHP process. |
|
247 | + * |
|
248 | + * @param string $job |
|
249 | + * @param array $settings |
|
250 | + * |
|
251 | + * @return array |
|
252 | + * |
|
253 | + * @throws Exception |
|
254 | + */ |
|
255 | + abstract public function runJob($job, array $settings = []); |
|
256 | + |
|
257 | + /** |
|
258 | + * @param array $settings |
|
259 | + * |
|
260 | + * @return string |
|
261 | + */ |
|
262 | + protected function settingsToParameters(array $settings) |
|
263 | + { |
|
264 | + $buffer = ''; |
|
265 | + |
|
266 | + foreach ($settings as $setting) { |
|
267 | + $buffer .= ' -d ' . $setting; |
|
268 | + } |
|
269 | + |
|
270 | + return $buffer; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Processes the TestResult object from an isolated process. |
|
275 | + * |
|
276 | + * @param Test $test |
|
277 | + * @param TestResult $result |
|
278 | + * @param string $stdout |
|
279 | + * @param string $stderr |
|
280 | + */ |
|
281 | + private function processChildResult(Test $test, TestResult $result, $stdout, $stderr) |
|
282 | + { |
|
283 | + $time = 0; |
|
284 | + |
|
285 | + if (!empty($stderr)) { |
|
286 | + $result->addError( |
|
287 | + $test, |
|
288 | + new Exception(\trim($stderr)), |
|
289 | + $time |
|
290 | + ); |
|
291 | + } else { |
|
292 | + \set_error_handler(function ($errno, $errstr, $errfile, $errline) { |
|
293 | + throw new ErrorException($errstr, $errno, $errno, $errfile, $errline); |
|
294 | + }); |
|
295 | + |
|
296 | + try { |
|
297 | + if (\strpos($stdout, "#!/usr/bin/env php\n") === 0) { |
|
298 | + $stdout = \substr($stdout, 19); |
|
299 | + } |
|
300 | + |
|
301 | + $childResult = \unserialize(\str_replace("#!/usr/bin/env php\n", '', $stdout)); |
|
302 | + \restore_error_handler(); |
|
303 | + } catch (ErrorException $e) { |
|
304 | + \restore_error_handler(); |
|
305 | + $childResult = false; |
|
306 | + |
|
307 | + $result->addError( |
|
308 | + $test, |
|
309 | + new Exception(\trim($stdout), 0, $e), |
|
310 | + $time |
|
311 | + ); |
|
312 | + } |
|
313 | + |
|
314 | + if ($childResult !== false) { |
|
315 | + if (!empty($childResult['output'])) { |
|
316 | + $output = $childResult['output']; |
|
317 | + } |
|
318 | + |
|
319 | + $test->setResult($childResult['testResult']); |
|
320 | + $test->addToAssertionCount($childResult['numAssertions']); |
|
321 | + |
|
322 | + /** @var TestResult $childResult */ |
|
323 | + $childResult = $childResult['result']; |
|
324 | + |
|
325 | + if ($result->getCollectCodeCoverageInformation()) { |
|
326 | + $result->getCodeCoverage()->merge( |
|
327 | + $childResult->getCodeCoverage() |
|
328 | + ); |
|
329 | + } |
|
330 | + |
|
331 | + $time = $childResult->time(); |
|
332 | + $notImplemented = $childResult->notImplemented(); |
|
333 | + $risky = $childResult->risky(); |
|
334 | + $skipped = $childResult->skipped(); |
|
335 | + $errors = $childResult->errors(); |
|
336 | + $warnings = $childResult->warnings(); |
|
337 | + $failures = $childResult->failures(); |
|
338 | + |
|
339 | + if (!empty($notImplemented)) { |
|
340 | + $result->addError( |
|
341 | + $test, |
|
342 | + $this->getException($notImplemented[0]), |
|
343 | + $time |
|
344 | + ); |
|
345 | + } elseif (!empty($risky)) { |
|
346 | + $result->addError( |
|
347 | + $test, |
|
348 | + $this->getException($risky[0]), |
|
349 | + $time |
|
350 | + ); |
|
351 | + } elseif (!empty($skipped)) { |
|
352 | + $result->addError( |
|
353 | + $test, |
|
354 | + $this->getException($skipped[0]), |
|
355 | + $time |
|
356 | + ); |
|
357 | + } elseif (!empty($errors)) { |
|
358 | + $result->addError( |
|
359 | + $test, |
|
360 | + $this->getException($errors[0]), |
|
361 | + $time |
|
362 | + ); |
|
363 | + } elseif (!empty($warnings)) { |
|
364 | + $result->addWarning( |
|
365 | + $test, |
|
366 | + $this->getException($warnings[0]), |
|
367 | + $time |
|
368 | + ); |
|
369 | + } elseif (!empty($failures)) { |
|
370 | + $result->addFailure( |
|
371 | + $test, |
|
372 | + $this->getException($failures[0]), |
|
373 | + $time |
|
374 | + ); |
|
375 | + } |
|
376 | + } |
|
377 | + } |
|
378 | + |
|
379 | + $result->endTest($test, $time); |
|
380 | + |
|
381 | + if (!empty($output)) { |
|
382 | + print $output; |
|
383 | + } |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Gets the thrown exception from a PHPUnit\Framework\TestFailure. |
|
388 | + * |
|
389 | + * @param TestFailure $error |
|
390 | + * |
|
391 | + * @return Exception |
|
392 | + * |
|
393 | + * @see https://github.com/sebastianbergmann/phpunit/issues/74 |
|
394 | + */ |
|
395 | + private function getException(TestFailure $error) |
|
396 | + { |
|
397 | + $exception = $error->thrownException(); |
|
398 | + |
|
399 | + if ($exception instanceof __PHP_Incomplete_Class) { |
|
400 | + $exceptionArray = []; |
|
401 | + foreach ((array) $exception as $key => $value) { |
|
402 | + $key = \substr($key, \strrpos($key, "\0") + 1); |
|
403 | + $exceptionArray[$key] = $value; |
|
404 | + } |
|
405 | + |
|
406 | + $exception = new SyntheticError( |
|
407 | + \sprintf( |
|
408 | + '%s: %s', |
|
409 | + $exceptionArray['_PHP_Incomplete_Class_Name'], |
|
410 | + $exceptionArray['message'] |
|
411 | + ), |
|
412 | + $exceptionArray['code'], |
|
413 | + $exceptionArray['file'], |
|
414 | + $exceptionArray['line'], |
|
415 | + $exceptionArray['trace'] |
|
416 | + ); |
|
417 | + } |
|
418 | + |
|
419 | + return $exception; |
|
420 | + } |
|
421 | 421 | } |
@@ -16,217 +16,217 @@ |
||
16 | 16 | */ |
17 | 17 | class DefaultPhpProcess extends AbstractPhpProcess |
18 | 18 | { |
19 | - /** |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $tempFile; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var bool |
|
26 | - */ |
|
27 | - protected $useTempFile = false; |
|
28 | - |
|
29 | - /** |
|
30 | - * Runs a single job (PHP code) using a separate PHP process. |
|
31 | - * |
|
32 | - * @param string $job |
|
33 | - * @param array $settings |
|
34 | - * |
|
35 | - * @return array<string, string> |
|
36 | - * |
|
37 | - * @throws Exception |
|
38 | - */ |
|
39 | - public function runJob($job, array $settings = []) |
|
40 | - { |
|
41 | - if ($this->useTempFile || $this->stdin) { |
|
42 | - if (!($this->tempFile = \tempnam(\sys_get_temp_dir(), 'PHPUnit')) || |
|
43 | - \file_put_contents($this->tempFile, $job) === false) { |
|
44 | - throw new Exception( |
|
45 | - 'Unable to write temporary file' |
|
46 | - ); |
|
47 | - } |
|
48 | - |
|
49 | - $job = $this->stdin; |
|
50 | - } |
|
51 | - |
|
52 | - return $this->runProcess($job, $settings); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Returns an array of file handles to be used in place of pipes |
|
57 | - * |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - protected function getHandles() |
|
61 | - { |
|
62 | - return []; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Handles creating the child process and returning the STDOUT and STDERR |
|
67 | - * |
|
68 | - * @param string $job |
|
69 | - * @param array $settings |
|
70 | - * |
|
71 | - * @return array<string, string> |
|
72 | - * |
|
73 | - * @throws Exception |
|
74 | - */ |
|
75 | - protected function runProcess($job, $settings) |
|
76 | - { |
|
77 | - $handles = $this->getHandles(); |
|
78 | - |
|
79 | - $env = null; |
|
80 | - |
|
81 | - if ($this->env) { |
|
82 | - $env = $_SERVER ?? []; |
|
83 | - unset($env['argv'], $env['argc']); |
|
84 | - $env = \array_merge($env, $this->env); |
|
85 | - |
|
86 | - foreach ($env as $envKey => $envVar) { |
|
87 | - if (\is_array($envVar)) { |
|
88 | - unset($env[$envKey]); |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - $pipeSpec = [ |
|
94 | - 0 => $handles[0] ?? ['pipe', 'r'], |
|
95 | - 1 => $handles[1] ?? ['pipe', 'w'], |
|
96 | - 2 => $handles[2] ?? ['pipe', 'w'], |
|
97 | - ]; |
|
98 | - |
|
99 | - $process = \proc_open( |
|
100 | - $this->getCommand($settings, $this->tempFile), |
|
101 | - $pipeSpec, |
|
102 | - $pipes, |
|
103 | - null, |
|
104 | - $env |
|
105 | - ); |
|
106 | - |
|
107 | - if (!\is_resource($process)) { |
|
108 | - throw new Exception( |
|
109 | - 'Unable to spawn worker process' |
|
110 | - ); |
|
111 | - } |
|
112 | - |
|
113 | - if ($job) { |
|
114 | - $this->process($pipes[0], $job); |
|
115 | - } |
|
116 | - |
|
117 | - \fclose($pipes[0]); |
|
118 | - |
|
119 | - if ($this->timeout) { |
|
120 | - $stderr = $stdout = ''; |
|
121 | - |
|
122 | - unset($pipes[0]); |
|
123 | - |
|
124 | - while (true) { |
|
125 | - $r = $pipes; |
|
126 | - $w = null; |
|
127 | - $e = null; |
|
128 | - |
|
129 | - $n = @\stream_select($r, $w, $e, $this->timeout); |
|
130 | - |
|
131 | - if ($n === false) { |
|
132 | - break; |
|
133 | - } elseif ($n === 0) { |
|
134 | - \proc_terminate($process, 9); |
|
135 | - |
|
136 | - throw new Exception( |
|
137 | - \sprintf( |
|
138 | - 'Job execution aborted after %d seconds', |
|
139 | - $this->timeout |
|
140 | - ) |
|
141 | - ); |
|
142 | - } elseif ($n > 0) { |
|
143 | - foreach ($r as $pipe) { |
|
144 | - $pipeOffset = 0; |
|
145 | - |
|
146 | - foreach ($pipes as $i => $origPipe) { |
|
147 | - if ($pipe == $origPipe) { |
|
148 | - $pipeOffset = $i; |
|
149 | - |
|
150 | - break; |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - if (!$pipeOffset) { |
|
155 | - break; |
|
156 | - } |
|
157 | - |
|
158 | - $line = \fread($pipe, 8192); |
|
159 | - |
|
160 | - if (\strlen($line) == 0) { |
|
161 | - \fclose($pipes[$pipeOffset]); |
|
162 | - |
|
163 | - unset($pipes[$pipeOffset]); |
|
164 | - } else { |
|
165 | - if ($pipeOffset == 1) { |
|
166 | - $stdout .= $line; |
|
167 | - } else { |
|
168 | - $stderr .= $line; |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - if (empty($pipes)) { |
|
174 | - break; |
|
175 | - } |
|
176 | - } |
|
177 | - } |
|
178 | - } else { |
|
179 | - if (isset($pipes[1])) { |
|
180 | - $stdout = \stream_get_contents($pipes[1]); |
|
181 | - |
|
182 | - \fclose($pipes[1]); |
|
183 | - } |
|
184 | - |
|
185 | - if (isset($pipes[2])) { |
|
186 | - $stderr = \stream_get_contents($pipes[2]); |
|
187 | - |
|
188 | - \fclose($pipes[2]); |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - if (isset($handles[1])) { |
|
193 | - \rewind($handles[1]); |
|
194 | - |
|
195 | - $stdout = \stream_get_contents($handles[1]); |
|
196 | - |
|
197 | - \fclose($handles[1]); |
|
198 | - } |
|
199 | - |
|
200 | - if (isset($handles[2])) { |
|
201 | - \rewind($handles[2]); |
|
202 | - |
|
203 | - $stderr = \stream_get_contents($handles[2]); |
|
204 | - |
|
205 | - \fclose($handles[2]); |
|
206 | - } |
|
207 | - |
|
208 | - \proc_close($process); |
|
209 | - |
|
210 | - $this->cleanup(); |
|
211 | - |
|
212 | - return ['stdout' => $stdout, 'stderr' => $stderr]; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @param resource $pipe |
|
217 | - * @param string $job |
|
218 | - * |
|
219 | - * @throws Exception |
|
220 | - */ |
|
221 | - protected function process($pipe, $job) |
|
222 | - { |
|
223 | - \fwrite($pipe, $job); |
|
224 | - } |
|
225 | - |
|
226 | - protected function cleanup() |
|
227 | - { |
|
228 | - if ($this->tempFile) { |
|
229 | - \unlink($this->tempFile); |
|
230 | - } |
|
231 | - } |
|
19 | + /** |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $tempFile; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var bool |
|
26 | + */ |
|
27 | + protected $useTempFile = false; |
|
28 | + |
|
29 | + /** |
|
30 | + * Runs a single job (PHP code) using a separate PHP process. |
|
31 | + * |
|
32 | + * @param string $job |
|
33 | + * @param array $settings |
|
34 | + * |
|
35 | + * @return array<string, string> |
|
36 | + * |
|
37 | + * @throws Exception |
|
38 | + */ |
|
39 | + public function runJob($job, array $settings = []) |
|
40 | + { |
|
41 | + if ($this->useTempFile || $this->stdin) { |
|
42 | + if (!($this->tempFile = \tempnam(\sys_get_temp_dir(), 'PHPUnit')) || |
|
43 | + \file_put_contents($this->tempFile, $job) === false) { |
|
44 | + throw new Exception( |
|
45 | + 'Unable to write temporary file' |
|
46 | + ); |
|
47 | + } |
|
48 | + |
|
49 | + $job = $this->stdin; |
|
50 | + } |
|
51 | + |
|
52 | + return $this->runProcess($job, $settings); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Returns an array of file handles to be used in place of pipes |
|
57 | + * |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + protected function getHandles() |
|
61 | + { |
|
62 | + return []; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Handles creating the child process and returning the STDOUT and STDERR |
|
67 | + * |
|
68 | + * @param string $job |
|
69 | + * @param array $settings |
|
70 | + * |
|
71 | + * @return array<string, string> |
|
72 | + * |
|
73 | + * @throws Exception |
|
74 | + */ |
|
75 | + protected function runProcess($job, $settings) |
|
76 | + { |
|
77 | + $handles = $this->getHandles(); |
|
78 | + |
|
79 | + $env = null; |
|
80 | + |
|
81 | + if ($this->env) { |
|
82 | + $env = $_SERVER ?? []; |
|
83 | + unset($env['argv'], $env['argc']); |
|
84 | + $env = \array_merge($env, $this->env); |
|
85 | + |
|
86 | + foreach ($env as $envKey => $envVar) { |
|
87 | + if (\is_array($envVar)) { |
|
88 | + unset($env[$envKey]); |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + $pipeSpec = [ |
|
94 | + 0 => $handles[0] ?? ['pipe', 'r'], |
|
95 | + 1 => $handles[1] ?? ['pipe', 'w'], |
|
96 | + 2 => $handles[2] ?? ['pipe', 'w'], |
|
97 | + ]; |
|
98 | + |
|
99 | + $process = \proc_open( |
|
100 | + $this->getCommand($settings, $this->tempFile), |
|
101 | + $pipeSpec, |
|
102 | + $pipes, |
|
103 | + null, |
|
104 | + $env |
|
105 | + ); |
|
106 | + |
|
107 | + if (!\is_resource($process)) { |
|
108 | + throw new Exception( |
|
109 | + 'Unable to spawn worker process' |
|
110 | + ); |
|
111 | + } |
|
112 | + |
|
113 | + if ($job) { |
|
114 | + $this->process($pipes[0], $job); |
|
115 | + } |
|
116 | + |
|
117 | + \fclose($pipes[0]); |
|
118 | + |
|
119 | + if ($this->timeout) { |
|
120 | + $stderr = $stdout = ''; |
|
121 | + |
|
122 | + unset($pipes[0]); |
|
123 | + |
|
124 | + while (true) { |
|
125 | + $r = $pipes; |
|
126 | + $w = null; |
|
127 | + $e = null; |
|
128 | + |
|
129 | + $n = @\stream_select($r, $w, $e, $this->timeout); |
|
130 | + |
|
131 | + if ($n === false) { |
|
132 | + break; |
|
133 | + } elseif ($n === 0) { |
|
134 | + \proc_terminate($process, 9); |
|
135 | + |
|
136 | + throw new Exception( |
|
137 | + \sprintf( |
|
138 | + 'Job execution aborted after %d seconds', |
|
139 | + $this->timeout |
|
140 | + ) |
|
141 | + ); |
|
142 | + } elseif ($n > 0) { |
|
143 | + foreach ($r as $pipe) { |
|
144 | + $pipeOffset = 0; |
|
145 | + |
|
146 | + foreach ($pipes as $i => $origPipe) { |
|
147 | + if ($pipe == $origPipe) { |
|
148 | + $pipeOffset = $i; |
|
149 | + |
|
150 | + break; |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + if (!$pipeOffset) { |
|
155 | + break; |
|
156 | + } |
|
157 | + |
|
158 | + $line = \fread($pipe, 8192); |
|
159 | + |
|
160 | + if (\strlen($line) == 0) { |
|
161 | + \fclose($pipes[$pipeOffset]); |
|
162 | + |
|
163 | + unset($pipes[$pipeOffset]); |
|
164 | + } else { |
|
165 | + if ($pipeOffset == 1) { |
|
166 | + $stdout .= $line; |
|
167 | + } else { |
|
168 | + $stderr .= $line; |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + if (empty($pipes)) { |
|
174 | + break; |
|
175 | + } |
|
176 | + } |
|
177 | + } |
|
178 | + } else { |
|
179 | + if (isset($pipes[1])) { |
|
180 | + $stdout = \stream_get_contents($pipes[1]); |
|
181 | + |
|
182 | + \fclose($pipes[1]); |
|
183 | + } |
|
184 | + |
|
185 | + if (isset($pipes[2])) { |
|
186 | + $stderr = \stream_get_contents($pipes[2]); |
|
187 | + |
|
188 | + \fclose($pipes[2]); |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + if (isset($handles[1])) { |
|
193 | + \rewind($handles[1]); |
|
194 | + |
|
195 | + $stdout = \stream_get_contents($handles[1]); |
|
196 | + |
|
197 | + \fclose($handles[1]); |
|
198 | + } |
|
199 | + |
|
200 | + if (isset($handles[2])) { |
|
201 | + \rewind($handles[2]); |
|
202 | + |
|
203 | + $stderr = \stream_get_contents($handles[2]); |
|
204 | + |
|
205 | + \fclose($handles[2]); |
|
206 | + } |
|
207 | + |
|
208 | + \proc_close($process); |
|
209 | + |
|
210 | + $this->cleanup(); |
|
211 | + |
|
212 | + return ['stdout' => $stdout, 'stderr' => $stderr]; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @param resource $pipe |
|
217 | + * @param string $job |
|
218 | + * |
|
219 | + * @throws Exception |
|
220 | + */ |
|
221 | + protected function process($pipe, $job) |
|
222 | + { |
|
223 | + \fwrite($pipe, $job); |
|
224 | + } |
|
225 | + |
|
226 | + protected function cleanup() |
|
227 | + { |
|
228 | + if ($this->tempFile) { |
|
229 | + \unlink($this->tempFile); |
|
230 | + } |
|
231 | + } |
|
232 | 232 | } |
@@ -13,185 +13,185 @@ |
||
13 | 13 | |
14 | 14 | class GlobalState |
15 | 15 | { |
16 | - /** |
|
17 | - * @var string[] |
|
18 | - */ |
|
19 | - protected static $superGlobalArrays = [ |
|
20 | - '_ENV', |
|
21 | - '_POST', |
|
22 | - '_GET', |
|
23 | - '_COOKIE', |
|
24 | - '_SERVER', |
|
25 | - '_FILES', |
|
26 | - '_REQUEST' |
|
27 | - ]; |
|
28 | - |
|
29 | - /** |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - public static function getIncludedFilesAsString() |
|
33 | - { |
|
34 | - return static::processIncludedFilesAsString(\get_included_files()); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * @param array $files |
|
39 | - * |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public static function processIncludedFilesAsString(array $files) |
|
43 | - { |
|
44 | - $blacklist = new Blacklist; |
|
45 | - $prefix = false; |
|
46 | - $result = ''; |
|
47 | - |
|
48 | - if (\defined('__PHPUNIT_PHAR__')) { |
|
49 | - $prefix = 'phar://' . __PHPUNIT_PHAR__ . '/'; |
|
50 | - } |
|
51 | - |
|
52 | - for ($i = \count($files) - 1; $i > 0; $i--) { |
|
53 | - $file = $files[$i]; |
|
54 | - |
|
55 | - if ($prefix !== false && \strpos($file, $prefix) === 0) { |
|
56 | - continue; |
|
57 | - } |
|
58 | - |
|
59 | - // Skip virtual file system protocols |
|
60 | - if (\preg_match('/^(vfs|phpvfs[a-z0-9]+):/', $file)) { |
|
61 | - continue; |
|
62 | - } |
|
63 | - |
|
64 | - if (!$blacklist->isBlacklisted($file) && \is_file($file)) { |
|
65 | - $result = 'require_once \'' . $file . "';\n" . $result; |
|
66 | - } |
|
67 | - } |
|
68 | - |
|
69 | - return $result; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public static function getIniSettingsAsString() |
|
76 | - { |
|
77 | - $result = ''; |
|
78 | - $iniSettings = \ini_get_all(null, false); |
|
79 | - |
|
80 | - foreach ($iniSettings as $key => $value) { |
|
81 | - $result .= \sprintf( |
|
82 | - '@ini_set(%s, %s);' . "\n", |
|
83 | - self::exportVariable($key), |
|
84 | - self::exportVariable($value) |
|
85 | - ); |
|
86 | - } |
|
87 | - |
|
88 | - return $result; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public static function getConstantsAsString() |
|
95 | - { |
|
96 | - $constants = \get_defined_constants(true); |
|
97 | - $result = ''; |
|
98 | - |
|
99 | - if (isset($constants['user'])) { |
|
100 | - foreach ($constants['user'] as $name => $value) { |
|
101 | - $result .= \sprintf( |
|
102 | - 'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n", |
|
103 | - $name, |
|
104 | - $name, |
|
105 | - self::exportVariable($value) |
|
106 | - ); |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - return $result; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public static function getGlobalsAsString() |
|
117 | - { |
|
118 | - $result = ''; |
|
119 | - $superGlobalArrays = self::getSuperGlobalArrays(); |
|
120 | - |
|
121 | - foreach ($superGlobalArrays as $superGlobalArray) { |
|
122 | - if (isset($GLOBALS[$superGlobalArray]) && \is_array($GLOBALS[$superGlobalArray])) { |
|
123 | - foreach (\array_keys($GLOBALS[$superGlobalArray]) as $key) { |
|
124 | - if ($GLOBALS[$superGlobalArray][$key] instanceof Closure) { |
|
125 | - continue; |
|
126 | - } |
|
127 | - |
|
128 | - $result .= \sprintf( |
|
129 | - '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n", |
|
130 | - $superGlobalArray, |
|
131 | - $key, |
|
132 | - self::exportVariable($GLOBALS[$superGlobalArray][$key]) |
|
133 | - ); |
|
134 | - } |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - $blacklist = $superGlobalArrays; |
|
139 | - $blacklist[] = 'GLOBALS'; |
|
140 | - |
|
141 | - foreach (\array_keys($GLOBALS) as $key) { |
|
142 | - if (!\in_array($key, $blacklist) && !$GLOBALS[$key] instanceof Closure) { |
|
143 | - $result .= \sprintf( |
|
144 | - '$GLOBALS[\'%s\'] = %s;' . "\n", |
|
145 | - $key, |
|
146 | - self::exportVariable($GLOBALS[$key]) |
|
147 | - ); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - return $result; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @return string[] |
|
156 | - */ |
|
157 | - protected static function getSuperGlobalArrays() |
|
158 | - { |
|
159 | - return self::$superGlobalArrays; |
|
160 | - } |
|
161 | - |
|
162 | - protected static function exportVariable($variable) |
|
163 | - { |
|
164 | - if (\is_scalar($variable) || null === $variable || |
|
165 | - (\is_array($variable) && self::arrayOnlyContainsScalars($variable))) { |
|
166 | - return \var_export($variable, true); |
|
167 | - } |
|
168 | - |
|
169 | - return 'unserialize(' . |
|
170 | - \var_export(\serialize($variable), true) . |
|
171 | - ')'; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param array $array |
|
176 | - * |
|
177 | - * @return bool |
|
178 | - */ |
|
179 | - protected static function arrayOnlyContainsScalars(array $array) |
|
180 | - { |
|
181 | - $result = true; |
|
182 | - |
|
183 | - foreach ($array as $element) { |
|
184 | - if (\is_array($element)) { |
|
185 | - $result = self::arrayOnlyContainsScalars($element); |
|
186 | - } elseif (!\is_scalar($element) && null !== $element) { |
|
187 | - $result = false; |
|
188 | - } |
|
189 | - |
|
190 | - if ($result === false) { |
|
191 | - break; |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - return $result; |
|
196 | - } |
|
16 | + /** |
|
17 | + * @var string[] |
|
18 | + */ |
|
19 | + protected static $superGlobalArrays = [ |
|
20 | + '_ENV', |
|
21 | + '_POST', |
|
22 | + '_GET', |
|
23 | + '_COOKIE', |
|
24 | + '_SERVER', |
|
25 | + '_FILES', |
|
26 | + '_REQUEST' |
|
27 | + ]; |
|
28 | + |
|
29 | + /** |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + public static function getIncludedFilesAsString() |
|
33 | + { |
|
34 | + return static::processIncludedFilesAsString(\get_included_files()); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * @param array $files |
|
39 | + * |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public static function processIncludedFilesAsString(array $files) |
|
43 | + { |
|
44 | + $blacklist = new Blacklist; |
|
45 | + $prefix = false; |
|
46 | + $result = ''; |
|
47 | + |
|
48 | + if (\defined('__PHPUNIT_PHAR__')) { |
|
49 | + $prefix = 'phar://' . __PHPUNIT_PHAR__ . '/'; |
|
50 | + } |
|
51 | + |
|
52 | + for ($i = \count($files) - 1; $i > 0; $i--) { |
|
53 | + $file = $files[$i]; |
|
54 | + |
|
55 | + if ($prefix !== false && \strpos($file, $prefix) === 0) { |
|
56 | + continue; |
|
57 | + } |
|
58 | + |
|
59 | + // Skip virtual file system protocols |
|
60 | + if (\preg_match('/^(vfs|phpvfs[a-z0-9]+):/', $file)) { |
|
61 | + continue; |
|
62 | + } |
|
63 | + |
|
64 | + if (!$blacklist->isBlacklisted($file) && \is_file($file)) { |
|
65 | + $result = 'require_once \'' . $file . "';\n" . $result; |
|
66 | + } |
|
67 | + } |
|
68 | + |
|
69 | + return $result; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public static function getIniSettingsAsString() |
|
76 | + { |
|
77 | + $result = ''; |
|
78 | + $iniSettings = \ini_get_all(null, false); |
|
79 | + |
|
80 | + foreach ($iniSettings as $key => $value) { |
|
81 | + $result .= \sprintf( |
|
82 | + '@ini_set(%s, %s);' . "\n", |
|
83 | + self::exportVariable($key), |
|
84 | + self::exportVariable($value) |
|
85 | + ); |
|
86 | + } |
|
87 | + |
|
88 | + return $result; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public static function getConstantsAsString() |
|
95 | + { |
|
96 | + $constants = \get_defined_constants(true); |
|
97 | + $result = ''; |
|
98 | + |
|
99 | + if (isset($constants['user'])) { |
|
100 | + foreach ($constants['user'] as $name => $value) { |
|
101 | + $result .= \sprintf( |
|
102 | + 'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n", |
|
103 | + $name, |
|
104 | + $name, |
|
105 | + self::exportVariable($value) |
|
106 | + ); |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + return $result; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public static function getGlobalsAsString() |
|
117 | + { |
|
118 | + $result = ''; |
|
119 | + $superGlobalArrays = self::getSuperGlobalArrays(); |
|
120 | + |
|
121 | + foreach ($superGlobalArrays as $superGlobalArray) { |
|
122 | + if (isset($GLOBALS[$superGlobalArray]) && \is_array($GLOBALS[$superGlobalArray])) { |
|
123 | + foreach (\array_keys($GLOBALS[$superGlobalArray]) as $key) { |
|
124 | + if ($GLOBALS[$superGlobalArray][$key] instanceof Closure) { |
|
125 | + continue; |
|
126 | + } |
|
127 | + |
|
128 | + $result .= \sprintf( |
|
129 | + '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n", |
|
130 | + $superGlobalArray, |
|
131 | + $key, |
|
132 | + self::exportVariable($GLOBALS[$superGlobalArray][$key]) |
|
133 | + ); |
|
134 | + } |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + $blacklist = $superGlobalArrays; |
|
139 | + $blacklist[] = 'GLOBALS'; |
|
140 | + |
|
141 | + foreach (\array_keys($GLOBALS) as $key) { |
|
142 | + if (!\in_array($key, $blacklist) && !$GLOBALS[$key] instanceof Closure) { |
|
143 | + $result .= \sprintf( |
|
144 | + '$GLOBALS[\'%s\'] = %s;' . "\n", |
|
145 | + $key, |
|
146 | + self::exportVariable($GLOBALS[$key]) |
|
147 | + ); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + return $result; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @return string[] |
|
156 | + */ |
|
157 | + protected static function getSuperGlobalArrays() |
|
158 | + { |
|
159 | + return self::$superGlobalArrays; |
|
160 | + } |
|
161 | + |
|
162 | + protected static function exportVariable($variable) |
|
163 | + { |
|
164 | + if (\is_scalar($variable) || null === $variable || |
|
165 | + (\is_array($variable) && self::arrayOnlyContainsScalars($variable))) { |
|
166 | + return \var_export($variable, true); |
|
167 | + } |
|
168 | + |
|
169 | + return 'unserialize(' . |
|
170 | + \var_export(\serialize($variable), true) . |
|
171 | + ')'; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param array $array |
|
176 | + * |
|
177 | + * @return bool |
|
178 | + */ |
|
179 | + protected static function arrayOnlyContainsScalars(array $array) |
|
180 | + { |
|
181 | + $result = true; |
|
182 | + |
|
183 | + foreach ($array as $element) { |
|
184 | + if (\is_array($element)) { |
|
185 | + $result = self::arrayOnlyContainsScalars($element); |
|
186 | + } elseif (!\is_scalar($element) && null !== $element) { |
|
187 | + $result = false; |
|
188 | + } |
|
189 | + |
|
190 | + if ($result === false) { |
|
191 | + break; |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + return $result; |
|
196 | + } |
|
197 | 197 | } |
@@ -12,10 +12,10 @@ discard block |
||
12 | 12 | |
13 | 13 | class ConfigurationGenerator |
14 | 14 | { |
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - private $defaultTemplate = <<<EOT |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + private $defaultTemplate = <<<EOT |
|
19 | 19 | <?xml version="1.0" encoding="UTF-8"?> |
20 | 20 | <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
21 | 21 | xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/{phpunit_version}/phpunit.xsd" |
@@ -38,30 +38,30 @@ discard block |
||
38 | 38 | |
39 | 39 | EOT; |
40 | 40 | |
41 | - /** |
|
42 | - * @param string $phpunitVersion |
|
43 | - * @param string $bootstrapScript |
|
44 | - * @param string $testsDirectory |
|
45 | - * @param string $srcDirectory |
|
46 | - * |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function generateDefaultConfiguration($phpunitVersion, $bootstrapScript, $testsDirectory, $srcDirectory) |
|
50 | - { |
|
51 | - return \str_replace( |
|
52 | - [ |
|
53 | - '{phpunit_version}', |
|
54 | - '{bootstrap_script}', |
|
55 | - '{tests_directory}', |
|
56 | - '{src_directory}' |
|
57 | - ], |
|
58 | - [ |
|
59 | - $phpunitVersion, |
|
60 | - $bootstrapScript, |
|
61 | - $testsDirectory, |
|
62 | - $srcDirectory |
|
63 | - ], |
|
64 | - $this->defaultTemplate |
|
65 | - ); |
|
66 | - } |
|
41 | + /** |
|
42 | + * @param string $phpunitVersion |
|
43 | + * @param string $bootstrapScript |
|
44 | + * @param string $testsDirectory |
|
45 | + * @param string $srcDirectory |
|
46 | + * |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function generateDefaultConfiguration($phpunitVersion, $bootstrapScript, $testsDirectory, $srcDirectory) |
|
50 | + { |
|
51 | + return \str_replace( |
|
52 | + [ |
|
53 | + '{phpunit_version}', |
|
54 | + '{bootstrap_script}', |
|
55 | + '{tests_directory}', |
|
56 | + '{src_directory}' |
|
57 | + ], |
|
58 | + [ |
|
59 | + $phpunitVersion, |
|
60 | + $bootstrapScript, |
|
61 | + $testsDirectory, |
|
62 | + $srcDirectory |
|
63 | + ], |
|
64 | + $this->defaultTemplate |
|
65 | + ); |
|
66 | + } |
|
67 | 67 | } |