|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\PHPSA; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\ParserFactory; |
|
6
|
|
|
use PHPSA\Analyzer; |
|
7
|
|
|
use PHPSA\Application; |
|
8
|
|
|
use PHPSA\Configuration; |
|
9
|
|
|
use PHPSA\Context; |
|
10
|
|
|
use PHPSA\Definition\FileParser; |
|
11
|
|
|
use PHPSA\Issue; |
|
12
|
|
|
use RecursiveDirectoryIterator; |
|
13
|
|
|
use RecursiveIteratorIterator; |
|
14
|
|
|
use Webiny\Component\EventManager\EventManager; |
|
15
|
|
|
use PHPSA\Compiler; |
|
16
|
|
|
|
|
17
|
|
|
class AnalyzeFixturesTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function provideTestParseAndDump() |
|
20
|
|
|
{ |
|
21
|
|
|
$iter = new RecursiveIteratorIterator( |
|
22
|
|
|
new RecursiveDirectoryIterator( |
|
23
|
|
|
__DIR__ . '/../analyze-fixtures' |
|
24
|
|
|
), |
|
25
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
/** @var \SplFileInfo $file */ |
|
29
|
|
|
foreach ($iter as $file) { |
|
30
|
|
|
if (!$file->isFile()) { |
|
31
|
|
|
continue; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$contents = file_get_contents($file); |
|
35
|
|
|
list (, $analyzer, $expected) = explode('----------------------------', $contents); |
|
36
|
|
|
|
|
37
|
|
|
yield [$file->getPathname(), trim($analyzer), trim($expected)]; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @dataProvider provideTestParseAndDump |
|
43
|
|
|
* |
|
44
|
|
|
* @param $file |
|
45
|
|
|
* @param string $analyzer |
|
46
|
|
|
* @param string $expectedDump |
|
47
|
|
|
* @throws \PHPSA\Exception\RuntimeException |
|
48
|
|
|
* @throws \Webiny\Component\EventManager\EventManagerException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testParseAndDump($file, $analyzer, $expectedDump) |
|
51
|
|
|
{ |
|
52
|
|
|
$compiler = new Compiler(); |
|
53
|
|
|
|
|
54
|
|
|
$fileParser = new FileParser( |
|
55
|
|
|
(new ParserFactory())->create( |
|
56
|
|
|
ParserFactory::PREFER_PHP7, |
|
57
|
|
|
new \PhpParser\Lexer\Emulative( |
|
58
|
|
|
array( |
|
59
|
|
|
'usedAttributes' => array( |
|
60
|
|
|
'comments', |
|
61
|
|
|
'startLine', |
|
62
|
|
|
'endLine', |
|
63
|
|
|
'startTokenPos', |
|
64
|
|
|
'endTokenPos' |
|
65
|
|
|
) |
|
66
|
|
|
) |
|
67
|
|
|
) |
|
68
|
|
|
), |
|
69
|
|
|
$compiler |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
$context = new Context( |
|
73
|
|
|
new \Symfony\Component\Console\Output\NullOutput(), |
|
74
|
|
|
$application = new Application(), |
|
75
|
|
|
$this->getEventManager($analyzer) |
|
76
|
|
|
); |
|
77
|
|
|
$application->compiler = $compiler; |
|
78
|
|
|
|
|
79
|
|
|
$fileParser->parserFile($file, $context); |
|
80
|
|
|
|
|
81
|
|
|
$compiler->compile($context); |
|
82
|
|
|
|
|
83
|
|
|
$expectedArray = json_decode($expectedDump, true); |
|
84
|
|
|
$expectedType = $expectedArray[0]["type"]; |
|
85
|
|
|
$issues = array_map( |
|
86
|
|
|
// @todo Remove after moving all notices on Issue(s) |
|
87
|
|
|
function (Issue $issue) { |
|
88
|
|
|
$location = $issue->getLocation(); |
|
89
|
|
|
|
|
90
|
|
|
return [ |
|
91
|
|
|
'type' => $issue->getCheckName(), |
|
92
|
|
|
'message' => $issue->getDescription(), |
|
93
|
|
|
'file' => $location->getFileName(), |
|
94
|
|
|
'line' => $location->getLineStart(), |
|
95
|
|
|
]; |
|
96
|
|
|
}, |
|
97
|
|
|
$application->getIssuesCollector()->getIssues() |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
foreach ($expectedArray as $check) { |
|
101
|
|
|
self::assertContains($check, $issues, $file); // every expected Issue is in the collector |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($issues as $check) { |
|
105
|
|
|
if ($check["type"] == $expectedType) { |
|
106
|
|
|
self::assertContains($check, $expectedArray, $file); // there is no other issue in the collector with the same type |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $analyzerName |
|
113
|
|
|
* @return EventManager |
|
114
|
|
|
* @throws \Webiny\Component\EventManager\EventManagerException |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getEventManager($analyzerName) |
|
117
|
|
|
{ |
|
118
|
|
|
if (!class_exists($analyzerName, true)) { |
|
119
|
|
|
throw new \InvalidArgumentException("Analyzer with name: {$analyzerName} does not exist"); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** @var \PHPSA\Analyzer\Pass\Metadata $metaData */ |
|
123
|
|
|
$metaData = $analyzerName::getMetadata(); |
|
124
|
|
|
if (!$metaData->allowsPhpVersion(PHP_VERSION)) { |
|
125
|
|
|
parent::markTestSkipped( |
|
|
|
|
|
|
126
|
|
|
sprintf( |
|
127
|
|
|
'We cannot tests %s with %s because PHP required version is %s', |
|
128
|
|
|
$analyzerName, |
|
129
|
|
|
PHP_VERSION, |
|
130
|
|
|
$metaData->getRequiredPhpVersion() |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** @var \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $analyzerConfiguration */ |
|
136
|
|
|
$analyzerConfiguration = $metaData->getConfiguration(); |
|
137
|
|
|
$analyzerConfiguration->attribute('enabled', true); |
|
138
|
|
|
|
|
139
|
|
|
$configuration = new Configuration( |
|
140
|
|
|
[], |
|
141
|
|
|
[ |
|
142
|
|
|
$analyzerConfiguration |
|
143
|
|
|
] |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
$analyzersConfig = $configuration->getValue('analyzers'); |
|
147
|
|
|
$analyzersConfig = array_merge( |
|
148
|
|
|
$analyzersConfig, |
|
149
|
|
|
[ |
|
150
|
|
|
$metaData->getName() => array_map( |
|
151
|
|
|
function () { |
|
152
|
|
|
return true; |
|
153
|
|
|
}, |
|
154
|
|
|
$analyzersConfig[$metaData->getName()] |
|
155
|
|
|
) |
|
156
|
|
|
] |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
$configuration->setValue('analyzers', $analyzersConfig); |
|
160
|
|
|
|
|
161
|
|
|
$em = EventManager::getInstance(); |
|
162
|
|
|
\PHPSA\Analyzer\Factory::factory($em, $configuration); |
|
163
|
|
|
|
|
164
|
|
|
return $em; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.