Failed Conditions
Push — master ( 1771c4...535739 )
by Matthias
03:44
created

UsedSymbolCollectorTest::testExtendingClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace ComposerRequireCheckerTest\NodeVisitor;
4
5
use ComposerRequireChecker\NodeVisitor\UsedSymbolCollector;
6
use PhpParser\Node\Expr\ClassConstFetch;
7
use PhpParser\Node\Expr\ConstFetch;
8
use PhpParser\Node\Expr\FuncCall;
9
use PhpParser\Node\Expr\Instanceof_;
10
use PhpParser\Node\Expr\New_;
11
use PhpParser\Node\Expr\StaticCall;
12
use PhpParser\Node\Expr\StaticPropertyFetch;
13
use PhpParser\Node\Expr\Variable;
14
use PhpParser\Node\Name;
15
use PhpParser\Node\Param;
16
use PhpParser\Node\Stmt\Catch_;
17
use PhpParser\Node\Stmt\Class_;
18
use PhpParser\Node\Stmt\ClassMethod;
19
use PhpParser\Node\Stmt\Function_;
20
use PhpParser\Node\Stmt\Interface_;
21
use PhpParser\Node\Stmt\TraitUse;
22
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;
23
use PHPUnit\Framework\TestCase;
24
25
/**
26
 * @covers \ComposerRequireChecker\NodeVisitor\UsedSymbolCollector
27
 */
