|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DF\PHPCoverFish; |
|
4
|
|
|
|
|
5
|
|
|
use DF\PHPCoverFish\Base\BaseCoverFishScanner; |
|
6
|
|
|
use DF\PHPCoverFish\Common\CoverFishPHPUnitFile; |
|
7
|
|
|
use DF\PHPCoverFish\Common\CoverFishPHPUnitTest; |
|
8
|
|
|
use DF\PHPCoverFish\Validator\ValidatorClassName; |
|
9
|
|
|
use DF\PHPCoverFish\Validator\ValidatorClassNameMethodAccess; |
|
10
|
|
|
use DF\PHPCoverFish\Validator\ValidatorClassNameMethodName; |
|
11
|
|
|
use DF\PHPCoverFish\Validator\ValidatorMethodName; |
|
12
|
|
|
use DF\PHPCoverFish\Common\CoverFishOutput; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use \PHP_Token_Stream; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class CoverFishScanner |
|
18
|
|
|
* |
|
19
|
|
|
* @package DF\PHPCoverFish |
|
20
|
|
|
* @author Patrick Paechnatz <[email protected]> |
|
21
|
|
|
* @copyright 2015 Patrick Paechnatz <[email protected]> |
|
22
|
|
|
* @license http://www.opensource.org/licenses/MIT |
|
23
|
|
|
* @link http://github.com/dunkelfrosch/phpcoverfish/tree |
|
24
|
|
|
* @since class available since Release 0.9.0 |
|
25
|
|
|
* @version 1.0.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
class CoverFishScanner extends BaseCoverFishScanner |
|
28
|
|
|
{ |
|
29
|
|
|
const APP_RELEASE_NAME = 'PHPCoverFish'; |
|
30
|
|
|
const APP_RELEASE_STATE = 'stable'; |
|
31
|
|
|
|
|
32
|
|
|
const APP_VERSION_MAJOR = 1; |
|
33
|
|
|
const APP_VERSION_MINOR = 0; |
|
34
|
|
|
const APP_VERSION_BUILD = 0; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $cliOptions |
|
38
|
|
|
* @param array $outputOptions |
|
39
|
|
|
* @param OutputInterface $output |
|
40
|
|
|
* |
|
41
|
|
|
* @codeCoverageIgnore |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(array $cliOptions, array $outputOptions, OutputInterface $output) |
|
44
|
|
|
{ |
|
45
|
|
|
parent::__construct($cliOptions); |
|
46
|
|
|
$this->coverFishOutput = new CoverFishOutput($outputOptions, $output, $this); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* init all available cover validator classes use incoming coverToken as parameter |
|
51
|
|
|
* |
|
52
|
|
|
* @param $coverToken |
|
53
|
|
|
*/ |
|
54
|
|
|
public function addCoverageValidatorPoolForCover($coverToken) |
|
55
|
|
|
{ |
|
56
|
|
|
// covers ClassName::methodName |
|
57
|
|
|
$this->addValidator(new ValidatorClassNameMethodName($coverToken)); |
|
58
|
|
|
// covers ::methodName |
|
59
|
|
|
$this->addValidator(new ValidatorMethodName($coverToken)); |
|
60
|
|
|
// covers ClassName |
|
61
|
|
|
$this->addValidator(new ValidatorClassName($coverToken)); |
|
62
|
|
|
// covers ClassName::accessor (for public, protected, private, !public, !protected, !private) |
|
63
|
|
|
$this->addValidator(new ValidatorClassNameMethodAccess($coverToken)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* scan all unit-test files inside specific path |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function analysePHPUnitFiles() |
|
72
|
|
|
{ |
|
73
|
|
|
$testFiles = $this->scanFilesInPath($this->testSourcePath); |
|
74
|
|
|
foreach ($testFiles as $file) { |
|
75
|
|
|
$this->analyseClassesInFile($file); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->coverFishOutput->writeResult($this->coverFishResult); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* scan all annotations inside one single unit test file |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $phpDocBlock |
|
85
|
|
|
* @param CoverFishPHPUnitTest $phpUnitTest |
|
86
|
|
|
*/ |
|
87
|
|
|
public function analyseCoverAnnotations($phpDocBlock, CoverFishPHPUnitTest $phpUnitTest) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->validatorCollection->clear(); |
|
90
|
|
|
$phpUnitTest->clearCoverMappings(); |
|
91
|
|
|
$phpUnitTest->clearCoverAnnotation(); |
|
92
|
|
|
|
|
93
|
|
|
/** @var string $cover */ |
|
94
|
|
|
foreach ($phpDocBlock['covers'] as $cover) { |
|
95
|
|
|
$phpUnitTest->addCoverAnnotation($cover); |
|
96
|
|
|
$this->addCoverageValidatorPoolForCover($cover); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$phpUnitTest = $this->validateAndReturnMapping($phpUnitTest); |
|
100
|
|
|
$this->phpUnitFile->addTest($phpUnitTest); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* scan all classes inside one defined unit test file |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $file |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
public function analyseClassesInFile($file) |
|
111
|
|
|
{ |
|
112
|
|
|
$ts = new PHP_Token_Stream($file); |
|
113
|
|
|
$this->phpUnitFile = new CoverFishPHPUnitFile(); |
|
114
|
|
|
|
|
115
|
|
|
foreach ($ts->getClasses() as $className => $classData) { |
|
116
|
|
|
$fqnClass = sprintf('%s\\%s', |
|
117
|
|
|
$this->coverFishHelper->getAttributeByKey('namespace', $classData['package']), |
|
118
|
|
|
$className |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
if (false === $this->coverFishHelper->isValidTestClass($fqnClass)) { |
|
122
|
|
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$classData['className'] = $className; |
|
126
|
|
|
$classData['classFile'] = $file; |
|
127
|
|
|
$this->analyseClass($classData); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* scan (test) class and add result to our coverFishResultCollection |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $classData |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
public function analyseClass(array $classData) |
|
139
|
|
|
{ |
|
140
|
|
|
// scan main test class annotation |
|
141
|
|
|
$this->analyseCoverAnnotations( |
|
142
|
|
|
$this->coverFishHelper->parseCoverAnnotationDocBlock($classData['docblock']), |
|
143
|
|
|
$this->setPHPUnitTestByClassData($classData) |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
// iterate through all available methods in give test class ignore all "non-test" methods |
|
147
|
|
|
foreach ($classData['methods'] as $methodName => $methodData) { |
|
148
|
|
|
// ignore all non-test- and docblock free methods for deep scan process |
|
149
|
|
|
if (false === $this->getCoverFishHelper()->isValidTestMethod($methodName) || |
|
150
|
|
|
false === array_key_exists('docblock', $methodData)) { |
|
151
|
|
|
continue; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$methodData['classFile'] = (string) $classData['classFile']; |
|
155
|
|
|
// scan unit test method annotation |
|
156
|
|
|
$this->analyseCoverAnnotations( |
|
157
|
|
|
$this->coverFishHelper->parseCoverAnnotationDocBlock($methodData['docblock']), |
|
158
|
|
|
$this->setPHPUnitTestByMethodData($methodData) |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// add final phpUnitFile structure including mapping result to our coverFishResult |
|
163
|
|
|
$this->coverFishResult->addUnit($this->phpUnitFile); |
|
164
|
|
|
} |
|
165
|
|
|
} |