1 | <?php |
||
7 | abstract class ExecutableTest |
||
8 | { |
||
9 | /** |
||
10 | * The path to the test to run |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $path; |
||
15 | |||
16 | /** |
||
17 | * A path to the temp file created |
||
18 | * for this test |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $temp; |
||
23 | protected $fullyQualifiedClassName; |
||
24 | protected $pipes = array(); |
||
25 | |||
26 | /** |
||
27 | * Path where the coveragereport is stored |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $coverageFileName; |
||
31 | |||
32 | /** |
||
33 | * @var Process |
||
34 | */ |
||
35 | protected $process; |
||
36 | |||
37 | /** |
||
38 | * A unique token value for a given |
||
39 | * process |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $token; |
||
44 | |||
45 | /** |
||
46 | * Last executed process command |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $lastCommand; |
||
51 | |||
52 | 67 | public function __construct($path, $fullyQualifiedClassName = null) |
|
57 | |||
58 | /** |
||
59 | * Get the expected count of tests to be executed |
||
60 | * |
||
61 | * @return int |
||
62 | */ |
||
63 | abstract public function getTestCount(); |
||
64 | |||
65 | /** |
||
66 | * Get the path to the test being executed |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | 7 | public function getPath() |
|
74 | |||
75 | /** |
||
76 | * Returns the path to this test's temp file. |
||
77 | * If the temp file does not exist, it will be |
||
78 | * created |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | 33 | public function getTempFile() |
|
83 | { |
||
84 | 33 | if (is_null($this->temp)) { |
|
85 | 6 | $this->temp = tempnam(sys_get_temp_dir(), "PT_"); |
|
86 | 6 | } |
|
87 | |||
88 | 33 | return $this->temp; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Return the test process' stderr contents |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getStderr() |
||
100 | |||
101 | /** |
||
102 | * Return any warnings that are in the test output, or false if there are none |
||
103 | * @return mixed |
||
104 | */ |
||
105 | 11 | public function getWarnings() |
|
106 | { |
||
107 | 11 | if (!$this->process) { |
|
108 | 9 | return false; |
|
109 | } |
||
110 | |||
111 | // PHPUnit has a bug where by it doesn't include warnings in the junit |
||
112 | // output, but still fails. This is a hacky, imperfect method for extracting them |
||
113 | // see https://github.com/sebastianbergmann/phpunit/issues/1317 |
||
114 | 2 | preg_match_all( |
|
115 | 2 | '/^\d+\) Warning\n(.+?)$/ms', |
|
116 | 2 | $this->process->getOutput(), |
|
117 | $matches |
||
118 | 2 | ); |
|
119 | |||
120 | 2 | return isset($matches[1]) ? $matches[1] : false; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Stop the process and return it's |
||
125 | * exit code |
||
126 | * |
||
127 | * @return int |
||
128 | */ |
||
129 | 2 | public function stop() |
|
133 | |||
134 | /** |
||
135 | * Removes the test file |
||
136 | */ |
||
137 | public function deleteFile() |
||
142 | |||
143 | /** |
||
144 | * Check if the process has terminated. |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | 2 | public function isDoneRunning() |
|
152 | |||
153 | /** |
||
154 | * Return the exit code of the process |
||
155 | * |
||
156 | * @return int |
||
157 | */ |
||
158 | 2 | public function getExitCode() |
|
162 | |||
163 | /** |
||
164 | * Return the last process command |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 1 | public function getLastCommand() |
|
169 | { |
||
170 | 1 | return $this->lastCommand; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * Executes the test by creating a separate process |
||
175 | * |
||
176 | * @param $binary |
||
177 | * @param array $options |
||
178 | * @param array $environmentVariables |
||
179 | * @return $this |
||
180 | */ |
||
181 | 2 | public function run($binary, $options = array(), $environmentVariables = array()) |
|
182 | { |
||
183 | 2 | $environmentVariables['PARATEST'] = 1; |
|
184 | 2 | $this->handleEnvironmentVariables($environmentVariables); |
|
185 | 2 | $command = $this->command($binary, $options); |
|
186 | 2 | $this->assertValidCommandLineLength($command); |
|
187 | 2 | $this->lastCommand = $command; |
|
188 | 2 | $this->process = new Process($command, null, $environmentVariables); |
|
189 | 2 | $this->process->start(); |
|
190 | 2 | return $this; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Returns the unique token for this test process |
||
195 | * |
||
196 | * @return int |
||
197 | */ |
||
198 | 1 | public function getToken() |
|
202 | |||
203 | /** |
||
204 | * Generate command line with passed options suitable to handle through paratest. |
||
205 | * |
||
206 | * @param string $binary Executable binary name. |
||
207 | * @param array $options Command line options. |
||
208 | * @return string Command line. |
||
209 | */ |
||
210 | 3 | public function command($binary, $options = array()) |
|
216 | |||
217 | /** |
||
218 | * Get covertage filename |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | 3 | public function getCoverageFileName() |
|
230 | |||
231 | /** |
||
232 | * Get process stdout content |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getStdout() |
||
240 | |||
241 | /** |
||
242 | * Set process termporary filename |
||
243 | * |
||
244 | * @param string $temp |
||
245 | */ |
||
246 | 38 | public function setTempFile($temp) |
|
250 | |||
251 | /** |
||
252 | * Assert that command line lenght is valid. |
||
253 | * |
||
254 | * In some situations process command line can became too long when combining different test |
||
255 | * cases in single --filter arguments so it's better to show error regarding that to user |
||
256 | * and propose him to decrease max batch size. |
||
257 | * |
||
258 | * @param string $cmd Command line |
||
259 | * @throws \RuntimeException on too long command line |
||
260 | */ |
||
261 | 2 | protected function assertValidCommandLineLength($cmd) |
|
276 | |||
277 | /** |
||
278 | * A template method that can be overridden to add necessary options for a test |
||
279 | * |
||
280 | * @param array $options the options that are passed to the run method |
||
281 | * @return array $options the prepared options |
||
282 | */ |
||
283 | 3 | protected function prepareOptions($options) |
|
287 | |||
288 | /** |
||
289 | * Returns the command string that will be executed |
||
290 | * by proc_open |
||
291 | * |
||
292 | * @param $binary |
||
293 | * @param array $options |
||
294 | * @return mixed |
||
295 | */ |
||
296 | 6 | protected function getCommandString($binary, $options = array()) |
|
314 | |||
315 | /** |
||
316 | * Checks environment variables for the presence of a TEST_TOKEN |
||
317 | * variable and sets $this->token based on its value |
||
318 | * |
||
319 | * @param $environmentVariables |
||
320 | */ |
||
321 | 3 | protected function handleEnvironmentVariables($environmentVariables) |
|
327 | |||
328 | /** |
||
329 | * Checks if the coverage-php option is set and redirects it to a unique temp file. |
||
330 | * This will ensure, that multiple tests write to separate coverage-files. |
||
331 | * |
||
332 | * @param array $options |
||
333 | * @return array $options |
||
334 | */ |
||
335 | 3 | protected function redirectCoverageOption($options) |
|
346 | } |
||
347 |
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.