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
|
|
|
public function provideTestParseAndDump() |
21
|
|
|
{ |
22
|
|
|
$iter = new RecursiveIteratorIterator( |
23
|
|
|
new RecursiveDirectoryIterator( |
24
|
|
|
__DIR__ . '/../analyze-fixtures' |
25
|
|
|
), |
26
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
/** @var \SplFileInfo $file */ |
30
|
|
|
foreach ($iter as $file) { |
31
|
|
|
if (!$file->isFile()) { |
32
|
|
|
continue; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$contents = file_get_contents($file); |
36
|
|
|
list (, $expected) = explode('----------------------------', $contents); |
37
|
|
|
|
38
|
|
|
yield [$file->getPathname(), $expected]; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @dataProvider provideTestParseAndDump |
44
|
|
|
* |
45
|
|
|
* @param $file |
46
|
|
|
* @param $expectedDump |
47
|
|
|
* @throws \PHPSA\Exception\RuntimeException |
48
|
|
|
* @throws \Webiny\Component\EventManager\EventManagerException |
49
|
|
|
*/ |
50
|
|
|
public function testParseAndDump($file, $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
|
|
|
$em = EventManager::getInstance(); |
73
|
|
|
|
74
|
|
|
$bufferOutput = new \Symfony\Component\Console\Output\BufferedOutput(); |
75
|
|
|
$context = new Context( |
76
|
|
|
$bufferOutput, |
77
|
|
|
$application = new Application(), |
78
|
|
|
$em |
79
|
|
|
); |
80
|
|
|
$application->compiler = $compiler; |
81
|
|
|
|
82
|
|
|
$fileParser->parserFile($file, $context); |
83
|
|
|
|
84
|
|
|
$em->listen(Compiler\Event\ExpressionBeforeCompile::EVENT_NAME) |
85
|
|
|
->handler( |
86
|
|
|
new ExpressionListener( |
87
|
|
|
[ |
88
|
|
|
Node\Expr\FuncCall::class => [ |
89
|
|
|
new AnalyzerPass\Expression\FunctionCall\AliasCheck(), |
90
|
|
|
new AnalyzerPass\Expression\FunctionCall\DebugCode(), |
91
|
|
|
new AnalyzerPass\Expression\FunctionCall\RandomApiMigration(), |
92
|
|
|
new AnalyzerPass\Expression\FunctionCall\UseCast(), |
93
|
|
|
new AnalyzerPass\Expression\FunctionCall\DeprecatedIniOptions(), |
94
|
|
|
], |
95
|
|
|
Node\Expr\Array_::class => [ |
96
|
|
|
new AnalyzerPass\Expression\ArrayShortDefinition() |
97
|
|
|
] |
98
|
|
|
] |
99
|
|
|
) |
100
|
|
|
) |
101
|
|
|
->method('beforeCompile'); |
102
|
|
|
|
103
|
|
|
$em->listen(Compiler\Event\StatementBeforeCompile::EVENT_NAME) |
104
|
|
|
->handler( |
105
|
|
|
new StatementListener( |
106
|
|
|
[ |
107
|
|
|
Node\Stmt\ClassMethod::class => [ |
108
|
|
|
new AnalyzerPass\Statement\MethodCannotReturn() |
109
|
|
|
] |
110
|
|
|
] |
111
|
|
|
) |
112
|
|
|
) |
113
|
|
|
->method('beforeCompile'); |
114
|
|
|
|
115
|
|
|
$compiler->compile($context); |
116
|
|
|
|
117
|
|
|
self::assertSame( |
118
|
|
|
json_encode($application->getIssuesCollector()->getIssues()), |
119
|
|
|
trim($expectedDump) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|