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