|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ComposerRequireCheckerTest\DefinedSymbolsLocator; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayObject; |
|
6
|
|
|
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots; |
|
7
|
|
|
use PhpParser\Node\Arg; |
|
8
|
|
|
use PhpParser\Node\Expr\FuncCall; |
|
9
|
|
|
use PhpParser\Node\Name; |
|
10
|
|
|
use PhpParser\Node\Scalar\String_; |
|
11
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
12
|
|
|
use PhpParser\Node\Stmt\Function_; |
|
13
|
|
|
use PhpParser\Node\Stmt\Trait_; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @covers \ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots |
|
18
|
|
|
*/ |
|
19
|
|
|
class LocateDefinedSymbolsFromASTRootsTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var LocateDefinedSymbolsFromASTRoots */ |
|
22
|
|
|
private $locator; |
|
23
|
|
|
|
|
24
|
|
|
protected function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
parent::setUp(); |
|
27
|
|
|
|
|
28
|
|
|
$this->locator = new LocateDefinedSymbolsFromASTRoots(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testNoRoots() |
|
32
|
|
|
{ |
|
33
|
|
|
$symbols = $this->locate([]); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertCount(0, $symbols); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testBasicLocateClass() |
|
39
|
|
|
{ |
|
40
|
|
|
$roots = [ |
|
41
|
|
|
[new Class_('MyClassA'), new Class_('MyClassB')], |
|
42
|
|
|
[new Class_('MyClassC')], |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
$symbols = $this->locate([$roots]); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertInternalType('array', $symbols); |
|
48
|
|
|
$this->assertCount(3, $symbols); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertContains('MyClassA', $symbols); |
|
51
|
|
|
$this->assertContains('MyClassB', $symbols); |
|
52
|
|
|
$this->assertContains('MyClassC', $symbols); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testBasicLocateFunctions() |
|
56
|
|
|
{ |
|
57
|
|
|
$roots = [ |
|
58
|
|
|
[new Function_('myFunctionA')], |
|
59
|
|
|
[new Class_('myFunctionB')], |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
$symbols = $this->locate([$roots]); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertInternalType('array', $symbols); |
|
65
|
|
|
$this->assertCount(2, $symbols); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertContains('myFunctionA', $symbols); |
|
68
|
|
|
$this->assertContains('myFunctionB', $symbols); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testBasicLocateTrait() |
|
72
|
|
|
{ |
|
73
|
|
|
$roots = [ |
|
74
|
|
|
[new Trait_('MyTraitA'), new Trait_('MyTraitB')], |
|
75
|
|
|
[new Trait_('MyTraitC')], |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
$symbols = $this->locate([$roots]); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertInternalType('array', $symbols); |
|
81
|
|
|
$this->assertCount(3, $symbols); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertContains('MyTraitA', $symbols); |
|
84
|
|
|
$this->assertContains('MyTraitB', $symbols); |
|
85
|
|
|
$this->assertContains('MyTraitC', $symbols); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testBasicLocateAnonymous() |
|
89
|
|
|
{ |
|
90
|
|
|
$roots = [ |
|
91
|
|
|
[new Class_(null)], |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
$symbols = $this->locate([$roots]); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertInternalType('array', $symbols); |
|
97
|
|
|
$this->assertCount(0, $symbols); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testBasicLocateDefineCalls() |
|
101
|
|
|
{ |
|
102
|
|
|
$roots = [[ |
|
103
|
|
|
new FuncCall(new Name('define'), [ |
|
104
|
|
|
new Arg(new String_('CONST_NAME')), |
|
105
|
|
|
new Arg(new String_('CONST_VALUE')), |
|
106
|
|
|
]) |
|
107
|
|
|
]]; |
|
108
|
|
|
|
|
109
|
|
|
$symbols = $this->locate([$roots]); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertInternalType('array', $symbols); |
|
112
|
|
|
$this->assertCount(1, $symbols); |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testBasicDoNotLocateNamespacedDefineCalls() |
|
117
|
|
|
{ |
|
118
|
|
|
$roots = [[ |
|
119
|
|
|
new FuncCall(new Name('define', ['namespacedName' => new Name\FullyQualified('Foo\define')]), [ |
|
120
|
|
|
new Arg(new String_('NO_CONST')), |
|
121
|
|
|
new Arg(new String_('NO_SOMETHING')), |
|
122
|
|
|
]) |
|
123
|
|
|
]]; |
|
124
|
|
|
|
|
125
|
|
|
$symbols = $this->locate([$roots]); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertInternalType('array', $symbols); |
|
128
|
|
|
$this->assertCount(0, $symbols); |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function locate(array $roots): array |
|
133
|
|
|
{ |
|
134
|
|
|
return ($this->locator)(new ArrayObject($roots)); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|