1 | <?php |
||
10 | abstract class ExecutableTest |
||
11 | { |
||
12 | /** |
||
13 | * The path to the test to run. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $path; |
||
18 | |||
19 | /** |
||
20 | * A path to the temp file created |
||
21 | * for this test. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $temp; |
||
26 | protected $fullyQualifiedClassName; |
||
27 | protected $pipes = []; |
||
28 | |||
29 | /** |
||
30 | * Path where the coveragereport is stored. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $coverageFileName; |
||
35 | |||
36 | /** |
||
37 | * @var Process |
||
38 | */ |
||
39 | protected $process; |
||
40 | |||
41 | /** |
||
42 | * A unique token value for a given |
||
43 | * process. |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $token; |
||
48 | |||
49 | /** |
||
50 | * Last executed process command. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $lastCommand; |
||
55 | |||
56 | 66 | public function __construct(string $path, string $fullyQualifiedClassName = null) |
|
61 | |||
62 | /** |
||
63 | * Get the expected count of tests to be executed. |
||
64 | * |
||
65 | * @return int |
||
66 | */ |
||
67 | abstract public function getTestCount(): int; |
||
68 | |||
69 | /** |
||
70 | * Get the path to the test being executed. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 6 | public function getPath(): string |
|
78 | |||
79 | /** |
||
80 | * Returns the path to this test's temp file. |
||
81 | * If the temp file does not exist, it will be |
||
82 | * created. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 32 | public function getTempFile(): string |
|
94 | |||
95 | /** |
||
96 | * Return the test process' stderr contents. |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getStderr(): string |
||
104 | |||
105 | /** |
||
106 | * Return any warnings that are in the test output, or false if there are none. |
||
107 | * |
||
108 | * @return mixed |
||
109 | */ |
||
110 | 11 | public function getWarnings() |
|
127 | |||
128 | /** |
||
129 | * Stop the process and return it's |
||
130 | * exit code. |
||
131 | * |
||
132 | * @return int |
||
133 | */ |
||
134 | 2 | public function stop(): int |
|
138 | |||
139 | /** |
||
140 | * Removes the test file. |
||
141 | */ |
||
142 | public function deleteFile() |
||
147 | |||
148 | /** |
||
149 | * Check if the process has terminated. |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 2 | public function isDoneRunning(): bool |
|
157 | |||
158 | /** |
||
159 | * Return the exit code of the process. |
||
160 | * |
||
161 | * @return int |
||
162 | */ |
||
163 | 2 | public function getExitCode(): int |
|
167 | |||
168 | /** |
||
169 | * Return the last process command. |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getLastCommand(): string |
||
177 | |||
178 | /** |
||
179 | * Executes the test by creating a separate process. |
||
180 | * |
||
181 | * @param $binary |
||
182 | * @param array $options |
||
183 | * @param array $environmentVariables |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | 2 | public function run(string $binary, array $options = [], array $environmentVariables = []) |
|
203 | |||
204 | /** |
||
205 | * Returns the unique token for this test process. |
||
206 | * |
||
207 | * @return int |
||
208 | */ |
||
209 | 1 | public function getToken(): int |
|
213 | |||
214 | /** |
||
215 | * Generate command line with passed options suitable to handle through paratest. |
||
216 | * |
||
217 | * @param string $binary executable binary name |
||
218 | * @param array $options command line options |
||
219 | * |
||
220 | * @return string command line |
||
221 | */ |
||
222 | 3 | public function command(string $binary, array $options = []): string |
|
229 | |||
230 | /** |
||
231 | * Get covertage filename. |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 3 | public function getCoverageFileName(): string |
|
243 | |||
244 | /** |
||
245 | * Get process stdout content. |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | public function getStdout(): string |
||
253 | |||
254 | /** |
||
255 | * Set process termporary filename. |
||
256 | * |
||
257 | * @param string $temp |
||
258 | */ |
||
259 | 37 | public function setTempFile(string $temp) |
|
263 | |||
264 | /** |
||
265 | * Assert that command line lenght is valid. |
||
266 | * |
||
267 | * In some situations process command line can became too long when combining different test |
||
268 | * cases in single --filter arguments so it's better to show error regarding that to user |
||
269 | * and propose him to decrease max batch size. |
||
270 | * |
||
271 | * @param string $cmd Command line |
||
272 | * |
||
273 | * @throws \RuntimeException on too long command line |
||
274 | */ |
||
275 | 2 | protected function assertValidCommandLineLength(string $cmd) |
|
291 | |||
292 | /** |
||
293 | * A template method that can be overridden to add necessary options for a test. |
||
294 | * |
||
295 | * @param array $options the options that are passed to the run method |
||
296 | * |
||
297 | * @return array $options the prepared options |
||
298 | */ |
||
299 | 3 | protected function prepareOptions(array $options): array |
|
303 | |||
304 | /** |
||
305 | * Returns the command string that will be executed |
||
306 | * by proc_open. |
||
307 | * |
||
308 | * @param string $binary |
||
309 | * @param array $options |
||
310 | * |
||
311 | * @return mixed |
||
312 | */ |
||
313 | 6 | protected function getCommandString(string $binary, array $options = []) |
|
329 | |||
330 | /** |
||
331 | * Checks environment variables for the presence of a TEST_TOKEN |
||
332 | * variable and sets $this->token based on its value. |
||
333 | * |
||
334 | * @param $environmentVariables |
||
335 | */ |
||
336 | 3 | protected function handleEnvironmentVariables(array $environmentVariables) |
|
342 | |||
343 | /** |
||
344 | * Checks if the coverage-php option is set and redirects it to a unique temp file. |
||
345 | * This will ensure, that multiple tests write to separate coverage-files. |
||
346 | * |
||
347 | * @param array $options |
||
348 | * |
||
349 | * @return array $options |
||
350 | */ |
||
351 | 3 | protected function redirectCoverageOption(array $options): array |
|
361 | } |
||
362 |