|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DF\PHPCoverFish\Base; |
|
4
|
|
|
|
|
5
|
|
|
use DF\PHPCoverFish\Common\ArrayCollection; |
|
6
|
|
|
use DF\PHPCoverFish\Common\CoverFishOutput; |
|
7
|
|
|
use DF\PHPCoverFish\Common\CoverFishPHPUnitFile; |
|
8
|
|
|
use DF\PHPCoverFish\Common\CoverFishPHPUnitTest; |
|
9
|
|
|
use DF\PHPCoverFish\Common\CoverFishResult; |
|
10
|
|
|
use DF\PHPCoverFish\Common\CoverFishHelper; |
|
11
|
|
|
use DF\PHPCoverFish\Validator\Base\BaseCoverFishValidatorInterface as CoverFishValidatorInterface; |
|
12
|
|
|
use SebastianBergmann\FinderFacade\FinderFacade; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class BaseScanner |
|
16
|
|
|
* |
|
17
|
|
|
* @package DF\PHPCoverFish |
|
18
|
|
|
* @author Patrick Paechnatz <[email protected]> |
|
19
|
|
|
* @copyright 2015 Patrick Paechnatz <[email protected]> |
|
20
|
|
|
* @license http://www.opensource.org/licenses/MIT |
|
21
|
|
|
* @link http://github.com/dunkelfrosch/phpcoverfish/tree |
|
22
|
|
|
* @since class available since Release 0.9.9 |
|
23
|
|
|
* @version 1.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class BaseScanner |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $phpUnitXMLPath; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $phpUnitXMLFile; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $phpUnitTestSuite; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $testSourcePath; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var String |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $testExcludePath; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var String |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $testAutoloadPath; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var bool |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $passes; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $usedClasses; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var CoverFishPHPUnitFile |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $phpUnitFile; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var CoverFishPHPUnitTest |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $phpUnitTest; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var CoverFishHelper |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $coverFishHelper; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var CoverFishResult |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $coverFishResult; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var CoverFishOutput |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $coverFishOutput; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @var ArrayCollection |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $validatorCollection; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @var bool |
|
99
|
|
|
*/ |
|
100
|
|
|
protected $stopOnError = false; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @var bool |
|
104
|
|
|
*/ |
|
105
|
|
|
protected $stopOnFailure = false; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected $filePattern = '*.*'; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var array |
|
114
|
|
|
*/ |
|
115
|
|
|
protected $filePatternExclude = array( |
|
116
|
|
|
'*.log', |
|
117
|
|
|
'*.txt', |
|
118
|
|
|
'*.md' |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return boolean |
|
123
|
|
|
*/ |
|
124
|
|
|
public function isStopOnError() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->stopOnError; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return boolean |
|
131
|
|
|
*/ |
|
132
|
|
|
public function isStopOnFailure() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->stopOnFailure; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getPhpUnitXMLPath() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->phpUnitXMLPath; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getPhpUnitXMLFile() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->phpUnitXMLFile; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getPhpUnitTestSuite() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->phpUnitTestSuite; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return ArrayCollection |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getValidatorCollection() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->validatorCollection; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @todo rename this variable to (get)TestSourcePathOrFile |
|
171
|
|
|
* |
|
172
|
|
|
* @codeCoverageIgnore |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getTestSourcePath() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->testSourcePath; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* return the excluded path, remove the last backslash before return |
|
183
|
|
|
* |
|
184
|
|
|
* @codeCoverageIgnore |
|
185
|
|
|
* |
|
186
|
|
|
* @return String |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getTestExcludePath() |
|
189
|
|
|
{ |
|
190
|
|
|
return rtrim($this->testExcludePath, '/'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @codeCoverageIgnore |
|
195
|
|
|
* |
|
196
|
|
|
* @return String |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getTestAutoloadPath() |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->testAutoloadPath; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @codeCoverageIgnore |
|
205
|
|
|
* |
|
206
|
|
|
* @return CoverFishPHPUnitFile |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getPhpUnitFile() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->phpUnitFile; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @codeCoverageIgnore |
|
215
|
|
|
* |
|
216
|
|
|
* @return CoverFishPHPUnitTest |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getPhpUnitTest() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->phpUnitTest; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @codeCoverageIgnore |
|
225
|
|
|
* |
|
226
|
|
|
* @return CoverFishHelper |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getCoverFishHelper() |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->coverFishHelper; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param CoverFishPHPUnitTest $phpUnitTest |
|
235
|
|
|
* |
|
236
|
|
|
* @codeCoverageIgnore |
|
237
|
|
|
*/ |
|
238
|
|
|
public function setPhpUnitTest($phpUnitTest) |
|
239
|
|
|
{ |
|
240
|
|
|
$this->phpUnitTest = $phpUnitTest; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @codeCoverageIgnore |
|
245
|
|
|
*/ |
|
246
|
|
|
public function __construct() |
|
247
|
|
|
{ |
|
248
|
|
|
$this->validatorCollection = new ArrayCollection(); |
|
249
|
|
|
$this->coverFishHelper = new CoverFishHelper(); |
|
250
|
|
|
$this->coverFishResult = new CoverFishResult(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* check existence of given autoload file (raw/psr-0/psr-4) |
|
255
|
|
|
* |
|
256
|
|
|
* @param string $autoloadFile |
|
257
|
|
|
* |
|
258
|
|
|
* @return bool |
|
259
|
|
|
* |
|
260
|
|
|
* @throws \Exception |
|
261
|
|
|
*/ |
|
262
|
|
|
public function checkSourceAutoload($autoloadFile) |
|
263
|
|
|
{ |
|
264
|
|
|
if (false === $this->coverFishHelper->checkFileExist($autoloadFile)) { |
|
265
|
|
|
throw new \Exception(sprintf('autoload file "%s" not found! please define your autoload.php file to use (e.g. ../app/autoload.php in symfony)', $autoloadFile)); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return true; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* get a testSuite main attribute of given phpunit xml file (like name, bootstrap ...) |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $attribute |
|
275
|
|
|
* @param \SimpleXMLElement $xmlDocument |
|
276
|
|
|
* |
|
277
|
|
|
* @return bool|string |
|
278
|
|
|
*/ |
|
279
|
|
|
public function getAttributeFromXML($attribute, \SimpleXMLElement $xmlDocument) |
|
280
|
|
|
{ |
|
281
|
|
|
/** @var \SimpleXMLElement $value */ |
|
282
|
|
|
foreach ($xmlDocument->attributes() as $key => $value) { |
|
283
|
|
|
/** @var \SimpleXMLElement $attribute */ |
|
284
|
|
|
if ($attribute === $key) { |
|
285
|
|
|
return (string) ($this->xmlToArray($value)[0]); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
return false; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* function will return the first testSuite directory found in testSuites node-block. |
|
294
|
|
|
* if no "$this->phpUnitTestSuite" provided, first testSuite will be returned! |
|
295
|
|
|
* |
|
296
|
|
|
* @param \SimpleXMLElement $xmlDocumentNodes |
|
297
|
|
|
* |
|
298
|
|
|
* @return bool|\SimpleXMLElement |
|
299
|
|
|
*/ |
|
300
|
|
|
public function getTestSuiteNodeFromXML(\SimpleXMLElement $xmlDocumentNodes) { |
|
301
|
|
|
|
|
302
|
|
|
/** @var \SimpleXMLElement $suite */ |
|
303
|
|
|
foreach ($xmlDocumentNodes->testsuites->testsuite as $suite) { |
|
304
|
|
|
|
|
305
|
|
|
if (false === $suiteName = $this->getAttributeFromXML('name', $suite)) { |
|
306
|
|
|
continue; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
if ((true === empty($this->phpUnitTestSuite)) || ($suiteName === $this->phpUnitTestSuite)) { |
|
310
|
|
|
return $suite; |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
return false; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* get a specific property from named testSuite node (like "exclude" or "directory") |
|
319
|
|
|
* |
|
320
|
|
|
* @param string $property |
|
321
|
|
|
* @param \SimpleXMLElement $xmlDocumentNodes |
|
322
|
|
|
* |
|
323
|
|
|
* @return bool|string |
|
324
|
|
|
*/ |
|
325
|
|
|
public function getTestSuitePropertyFromXML($property, \SimpleXMLElement $xmlDocumentNodes) |
|
326
|
|
|
{ |
|
327
|
|
|
/** @var \SimpleXMLElement $suite */ |
|
328
|
|
|
$suite = $this->getTestSuiteNodeFromXML($xmlDocumentNodes); |
|
329
|
|
|
if (false === empty($suite) && property_exists($suite, $property)) { |
|
330
|
|
|
return (string) ($this->xmlToArray($suite->$property)[0]); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
return false; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* @param \SimpleXMLElement|array $xmlObject |
|
338
|
|
|
* @param array $output |
|
339
|
|
|
* |
|
340
|
|
|
* @return array |
|
341
|
|
|
*/ |
|
342
|
|
|
public function xmlToArray($xmlObject, $output = array()) |
|
343
|
|
|
{ |
|
344
|
|
|
foreach ((array) $xmlObject as $index => $node) { |
|
345
|
|
|
$output[$index] = ($node instanceof \SimpleXMLElement || is_array($node)) |
|
346
|
|
|
? $this->xmlToArray($node) |
|
347
|
|
|
: $node; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
return $output; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @param CoverFishValidatorInterface $validator |
|
355
|
|
|
* |
|
356
|
|
|
* @return ArrayCollection |
|
357
|
|
|
*/ |
|
358
|
|
|
public function addValidator(CoverFishValidatorInterface $validator) |
|
359
|
|
|
{ |
|
360
|
|
|
$this->validatorCollection->add($validator); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* workaround for missing/buggy path exclude in symfony finder class |
|
365
|
|
|
* |
|
366
|
|
|
* @param array $files |
|
367
|
|
|
* @param string $excludePath |
|
368
|
|
|
* |
|
369
|
|
|
* @return array |
|
370
|
|
|
*/ |
|
371
|
|
|
public function removeExcludedPath(array $files, $excludePath) |
|
372
|
|
|
{ |
|
373
|
|
|
$result = $includedFiles = array(); |
|
374
|
|
|
|
|
375
|
|
|
if (true === empty($excludePath)) { |
|
376
|
|
|
return $files; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
foreach ($files as $filePath) { |
|
380
|
|
|
preg_match_all($this->coverFishHelper->getRegexPath($excludePath), $filePath, $result, PREG_SET_ORDER); |
|
381
|
|
|
if (true === empty($result)) { |
|
382
|
|
|
$includedFiles[] = $filePath; |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
return $includedFiles; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* scan all files by given path recursively, if one php file will be provided within given path, |
|
391
|
|
|
* this file will be returned in finder format |
|
392
|
|
|
* |
|
393
|
|
|
* @param string $sourcePath |
|
394
|
|
|
* |
|
395
|
|
|
* @return array |
|
396
|
|
|
*/ |
|
397
|
|
|
public function scanFilesInPath($sourcePath) |
|
398
|
|
|
{ |
|
399
|
|
|
$filePattern = $this->filePattern; |
|
400
|
|
|
if (strpos($sourcePath, str_replace('*', null, $filePattern))) { |
|
401
|
|
|
$filePattern = $this->coverFishHelper->getFileNameFromPath($sourcePath); |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
$facade = new FinderFacade( |
|
405
|
|
|
array($sourcePath), |
|
406
|
|
|
$this->filePatternExclude, |
|
407
|
|
|
array($filePattern) |
|
408
|
|
|
); |
|
409
|
|
|
|
|
410
|
|
|
return $this->removeExcludedPath($facade->findFiles(), $this->testExcludePath); |
|
411
|
|
|
} |
|
412
|
|
|
} |