1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\PHPSA; |
4
|
|
|
|
5
|
|
|
use PhpParser\ParserFactory; |
6
|
|
|
use PHPSA\Analyzer\EventListener\ExpressionListener; |
7
|
|
|
use PHPSA\Analyzer\EventListener\StatementListener; |
8
|
|
|
use PHPSA\Application; |
9
|
|
|
use PHPSA\Context; |
10
|
|
|
use PHPSA\Definition\FileParser; |
11
|
|
|
use RecursiveDirectoryIterator; |
12
|
|
|
use RecursiveIteratorIterator; |
13
|
|
|
use Webiny\Component\EventManager\EventManager; |
14
|
|
|
use PHPSA\Compiler; |
15
|
|
|
use PhpParser\Node; |
16
|
|
|
use PHPSA\Analyzer\Pass as AnalyzerPass; |
17
|
|
|
|
18
|
|
|
class AnalyzeFixturesTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var EventManager |
22
|
|
|
*/ |
23
|
|
|
static protected $em; |
24
|
|
|
|
25
|
|
|
public function provideTestParseAndDump() |
26
|
|
|
{ |
27
|
|
|
$iter = new RecursiveIteratorIterator( |
28
|
|
|
new RecursiveDirectoryIterator( |
29
|
|
|
__DIR__ . '/../analyze-fixtures' |
30
|
|
|
), |
31
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** @var \SplFileInfo $file */ |
35
|
|
|
foreach ($iter as $file) { |
36
|
|
|
if (!$file->isFile()) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$contents = file_get_contents($file); |
41
|
|
|
list (, $expected) = explode('----------------------------', $contents); |
42
|
|
|
|
43
|
|
|
yield [$file->getPathname(), $expected]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider provideTestParseAndDump |
49
|
|
|
* |
50
|
|
|
* @param $file |
51
|
|
|
* @param $expectedDump |
52
|
|
|
* @throws \PHPSA\Exception\RuntimeException |
53
|
|
|
* @throws \Webiny\Component\EventManager\EventManagerException |
54
|
|
|
*/ |
55
|
|
|
public function testParseAndDump($file, $expectedDump) |
56
|
|
|
{ |
57
|
|
|
$compiler = new Compiler(); |
58
|
|
|
|
59
|
|
|
$fileParser = new FileParser( |
60
|
|
|
(new ParserFactory())->create( |
61
|
|
|
ParserFactory::PREFER_PHP7, |
62
|
|
|
new \PhpParser\Lexer\Emulative( |
63
|
|
|
array( |
64
|
|
|
'usedAttributes' => array( |
65
|
|
|
'comments', |
66
|
|
|
'startLine', |
67
|
|
|
'endLine', |
68
|
|
|
'startTokenPos', |
69
|
|
|
'endTokenPos' |
70
|
|
|
) |
71
|
|
|
) |
72
|
|
|
) |
73
|
|
|
), |
74
|
|
|
$compiler |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$context = new Context( |
78
|
|
|
new \Symfony\Component\Console\Output\NullOutput(), |
79
|
|
|
$application = new Application(), |
80
|
|
|
$this->getEventManager() |
81
|
|
|
); |
82
|
|
|
$application->compiler = $compiler; |
83
|
|
|
|
84
|
|
|
$fileParser->parserFile($file, $context); |
85
|
|
|
|
86
|
|
|
$compiler->compile($context); |
87
|
|
|
|
88
|
|
|
self::assertEquals( |
89
|
|
|
$application->getIssuesCollector()->getIssues(), |
90
|
|
|
json_decode(trim($expectedDump), true) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return EventManager |
96
|
|
|
* @throws \Webiny\Component\EventManager\EventManagerException |
97
|
|
|
*/ |
98
|
|
|
protected function getEventManager() |
99
|
|
|
{ |
100
|
|
|
if (self::$em) { |
101
|
|
|
return self::$em; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
self::$em = EventManager::getInstance(); |
105
|
|
|
self::$em->listen(Compiler\Event\ExpressionBeforeCompile::EVENT_NAME) |
106
|
|
|
->handler( |
107
|
|
|
new ExpressionListener( |
108
|
|
|
[ |
109
|
|
|
Node\Expr\FuncCall::class => [ |
110
|
|
|
new AnalyzerPass\Expression\FunctionCall\AliasCheck(), |
111
|
|
|
new AnalyzerPass\Expression\FunctionCall\DebugCode(), |
112
|
|
|
new AnalyzerPass\Expression\FunctionCall\RandomApiMigration(), |
113
|
|
|
new AnalyzerPass\Expression\FunctionCall\UseCast(), |
114
|
|
|
new AnalyzerPass\Expression\FunctionCall\DeprecatedIniOptions(), |
115
|
|
|
], |
116
|
|
|
Node\Expr\Array_::class => [ |
117
|
|
|
new AnalyzerPass\Expression\ArrayShortDefinition() |
118
|
|
|
] |
119
|
|
|
] |
120
|
|
|
) |
121
|
|
|
) |
122
|
|
|
->method('beforeCompile'); |
123
|
|
|
|
124
|
|
|
self::$em->listen(Compiler\Event\StatementBeforeCompile::EVENT_NAME) |
125
|
|
|
->handler( |
126
|
|
|
new StatementListener( |
127
|
|
|
[ |
128
|
|
|
Node\Stmt\ClassMethod::class => [ |
129
|
|
|
new AnalyzerPass\Statement\MethodCannotReturn() |
130
|
|
|
] |
131
|
|
|
] |
132
|
|
|
) |
133
|
|
|
) |
134
|
|
|
->method('beforeCompile'); |
135
|
|
|
|
136
|
|
|
return self::$em; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|