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