28
class UsedSymbolCollectorTest extends TestCase
29
{
30
    /** @var UsedSymbolCollector */
31
    private $visitor;
32
33
    protected function setUp()
34
    {
35
        parent::setUp();
36
37
        $this->visitor = new UsedSymbolCollector();
38
    }
39
40
    public function testExtendingClass()
41
    {
42
        $node = new Class_('Foo');
43
        $node->extends = new Name('Bar');
44
45
        $this->visitor->enterNode($node);
46
47
        $symbols = $this->visitor->getCollectedSymbols();
48
        $this->assertCount(1, $symbols);
49
        $this->assertContains('Bar', $symbols);
50
    }
51
52
    public function testExtendingInterface()
53
    {
54
        $node = new Interface_('Foo');
55
        $node->extends = [new Name('Bar'), new Name('Baz')];
56
57
        $this->visitor->enterNode($node);
58
59
        $symbols = $this->visitor->getCollectedSymbols();
60
        $this->assertCount(2, $symbols);
61
        $this->assertContains('Bar', $symbols);
62
        $this->assertContains('Baz', $symbols);
63
    }
64
65
    public function testImplements()
66
    {
67
        $node = new Class_('Foo');
68
        $node->implements = [new Name('Bar'), new Name('Baz')];
69
70
        $this->visitor->enterNode($node);
71
72
        $symbols = $this->visitor->getCollectedSymbols();
73
        $this->assertCount(2, $symbols);
74
        $this->assertContains('Bar', $symbols);
75
        $this->assertContains('Baz', $symbols);
76
    }
77
78
    public function testStaticCall()
79
    {
80
        $class = new Name('Foo');
81
        $node = new StaticCall($class, 'foo');
82
        $node->class = $class;
83
84
        $this->visitor->enterNode($node);
85
86
        $symbols = $this->visitor->getCollectedSymbols();
87
        $this->assertCount(1, $symbols);
88
        $this->assertContains('Foo', $symbols);
89
    }
90
91
    public function testStaticPropertyFetch()
92
    {
93
        $class = new Name('Foo');
94
        $node = new StaticPropertyFetch($class, 'foo');
95
        $node->class = $class;
96
97
        $this->visitor->enterNode($node);
98
99
        $symbols = $this->visitor->getCollectedSymbols();
100
        $this->assertCount(1, $symbols);
101
        $this->assertContains('Foo', $symbols);
102
    }
103
104
    public function testClassConstantFetch()
105
    {
106
        $class = new Name('Foo');
107
        $node = new ClassConstFetch($class, 'FOO');
108
        $node->class = $class;
109
110
        $this->visitor->enterNode($node);
111
112
        $symbols = $this->visitor->getCollectedSymbols();
113
        $this->assertCount(1, $symbols);
114
        $this->assertContains('Foo', $symbols);
115
    }
116
117
    public function testNew()
118
    {
119
        $class = new Name('Foo');
120
        $node = new New_($class);
121
        $node->class = $class;
122
123
        $this->visitor->enterNode($node);
124
125
        $symbols = $this->visitor->getCollectedSymbols();
126
        $this->assertCount(1, $symbols);
127
        $this->assertContains('Foo', $symbols);
128
    }
129
130
    public function testInstanceof()
131
    {
132
        $class = new Name('Foo');
133
        $node = new Instanceof_(new Variable('foo'), $class);
134
        $node->class = $class;
135
136
        $this->visitor->enterNode($node);
137
138
        $symbols = $this->visitor->getCollectedSymbols();
139
        $this->assertCount(1, $symbols);
140
        $this->assertContains('Foo', $symbols);
141
    }
142
143
    public function testCatch()
144
    {
145
        $class = new Name('Foo');
146
        $node = new Catch_([$class], 'e');
147
148
        $this->visitor->enterNode($node);
149
150
        $symbols = $this->visitor->getCollectedSymbols();
151
        $this->assertCount(1, $symbols);
152
        $this->assertContains('Foo', $symbols);
153
    }
154
155
    public function testFunctionCallUsage()
156
    {
157
        $functionName = new Name('foo');
158
        $node = new FuncCall($functionName);
159
        $node->name = $functionName;
160
161
        $this->visitor->enterNode($node);
162
163
        $symbols = $this->visitor->getCollectedSymbols();
164
        $this->assertCount(1, $symbols);
165
        $this->assertContains('foo', $symbols);
166
    }
167
168
    public function testFunctionParameterType()
169
    {
170
        $functionName = new Name('foo');
171
        $node = new Function_($functionName);
172
        $node->name = $functionName;
173
        $param = new Param('bar');
174
        $param->type = new Name('Baz');
175
        $node->params = [$param];
176
177
        $this->visitor->enterNode($node);
178
179
        $symbols = $this->visitor->getCollectedSymbols();
180
        $this->assertCount(1, $symbols);
181
        $this->assertContains('Baz', $symbols);
182
    }
183
184
    public function testFunctionParameterTypeAsString()
185
    {
186
        $functionName = new Name('foo');
187
        $node = new Function_($functionName);
188
        $node->name = $functionName;
189
        $param = new Param('bar');
190
        $param->type = 'Baz';
191
        $node->params = [$param];
192
193
        $this->visitor->enterNode($node);
194
195
        $symbols = $this->visitor->getCollectedSymbols();
196
        $this->assertCount(1, $symbols);
197
        $this->assertContains('Baz', $symbols);
198
    }
199
200
    public function testMethodParameterType()
201
    {
202
        $functionName = new Name('foo');
203
        $node = new ClassMethod($functionName);
204
        $node->name = $functionName;
205
        $param = new Param('bar');
206
        $param->type = new Name('Baz');
207
        $node->params = [$param];
208
209
        $this->visitor->enterNode($node);
210
211
        $symbols = $this->visitor->getCollectedSymbols();
212
        $this->assertCount(1, $symbols);
213
        $this->assertContains('Baz', $symbols);
214
    }
215
216
    public function testMethodParameterTypeAsString()
217
    {
218
        $functionName = new Name('foo');
219
        $node = new ClassMethod($functionName);
220
        $node->name = $functionName;
221
        $param = new Param('bar');
222
        $param->type = 'Baz';
223
        $node->params = [$param];
224
225
        $this->visitor->enterNode($node);
226
227
        $symbols = $this->visitor->getCollectedSymbols();
228
        $this->assertCount(1, $symbols);
229
        $this->assertContains('Baz', $symbols);
230
    }
231
232
    public function testFunctionReturnType()
233
    {
234
        $functionName = new Name('foo');
235
        $node = new Function_($functionName);
236
        $node->returnType = new Name('Bar');
237
238
        $this->visitor->enterNode($node);
239
240
        $symbols = $this->visitor->getCollectedSymbols();
241
        $this->assertCount(1, $symbols);
242
        $this->assertContains('Bar', $symbols);
243
    }
244
245
    public function testFunctionReturnTypeAsString()
246
    {
247
        $functionName = new Name('foo');
248
        $node = new Function_($functionName);
249
        $node->returnType = 'Bar';
250
251
        $this->visitor->enterNode($node);
252
253
        $symbols = $this->visitor->getCollectedSymbols();
254
        $this->assertCount(1, $symbols);
255
        $this->assertContains('Bar', $symbols);
256
    }
257
258
    public function testMethodReturnType()
259
    {
260
        $functionName = new Name('foo');
261
        $node = new ClassMethod($functionName);
262
        $node->returnType = new Name('Bar');
263
264
        $this->visitor->enterNode($node);
265
266
        $symbols = $this->visitor->getCollectedSymbols();
267
        $this->assertCount(1, $symbols);
268
        $this->assertContains('Bar', $symbols);
269
    }
270
271
    public function testMethodReturnTypeAsString()
272
    {
273
        $functionName = new Name('foo');
274
        $node = new ClassMethod($functionName);
275
        $node->returnType = 'Bar';
276
277
        $this->visitor->enterNode($node);
278
279
        $symbols = $this->visitor->getCollectedSymbols();
280
        $this->assertCount(1, $symbols);
281
        $this->assertContains('Bar', $symbols);
282
    }
283
284
    public function testConstantFetch()
285
    {
286
        $exceptionClass = new Name('FooException');
287
        $node = new ConstFetch($exceptionClass);
288
        $node->name = $exceptionClass;
289
290
        $this->visitor->enterNode($node);
291
292
        $symbols = $this->visitor->getCollectedSymbols();
293
        $this->assertCount(1, $symbols);
294
        $this->assertContains('FooException', $symbols);
295
    }
296
297
    public function testTraits()
298
    {
299
        $node = new TraitUse([new Name('Foo')]);
300
301
        $this->visitor->enterNode($node);
302
303
        $symbols = $this->visitor->getCollectedSymbols();
304
        $this->assertCount(1, $symbols);
305
        $this->assertContains('Foo', $symbols);
306
    }
307
308
    public function testTraitUseVisibilityAdaptation()
309
    {
310
        $traitUseAdaption = new Alias(null, 'testMethod', Class_::MODIFIER_PUBLIC, null);
311
        $traitUse = new TraitUse([new Name('Foo')], [$traitUseAdaption]);
312
313
        $this->visitor->enterNode($traitUse);
314
315
        $symbols = $this->visitor->getCollectedSymbols();
316
        $this->assertCount(1, $symbols);
317
        $this->assertContains('Foo', $symbols);
318
    }
319
320
    public function testBeforeTraverseResetsRecordedSymbols()
321
    {
322
        $node = new Class_('Foo');
323
        $node->extends = new Name('Bar');
324
        $this->visitor->enterNode($node);
325
326
        $this->visitor->beforeTraverse([]);
327
328
        $this->assertCount(0, $this->visitor->getCollectedSymbols());
329
    }
330
}
331