1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ParaTest\Runners\PHPUnit; |
6
|
|
|
|
7
|
|
|
use ParaTest\Parser\NoClassInFileException; |
8
|
|
|
use ParaTest\Parser\ParsedClass; |
9
|
|
|
use ParaTest\Parser\ParsedFunction; |
10
|
|
|
use ParaTest\Parser\ParsedObject; |
11
|
|
|
use ParaTest\Parser\Parser; |
12
|
|
|
|
13
|
|
|
class SuiteLoader |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The collection of loaded files. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $files = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The collection of parsed test classes. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $loadedSuites = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Options |
31
|
|
|
*/ |
32
|
|
|
public $options; |
33
|
|
|
|
34
|
25 |
|
public function __construct(Options $options = null) |
35
|
|
|
{ |
36
|
25 |
|
$this->options = $options; |
37
|
25 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns all parsed suite objects as ExecutableTest |
41
|
|
|
* instances. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
2 |
|
public function getSuites(): array |
46
|
|
|
{ |
47
|
2 |
|
return $this->loadedSuites; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns a collection of TestMethod objects |
52
|
|
|
* for all loaded ExecutableTest instances. |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
3 |
|
public function getTestMethods(): array |
57
|
|
|
{ |
58
|
3 |
|
$methods = []; |
59
|
3 |
|
foreach ($this->loadedSuites as $suite) { |
60
|
2 |
|
$methods = array_merge($methods, $suite->getFunctions()); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
return $methods; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Populates the loaded suite collection. Will load suites |
68
|
|
|
* based off a phpunit xml configuration or a specified path. |
69
|
|
|
* |
70
|
|
|
* @param string $path |
71
|
|
|
* |
72
|
|
|
* @throws \RuntimeException |
73
|
|
|
*/ |
74
|
23 |
|
public function load(string $path = '') |
75
|
|
|
{ |
76
|
23 |
|
if (is_object($this->options) && isset($this->options->filtered['configuration'])) { |
|
|
|
|
77
|
16 |
|
$configuration = $this->options->filtered['configuration']; |
|
|
|
|
78
|
|
|
} else { |
79
|
7 |
|
$configuration = new Configuration(''); |
80
|
|
|
} |
81
|
|
|
|
82
|
23 |
|
if ($path) { |
83
|
9 |
|
$testFileLoader = new TestFileLoader($this->options); |
84
|
9 |
|
$this->files = array_merge($this->files, $testFileLoader->loadPath($path)); |
85
|
14 |
|
} elseif (isset($this->options->testsuite) && $this->options->testsuite) { |
|
|
|
|
86
|
10 |
|
foreach ($configuration->getSuiteByName($this->options->testsuite) as $suite) { |
|
|
|
|
87
|
10 |
|
foreach ($suite as $suitePath) { |
88
|
10 |
|
$testFileLoader = new TestFileLoader($this->options); |
89
|
10 |
|
$this->files = array_merge($this->files, $testFileLoader->loadSuitePath($suitePath)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
4 |
|
} elseif ($suites = $configuration->getSuites()) { |
93
|
2 |
|
foreach ($suites as $suite) { |
94
|
2 |
|
foreach ($suite as $suitePath) { |
95
|
2 |
|
$testFileLoader = new TestFileLoader($this->options); |
96
|
2 |
|
$this->files = array_merge($this->files, $testFileLoader->loadSuitePath($suitePath)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
21 |
|
if (!$this->files) { |
|
|
|
|
102
|
1 |
|
throw new \RuntimeException('No path or configuration provided (tests must end with Test.php)'); |
103
|
|
|
} |
104
|
|
|
|
105
|
20 |
|
$this->files = array_unique($this->files); // remove duplicates |
106
|
|
|
|
107
|
20 |
|
$this->initSuites(); |
108
|
20 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Called after all files are loaded. Parses loaded files into |
112
|
|
|
* ExecutableTest objects - either Suite or TestMethod. |
113
|
|
|
*/ |
114
|
20 |
|
private function initSuites() |
115
|
|
|
{ |
116
|
20 |
|
foreach ($this->files as $path) { |
117
|
|
|
try { |
118
|
20 |
|
$parser = new Parser($path); |
119
|
19 |
|
if ($class = $parser->getClass()) { |
|
|
|
|
120
|
19 |
|
$this->loadedSuites[$path] = $this->createSuite($path, $class); |
121
|
|
|
} |
122
|
1 |
|
} catch (NoClassInFileException $e) { |
123
|
20 |
|
continue; |
124
|
|
|
} |
125
|
|
|
} |
126
|
20 |
|
} |
127
|
|
|
|
128
|
19 |
|
private function executableTests(string $path, ParsedClass $class) |
129
|
|
|
{ |
130
|
19 |
|
$executableTests = []; |
131
|
19 |
|
$methodBatches = $this->getMethodBatches($class); |
132
|
19 |
|
foreach ($methodBatches as $methodBatch) { |
133
|
19 |
|
$executableTest = new TestMethod($path, $methodBatch); |
134
|
19 |
|
$executableTests[] = $executableTest; |
135
|
|
|
} |
136
|
|
|
|
137
|
19 |
|
return $executableTests; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get method batches. |
142
|
|
|
* |
143
|
|
|
* Identify method dependencies, and group dependents and dependees on a single methodBatch. |
144
|
|
|
* Use max batch size to fill batches. |
145
|
|
|
* |
146
|
|
|
* @param ParsedClass $class |
147
|
|
|
* |
148
|
|
|
* @return array of MethodBatches. Each MethodBatch has an array of method names |
149
|
|
|
*/ |
150
|
19 |
|
private function getMethodBatches(ParsedClass $class): array |
151
|
|
|
{ |
152
|
19 |
|
$classMethods = $class->getMethods($this->options ? $this->options->annotations : []); |
|
|
|
|
153
|
19 |
|
$maxBatchSize = $this->options && $this->options->functional ? $this->options->maxBatchSize : 0; |
|
|
|
|
154
|
19 |
|
$batches = []; |
155
|
19 |
|
foreach ($classMethods as $method) { |
156
|
19 |
|
$tests = $this->getMethodTests($class, $method, $maxBatchSize !== 0); |
157
|
|
|
// if filter passed to paratest then method tests can be blank if not match to filter |
158
|
19 |
|
if (!$tests) { |
|
|
|
|
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
|
162
|
19 |
|
if (($dependsOn = $this->methodDependency($method)) !== null) { |
163
|
1 |
|
$this->addDependentTestsToBatchSet($batches, $dependsOn, $tests); |
164
|
|
|
} else { |
165
|
19 |
|
$this->addTestsToBatchSet($batches, $tests, $maxBatchSize); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
19 |
|
return $batches; |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
private function addDependentTestsToBatchSet(array &$batches, string $dependsOn, array $tests) |
173
|
|
|
{ |
174
|
1 |
|
foreach ($batches as $key => $batch) { |
175
|
1 |
|
foreach ($batch as $methodName) { |
176
|
1 |
|
if ($dependsOn === $methodName) { |
177
|
1 |
|
$batches[$key] = array_merge($batches[$key], $tests); |
178
|
1 |
|
continue; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
1 |
|
} |
183
|
|
|
|
184
|
19 |
|
private function addTestsToBatchSet(array &$batches, array $tests, int $maxBatchSize) |
185
|
|
|
{ |
186
|
19 |
|
foreach ($tests as $test) { |
187
|
19 |
|
$lastIndex = count($batches) - 1; |
188
|
19 |
|
if ($lastIndex !== -1 |
189
|
19 |
|
&& count($batches[$lastIndex]) < $maxBatchSize |
190
|
|
|
) { |
191
|
|
|
$batches[$lastIndex][] = $test; |
192
|
|
|
} else { |
193
|
19 |
|
$batches[] = [$test]; |
194
|
|
|
} |
195
|
|
|
} |
196
|
19 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get method all available tests. |
200
|
|
|
* |
201
|
|
|
* With empty filter this method returns single test if doesnt' have data provider or |
202
|
|
|
* data provider is not used and return all test if has data provider and data provider is used. |
203
|
|
|
* |
204
|
|
|
* @param ParsedClass $class parsed class |
205
|
|
|
* @param ParsedObject $method parsed method |
206
|
|
|
* @param bool $useDataProvider try to use data provider or not |
207
|
|
|
* |
208
|
|
|
* @return string[] array of test names |
209
|
|
|
*/ |
210
|
19 |
|
private function getMethodTests(ParsedClass $class, ParsedFunction $method, bool $useDataProvider = false): array |
211
|
|
|
{ |
212
|
19 |
|
$result = []; |
213
|
|
|
|
214
|
19 |
|
$groups = $this->methodGroups($method); |
215
|
|
|
|
216
|
19 |
|
$dataProvider = $this->methodDataProvider($method); |
217
|
19 |
|
if ($useDataProvider && isset($dataProvider)) { |
218
|
|
|
$testFullClassName = '\\' . $class->getName(); |
219
|
|
|
$testClass = new $testFullClassName(); |
220
|
|
|
$result = []; |
221
|
|
|
$datasetKeys = array_keys($testClass->$dataProvider()); |
222
|
|
|
foreach ($datasetKeys as $key) { |
223
|
|
|
$test = sprintf( |
224
|
|
|
'%s with data set %s', |
225
|
|
|
$method->getName(), |
226
|
|
|
is_int($key) ? '#' . $key : '"' . $key . '"' |
227
|
|
|
); |
228
|
|
|
if ($this->testMatchOptions($class->getName(), $test, $groups)) { |
229
|
|
|
$result[] = $test; |
230
|
|
|
} |
231
|
|
|
} |
232
|
19 |
|
} elseif ($this->testMatchOptions($class->getName(), $method->getName(), $groups)) { |
233
|
19 |
|
$result = [$method->getName()]; |
234
|
|
|
} |
235
|
|
|
|
236
|
19 |
|
return $result; |
237
|
|
|
} |
238
|
|
|
|
239
|
19 |
|
private function testMatchGroupOptions(array $groups): bool |
240
|
|
|
{ |
241
|
19 |
|
if (empty($groups)) { |
242
|
17 |
|
return true; |
243
|
|
|
} |
244
|
|
|
|
245
|
15 |
|
if (!empty($this->options->groups) |
|
|
|
|
246
|
15 |
|
&& !array_intersect($groups, $this->options->groups) |
|
|
|
|
247
|
|
|
) { |
248
|
|
|
return false; |
249
|
|
|
} |
250
|
|
|
|
251
|
15 |
|
if (!empty($this->options->excludeGroups) |
|
|
|
|
252
|
15 |
|
&& array_intersect($groups, $this->options->excludeGroups) |
|
|
|
|
253
|
|
|
) { |
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
|
257
|
15 |
|
return true; |
258
|
|
|
} |
259
|
|
|
|
260
|
19 |
|
private function testMatchFilterOptions(string $className, string $name): bool |
261
|
|
|
{ |
262
|
19 |
|
if (empty($this->options->filter)) { |
|
|
|
|
263
|
19 |
|
return true; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$re = substr($this->options->filter, 0, 1) === '/' |
|
|
|
|
267
|
|
|
? $this->options->filter |
|
|
|
|
268
|
|
|
: '/' . $this->options->filter . '/'; |
|
|
|
|
269
|
|
|
$fullName = $className . '::' . $name; |
270
|
|
|
|
271
|
|
|
return 1 === preg_match($re, $fullName); |
272
|
|
|
} |
273
|
|
|
|
274
|
19 |
|
private function testMatchOptions(string $className, string $name, array $group): bool |
275
|
|
|
{ |
276
|
19 |
|
$result = $this->testMatchGroupOptions($group) |
277
|
19 |
|
&& $this->testMatchFilterOptions($className, $name); |
278
|
|
|
|
279
|
19 |
|
return $result; |
280
|
|
|
} |
281
|
|
|
|
282
|
19 |
|
private function methodDataProvider(ParsedFunction $method) |
283
|
|
|
{ |
284
|
19 |
|
if (preg_match("/@\bdataProvider\b \b(.*)\b/", $method->getDocBlock(), $matches)) { |
285
|
|
|
return $matches[1]; |
286
|
|
|
} |
287
|
19 |
|
} |
288
|
|
|
|
289
|
19 |
|
private function methodDependency(ParsedFunction $method) |
290
|
|
|
{ |
291
|
19 |
|
if (preg_match("/@\bdepends\b \b(.*)\b/", $method->getDocBlock(), $matches)) { |
292
|
1 |
|
return $matches[1]; |
293
|
|
|
} |
294
|
19 |
|
} |
295
|
|
|
|
296
|
19 |
|
private function methodGroups(ParsedFunction $method) |
297
|
|
|
{ |
298
|
19 |
|
if (preg_match_all("/@\bgroup\b \b(.*)\b/", $method->getDocBlock(), $matches)) { |
299
|
15 |
|
return $matches[1]; |
300
|
|
|
} |
301
|
|
|
|
302
|
17 |
|
return []; |
303
|
|
|
} |
304
|
|
|
|
305
|
19 |
|
private function createSuite(string $path, ParsedClass $class): Suite |
306
|
|
|
{ |
307
|
19 |
|
return new Suite( |
308
|
19 |
|
$path, |
309
|
19 |
|
$this->executableTests( |
310
|
19 |
|
$path, |
311
|
19 |
|
$class |
312
|
|
|
), |
313
|
19 |
|
$class->getName() |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.