|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DF\PHPCoverFish\Base; |
|
4
|
|
|
|
|
5
|
|
|
use DF\PHPCoverFish\Common\CoverFishPHPUnitFile; |
|
6
|
|
|
use DF\PHPCoverFish\Common\CoverFishPHPUnitTest; |
|
7
|
|
|
use DF\PHPCoverFish\Validator\Base\BaseCoverFishValidatorInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class BaseCoverFishScanner |
|
11
|
|
|
* |
|
12
|
|
|
* @package DF\PHPCoverFish |
|
13
|
|
|
* @author Patrick Paechnatz <[email protected]> |
|
14
|
|
|
* @copyright 2015 Patrick Paechnatz <[email protected]> |
|
15
|
|
|
* @license http://www.opensource.org/licenses/MIT |
|
16
|
|
|
* @link http://github.com/dunkelfrosch/phpcoverfish/tree |
|
17
|
|
|
* @since class available since Release 0.9.9 |
|
18
|
|
|
* @version 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class BaseCoverFishScanner extends BaseScanner |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $filePattern = '*.php'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $filePatternExclude = array( |
|
31
|
|
|
'*.log', |
|
32
|
|
|
'*.js', |
|
33
|
|
|
'*.html', |
|
34
|
|
|
'*.twig', |
|
35
|
|
|
'*.css', |
|
36
|
|
|
'*.scss', |
|
37
|
|
|
'*.less', |
|
38
|
|
|
'*.txt', |
|
39
|
|
|
'*.md', |
|
40
|
|
|
'*.yml', |
|
41
|
|
|
'*.xml' |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param array $cliOptions |
|
46
|
|
|
* |
|
47
|
|
|
* @codeCoverageIgnore |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(array $cliOptions) |
|
50
|
|
|
{ |
|
51
|
|
|
parent::__construct(); |
|
52
|
|
|
$this->initCoverFishScanner($cliOptions); |
|
53
|
|
|
|
|
54
|
|
|
if (true === $this->coverFishHelper->checkFileExist($this->phpUnitXMLFile)) { |
|
55
|
|
|
$this->setConfigFromPHPUnitConfigFile(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (true === $this->checkSourceAutoload($this->testAutoloadPath)) { |
|
59
|
|
|
// still looking for a valid workaround %) |
|
60
|
|
|
include(sprintf('%s', $this->testAutoloadPath)); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array $cliOptions |
|
66
|
|
|
* |
|
67
|
|
|
* @codeCoverageIgnore |
|
68
|
|
|
*/ |
|
69
|
|
|
private function initCoverFishScanner(array $cliOptions) |
|
70
|
|
|
{ |
|
71
|
|
|
// fetch all necessary coverfish parameter by optional given raw-data first |
|
72
|
|
|
$this->testAutoloadPath = $cliOptions['raw_scan_autoload_file']; |
|
73
|
|
|
$this->testSourcePath = $cliOptions['raw_scan_source']; |
|
74
|
|
|
$this->testExcludePath = $cliOptions['raw_scan_exclude_path']; |
|
75
|
|
|
|
|
76
|
|
|
// fetch additional system/app parameter |
|
77
|
|
|
$this->phpUnitXMLFile = $cliOptions['sys_phpunit_config']; |
|
78
|
|
|
$this->phpUnitTestSuite = $cliOptions['sys_phpunit_config_test_suite']; |
|
79
|
|
|
$this->stopOnFailure = (bool) $cliOptions['sys_stop_on_failure']; |
|
80
|
|
|
$this->stopOnError = (bool) $cliOptions['sys_stop_on_error']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* update/set configuration using phpunit xml file |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setConfigFromPHPUnitConfigFile() |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
/** @var \SimpleXMLElement $xmlDocument */ |
|
90
|
|
|
$xmlDocument = simplexml_load_file($this->phpUnitXMLFile); |
|
91
|
|
|
|
|
92
|
|
|
$this->phpUnitXMLPath = $this->coverFishHelper->getPathFromFileNameAndPath($this->phpUnitXMLFile); |
|
93
|
|
|
$this->testAutoloadPath = sprintf('%s%s', $this->phpUnitXMLPath, $this->getAttributeFromXML('bootstrap', $xmlDocument)); |
|
94
|
|
|
$this->testSourcePath = sprintf('%s%s', $this->phpUnitXMLPath, $this->getTestSuitePropertyFromXML('directory', $xmlDocument)); |
|
95
|
|
|
$this->testExcludePath = sprintf('%s', $this->getTestSuitePropertyFromXML('exclude', $xmlDocument)); |
|
96
|
|
|
|
|
97
|
|
|
} catch (\Exception $e) { |
|
98
|
|
|
|
|
99
|
|
|
echo (sprintf('parse error loading phpunit config file "%s"!', $this->phpUnitXMLFile)); |
|
100
|
|
|
echo (sprintf('-> message: %s', $e->getMessage())); |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param CoverFishPHPUnitTest $phpUnitTest |
|
107
|
|
|
* |
|
108
|
|
|
* @return CoverFishPHPUnitTest |
|
109
|
|
|
*/ |
|
110
|
|
|
public function validateAndReturnMapping(CoverFishPHPUnitTest $phpUnitTest) |
|
111
|
|
|
{ |
|
112
|
|
|
$phpUnitTest->clearCoverMappings(); |
|
113
|
|
|
|
|
114
|
|
|
/** @var BaseCoverFishValidatorInterface $validator */ |
|
115
|
|
|
foreach ($this->validatorCollection as $validator) { |
|
116
|
|
|
if (false === $validator->validate()) { |
|
117
|
|
|
continue; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$phpUnitTest->addCoverMapping($validator->getMapping($this->phpUnitFile)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $phpUnitTest; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $className |
|
128
|
|
|
* @param array $classData |
|
129
|
|
|
* |
|
130
|
|
|
* @return CoverFishPHPUnitFile |
|
131
|
|
|
*/ |
|
132
|
|
|
public function setPHPUnitTestMetaData($className, $classData) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->phpUnitFile->setClassName($className); |
|
135
|
|
|
$this->phpUnitFile |
|
136
|
|
|
->setFile($this->coverFishHelper |
|
137
|
|
|
->getAttributeByKey('file', $classData) |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
$this->phpUnitFile |
|
141
|
|
|
->setClassNameSpace($this->coverFishHelper |
|
142
|
|
|
->getAttributeByKey('namespace', $classData['package']) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$usedClassesInFile = $this->coverFishHelper->getUsedClassesInClass($this->phpUnitFile->getFile()); |
|
146
|
|
|
|
|
147
|
|
|
if (is_array($usedClassesInFile) && false === empty($usedClassesInFile)) { |
|
148
|
|
|
$this->phpUnitFile->setUsedClasses($usedClassesInFile); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->phpUnitFile |
|
152
|
|
|
->setParentClass($this->coverFishHelper |
|
153
|
|
|
->getAttributeByKey('parent', $classData) |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
$coversDefaultClass = $this->coverFishHelper->getAnnotationByKey($classData['docblock'], 'coversDefaultClass'); |
|
157
|
|
|
if (is_array($coversDefaultClass)) { |
|
158
|
|
|
$this->phpUnitFile |
|
159
|
|
|
->setCoversDefaultClass($this->coverFishHelper->getCoversDefaultClassUsable( |
|
160
|
|
|
$coversDefaultClass |
|
161
|
|
|
)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $this->phpUnitFile; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* fetch all relevant unit test data from incoming methodDataBlock array |
|
169
|
|
|
* |
|
170
|
|
|
* @param array $methodData |
|
171
|
|
|
* |
|
172
|
|
|
* @return CoverFishPHPUnitTest |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setPHPUnitTestByMethodData(array $methodData) |
|
175
|
|
|
{ |
|
176
|
|
|
/** @var string $classFileAndPath */ |
|
177
|
|
|
$classFileAndPath = $methodData['classFile']; |
|
178
|
|
|
|
|
179
|
|
|
$phpUnitTest = new CoverFishPHPUnitTest(); |
|
180
|
|
|
$phpUnitTest->setFromMethod(true); |
|
181
|
|
|
$phpUnitTest->setSignature($methodData['signature']); |
|
182
|
|
|
$phpUnitTest->setVisibility($methodData['visibility']); |
|
183
|
|
|
$phpUnitTest->setLine($methodData['startLine']); |
|
184
|
|
|
$phpUnitTest->setFileAndPath($classFileAndPath); |
|
185
|
|
|
$phpUnitTest->setFile($this->coverFishHelper->getFileNameFromPath($classFileAndPath)); |
|
186
|
|
|
$phpUnitTest->setLoc($this->coverFishHelper->getLocOfTestMethod($methodData)); |
|
187
|
|
|
|
|
188
|
|
|
return $phpUnitTest; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param array $classData |
|
193
|
|
|
* |
|
194
|
|
|
* @return CoverFishPHPUnitTest |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setPHPUnitTestByClassData($classData) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->setPHPUnitTestMetaData($classData['className'], $classData); |
|
199
|
|
|
/** @var string $classFileAndPath */ |
|
200
|
|
|
$classFileAndPath = $classData['classFile']; |
|
201
|
|
|
|
|
202
|
|
|
$phpUnitTest = new CoverFishPHPUnitTest(); |
|
203
|
|
|
$phpUnitTest->setFromClass(true); |
|
204
|
|
|
$phpUnitTest->setSignature($classData['className']); |
|
205
|
|
|
$phpUnitTest->setFileAndPath($classFileAndPath); |
|
206
|
|
|
$phpUnitTest->setFile($this->coverFishHelper->getFileNameFromPath($classFileAndPath)); |
|
207
|
|
|
|
|
208
|
|
|
return $phpUnitTest; |
|
209
|
|
|
} |
|
210
|
|
|
} |