1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ComposerRequireCheckerTest\NodeVisitor; |
4
|
|
|
|
5
|
|
|
use ComposerRequireChecker\NodeVisitor\DefinedSymbolCollector; |
6
|
|
|
use PhpParser\Node\Arg; |
7
|
|
|
use PhpParser\Node\Expr\FuncCall; |
8
|
|
|
use PhpParser\Node\Identifier; |
9
|
|
|
use PhpParser\Node\Name; |
10
|
|
|
use PhpParser\Node\Scalar\String_; |
11
|
|
|
use PhpParser\Node\Stmt\Class_; |
12
|
|
|
use PhpParser\Node\Stmt\Const_; |
13
|
|
|
use PhpParser\Node\Stmt\Function_; |
14
|
|
|
use PhpParser\Node\Stmt\Interface_; |
15
|
|
|
use PhpParser\Node\Stmt\Trait_; |
16
|
|
|
use PhpParser\NodeTraverser; |
17
|
|
|
use PhpParser\NodeTraverserInterface; |
18
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
19
|
|
|
use PhpParser\Parser; |
20
|
|
|
use PhpParser\ParserFactory; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
|
23
|
|
|
final class DefinedSymbolCollectorTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var DefinedSymbolCollector |
27
|
|
|
*/ |
28
|
|
|
private $collector; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Parser |
32
|
|
|
*/ |
33
|
|
|
private $parser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var NodeTraverserInterface |
37
|
|
|
*/ |
38
|
|
|
private $traverser; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
protected function setUp(): void |
44
|
|
|
{ |
45
|
|
|
$this->collector = new DefinedSymbolCollector(); |
46
|
|
|
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
47
|
|
|
$this->traverser = new NodeTraverser(); |
48
|
|
|
|
49
|
|
|
$this->traverser->addVisitor(new NameResolver()); |
50
|
|
|
$this->traverser->addVisitor($this->collector); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testExceptionWhenNoNamespaceDefined(): void |
54
|
|
|
{ |
55
|
|
|
$this->expectException(\UnexpectedValueException::class); |
56
|
|
|
$node = new Class_('gedöns'); |
57
|
|
|
$this->collector->enterNode($node); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testRecordDefinedConstDefinition(): void |
61
|
|
|
{ |
62
|
|
|
$node = new FuncCall(new Name('define'), [ |
63
|
|
|
new Arg(new String_('CONST_A')), |
64
|
|
|
new Arg(new String_('VALUE_A')), |
65
|
|
|
]); |
66
|
|
|
$this->collector->enterNode($node); |
67
|
|
|
|
68
|
|
|
$this->assertContains('CONST_A', $this->collector->getDefinedSymbols()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testDontRecordNamespacedDefinedConstDefinition(): void |
72
|
|
|
{ |
73
|
|
|
$node = new FuncCall(new Name('define', ['namespacedName' => new Name\FullyQualified('Foo\define')]), [ |
74
|
|
|
new Arg(new String_('NO_CONST')), |
75
|
|
|
new Arg(new String_('VALUE_A')), |
76
|
|
|
]); |
77
|
|
|
$this->collector->enterNode($node); |
78
|
|
|
|
79
|
|
|
$this->assertEmpty($this->collector->getDefinedSymbols()); |
80
|
|
|
$this->assertNotContains('NO_CONST', $this->collector->getDefinedSymbols()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testRecordClassDefinition(): void |
84
|
|
|
{ |
85
|
|
|
$node = new Class_(new Identifier('Foo')); |
86
|
|
|
$this->traverser->traverse([$node]); |
87
|
|
|
|
88
|
|
|
$this->assertContains('Foo', $this->collector->getDefinedSymbols()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testRecordInterfaceDefinition(): void |
92
|
|
|
{ |
93
|
|
|
$node = new Interface_(new Identifier('Foo')); |
94
|
|
|
$this->traverser->traverse([$node]); |
95
|
|
|
|
96
|
|
|
$this->assertContains('Foo', $this->collector->getDefinedSymbols()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testRecordTraitDefinition(): void |
100
|
|
|
{ |
101
|
|
|
$node = new Trait_(new Identifier('Foo')); |
102
|
|
|
$this->traverser->traverse([$node]); |
103
|
|
|
|
104
|
|
|
$this->assertContains('Foo', $this->collector->getDefinedSymbols()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testRecordFunctionDefinition(): void |
108
|
|
|
{ |
109
|
|
|
$node = new Function_(new Identifier('Foo')); |
110
|
|
|
$this->traverser->traverse([$node]); |
111
|
|
|
|
112
|
|
|
$this->assertContains('Foo', $this->collector->getDefinedSymbols()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testRecordConstDefinition(): void |
116
|
|
|
{ |
117
|
|
|
$node = new Const_([ |
118
|
|
|
new \PhpParser\Node\Const_(new Name('CONST_A'), new String_('foo')), |
119
|
|
|
new \PhpParser\Node\Const_(new Name('CONST_B'), new String_('foo')), |
120
|
|
|
]); |
121
|
|
|
$this->traverser->traverse([$node]); |
122
|
|
|
|
123
|
|
|
$this->assertContains('CONST_A', $this->collector->getDefinedSymbols()); |
124
|
|
|
$this->assertContains('CONST_B', $this->collector->getDefinedSymbols()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|