1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ComposerRequireCheckerTest\NodeVisitor; |
4
|
|
|
|
5
|
|
|
use ComposerRequireChecker\NodeVisitor\IncludeCollector; |
6
|
|
|
use PhpParser\Node; |
7
|
|
|
use PhpParser\Node\Expr\BinaryOp\Concat; |
8
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
9
|
|
|
use PhpParser\Node\Expr\Include_; |
10
|
|
|
use PhpParser\Node\Expr\Variable; |
11
|
|
|
use PhpParser\Node\Name; |
12
|
|
|
use PhpParser\Node\Scalar\MagicConst\Dir; |
13
|
|
|
use PhpParser\Node\Scalar\MagicConst\File; |
14
|
|
|
use PhpParser\Node\Scalar\String_; |
15
|
|
|
use PhpParser\Node\Stmt\Expression; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use ReflectionProperty; |
18
|
|
|
|
19
|
|
|
class IncludeCollectorTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* yields testcase datasets |
23
|
|
|
* @return iterable |
24
|
|
|
*/ |
25
|
|
|
public function provideGetIncluded(): iterable |
26
|
|
|
{ |
27
|
|
|
yield "no includes" => [__FILE__, [], []]; |
28
|
|
|
yield "simple include" => [__FILE__, ['anywhere/here.php'], [__DIR__ . '/anywhere/here.php']]; |
29
|
|
|
yield "simple absolute include" => [__FILE__, ['/anywhere/here.php'], ['/anywhere/here.php']]; |
30
|
|
|
yield "simple String_ include" => [ |
31
|
|
|
__FILE__, |
32
|
|
|
[new String_('anywhere/here.php')], |
33
|
|
|
[__DIR__ . '/anywhere/here.php'] |
34
|
|
|
]; |
35
|
|
|
yield "absolute include by DIR" => [ |
36
|
|
|
__FILE__, |
37
|
|
|
[ |
38
|
|
|
new Concat( |
39
|
|
|
new Dir(), |
40
|
|
|
new String_('anywhere/here.php') |
41
|
|
|
) |
42
|
|
|
], |
43
|
|
|
[__DIR__ . 'anywhere/here.php'] |
44
|
|
|
]; |
45
|
|
|
yield "absolute include by FILE" => [ |
46
|
|
|
__FILE__, |
47
|
|
|
[ |
48
|
|
|
new Concat( |
49
|
|
|
new File(), |
50
|
|
|
new String_('anywhere/here.php') |
51
|
|
|
) |
52
|
|
|
], |
53
|
|
|
[__FILE__ . 'anywhere/here.php'] |
54
|
|
|
]; |
55
|
|
|
yield "includes with variables" => [ |
56
|
|
|
__FILE__, |
57
|
|
|
[ |
58
|
|
|
new Concat( |
59
|
|
|
new ConstFetch(new Name("NAME")), |
60
|
|
|
new String_('.php') |
61
|
|
|
) |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'DefinedSymbolCollectorFunctionalTest.php', |
65
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'DefinedSymbolCollectorTest.php', |
66
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'UsedSymbolCollectorFunctionalTest.php', |
67
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'UsedSymbolCollectorTest.php' |
68
|
|
|
] |
69
|
|
|
]; |
70
|
|
|
yield "includes with constants" => [ |
71
|
|
|
__FILE__, |
72
|
|
|
[ |
73
|
|
|
new Concat( |
74
|
|
|
new Variable(new Name('name')), |
75
|
|
|
new String_('.php') |
76
|
|
|
) |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'DefinedSymbolCollectorFunctionalTest.php', |
80
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'DefinedSymbolCollectorTest.php', |
81
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'UsedSymbolCollectorFunctionalTest.php', |
82
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'UsedSymbolCollectorTest.php' |
83
|
|
|
] |
84
|
|
|
]; |
85
|
|
|
yield "includes with DIRECTORY_SEPARATOR" => [ |
86
|
|
|
__FILE__, |
87
|
|
|
[ |
88
|
|
|
new Concat( |
89
|
|
|
new Dir(), |
90
|
|
|
new Concat( |
91
|
|
|
new ConstFetch(new Name('DIRECTORY_SEPARATOR')), |
92
|
|
|
new String_('Example.php') |
93
|
|
|
) |
94
|
|
|
) |
95
|
|
|
], |
96
|
|
|
[ |
97
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'Example.php' |
98
|
|
|
] |
99
|
|
|
]; |
100
|
|
|
yield "includes with invalid node" => [ |
101
|
|
|
__FILE__, |
102
|
|
|
[ |
103
|
|
|
new Expression(new String_('')) |
104
|
|
|
], |
105
|
|
|
[] |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @test |
111
|
|
|
* @dataProvider provideGetIncluded |
112
|
|
|
* @param string $file |
113
|
|
|
* @param array $included |
114
|
|
|
* @param array $result |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function testGetIncluded(string $file, array $included, array $result): void |
118
|
|
|
{ |
119
|
|
|
$collector = new IncludeCollector(); |
120
|
|
|
$reflector = new ReflectionProperty($collector, 'included'); |
121
|
|
|
$reflector->setAccessible(true); |
122
|
|
|
$reflector->setValue($collector, $included); |
123
|
|
|
$reflector->setAccessible(false); |
124
|
|
|
$collected = $collector->getIncluded($file); |
125
|
|
|
sort($result); |
126
|
|
|
sort($collected); |
127
|
|
|
$this->assertEquals($result, $collected); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @test |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function testBeforeTraverseEmptiesIncludes(): void |
135
|
|
|
{ |
136
|
|
|
$collector = new IncludeCollector(); |
137
|
|
|
$reflector = new ReflectionProperty($collector, 'included'); |
138
|
|
|
$reflector->setAccessible(true); |
139
|
|
|
$reflector->setValue($collector, ['a', '#', 'p']); |
140
|
|
|
$reflector->setAccessible(false); |
141
|
|
|
$this->assertAttributeCount(3, 'included', $collector); |
142
|
|
|
$collector->beforeTraverse([]); |
143
|
|
|
$this->assertAttributeCount(0, 'included', $collector); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return iterable |
148
|
|
|
*/ |
149
|
|
|
public function provideEnterNode(): iterable |
150
|
|
|
{ |
151
|
|
|
$expr = new String_(''); |
152
|
|
|
yield "require" => [new Include_($expr, Include_::TYPE_REQUIRE), 1]; |
153
|
|
|
yield "require_once" => [new Include_($expr, Include_::TYPE_REQUIRE_ONCE), 1]; |
154
|
|
|
yield "include" => [new Include_($expr, Include_::TYPE_INCLUDE), 1]; |
155
|
|
|
yield "include_once" => [new Include_($expr, Include_::TYPE_INCLUDE_ONCE), 1]; |
156
|
|
|
yield "different node" => [$expr, 0]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @test |
161
|
|
|
* @dataProvider provideEnterNode |
162
|
|
|
* @param Node $node |
163
|
|
|
* @param int $count |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function testEnterNode(Node $node, int $count): void |
167
|
|
|
{ |
168
|
|
|
$collector = new IncludeCollector(); |
169
|
|
|
$collector->enterNode($node); |
170
|
|
|
$this->assertAttributeCount($count, 'included', $collector); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|