|
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
|
|
|
\PHPSA\Analyzer\Factory::factory(self::$em); |
|
106
|
|
|
|
|
107
|
|
|
return self::$em; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|