Completed
Pull Request — master (#45)
by Matthias
03:25
created

testTraitUseVisibilityAdaptation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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 View Code Duplication
    public function testExtendingInterface()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testImplements()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testStaticCall()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testStaticPropertyFetch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testClassConstantFetch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testNew()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testInstanceof()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testFunctionCallUsage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testFunctionParameterType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testFunctionParameterTypeAsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testMethodParameterType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testMethodParameterTypeAsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testFunctionReturnType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testFunctionReturnTypeAsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testMethodReturnType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testMethodReturnTypeAsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testConstantFetch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testTraits()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testBeforeTraverseResetsRecordedSymbols()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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