-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathReflectionFileNamespaceTest.php
More file actions
249 lines (202 loc) · 9.26 KB
/
Copy pathReflectionFileNamespaceTest.php
File metadata and controls
249 lines (202 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
declare(strict_types=1);
namespace Go\ParserReflection;
use Go\ParserReflection\Stub\TestNamespaceClassFoo;
use PHPUnit\Framework\TestCase;
class ReflectionFileNamespaceTest extends TestCase
{
public const STUB_FILE = '/Stub/FileWithNamespaces.php';
public const STUB_GLOBAL_FILE = '/Stub/FileWithGlobalNamespace.php';
protected ReflectionFileNamespace $parsedRefFileNamespace;
protected function setUp(): void
{
$fileName = stream_resolve_include_path(__DIR__ . self::STUB_FILE);
$fileNode = ReflectionEngine::parseFile($fileName);
$reflectionFile = new ReflectionFile($fileName, $fileNode);
$parsedFileNamespace = $reflectionFile->getFileNamespace('Go\ParserReflection\Stub');
$this->parsedRefFileNamespace = $parsedFileNamespace;
include_once $fileName;
}
public function testGetClass(): void
{
$refClass = $this->parsedRefFileNamespace->getClass(TestNamespaceClassFoo::class);
$this->assertInstanceOf(\ReflectionClass::class, $refClass);
$this->assertSame(TestNamespaceClassFoo::class, $refClass->name);
$this->expectException(ReflectionException::class);
$this->expectExceptionMessageMatches('/^Could not find the class Unknown in the file/');
$this->parsedRefFileNamespace->getClass('Unknown');
}
public function testGetClasses(): void
{
$refClasses = $this->parsedRefFileNamespace->getClasses();
$this->assertCount(2, $refClasses);
}
public function testGetEnums(): void
{
$fileName = stream_resolve_include_path(__DIR__ . '/Stub/FileWithClasses81.php');
$fileNode = ReflectionEngine::parseFile($fileName);
$reflectionFile = new ReflectionFile($fileName, $fileNode);
include_once $fileName;
$fileNamespace = $reflectionFile->getFileNamespace('Go\ParserReflection\Stub');
$enums = $fileNamespace->getEnums();
// FileWithClasses81.php contains: SimplePhp81EnumWithSuit, BackedPhp81EnumHTTPMethods,
// BackedPhp81EnumHTTPStatusWithMethod, and Php81EnumWithInterface
$this->assertCount(4, $enums);
foreach ($enums as $enum) {
$this->assertInstanceOf(ReflectionEnum::class, $enum);
}
// Verify that getClasses() still returns enums as ReflectionClass instances (backward compat)
$classes = $fileNamespace->getClasses();
$enumNames = array_keys($enums);
foreach ($enumNames as $enumName) {
$this->assertArrayHasKey($enumName, $classes, "getClasses() should still include enum {$enumName} for backward compatibility");
$this->assertInstanceOf(ReflectionClass::class, $classes[$enumName]);
}
}
public function testGetConstant(): void
{
$constValue = $this->parsedRefFileNamespace->getConstant('NAMESPACE_NAME');
$this->assertNotFalse($constValue);
$this->assertSame(\Go\ParserReflection\Stub\NAMESPACE_NAME, $constValue);
$this->expectException(ReflectionException::class);
$this->expectExceptionMessageMatches('/^Could not find the constant Unknown in the file/');
$this->parsedRefFileNamespace->getConstant('Unknown');
}
public function testGetConstants(): void
{
$constValue = $this->parsedRefFileNamespace->getConstants(true);
$this->assertEquals(
array(
'START_MARKER' => 9,
'NAMESPACE_NAME' => 'Go\ParserReflection\Stub',
'FILE_NAME' => __DIR__ . self::STUB_FILE,
'END_MARKER' => 26,
'INT_CONST' => 5,
),
$constValue
);
$constValue = $this->parsedRefFileNamespace->getConstants(false);
$this->assertNotFalse($constValue);
$this->assertEquals(
array(
'START_MARKER' => 9,
'NAMESPACE_NAME' => 'Go\ParserReflection\Stub',
'FILE_NAME' => __DIR__ . self::STUB_FILE,
'END_MARKER' => 26,
),
$constValue
);
}
public function testGetConstantsCacheIndependence(): void
{
$globalConstants = $this->parsedRefFileNamespace->getConstants(true);
$this->assertArrayHasKey('FILE_NAME', $globalConstants, 'Namespaced constant found.');
$this->assertArrayHasKey('INT_CONST', $globalConstants, 'Global constant found.');
$constants = $this->parsedRefFileNamespace->getConstants();
$this->assertArrayHasKey('FILE_NAME', $constants, 'Namespaced constant found.');
$this->assertArrayNotHasKey('INT_CONST', $constants, 'Global constant not found.');
$this->assertNotEmpty($this->parsedRefFileNamespace->getConstant('FILE_NAME'), 'Namespaced constant found.');
$this->assertTrue($this->parsedRefFileNamespace->hasConstant('FILE_NAME'), 'Namespaced constant found.');
$this->assertFalse($this->parsedRefFileNamespace->hasConstant('INT_CONST'), 'Global constant not found.');
$this->expectException(ReflectionException::class);
$this->expectExceptionMessageMatches('/^Could not find the constant INT_CONST in the file/');
$this->parsedRefFileNamespace->getConstant('INT_CONST');
}
public function testGetGlobalConstants(): void
{
$fileName = stream_resolve_include_path(__DIR__ . self::STUB_GLOBAL_FILE);
$reflectionFile = new ReflectionFile($fileName);
$reflectionFileNamespace = $reflectionFile->getFileNamespace('');
$this->assertSame(
[
// Scalar types are handled.
'INT_CONST' => 5,
'STRING_CONST' => 'text',
'BOOLEAN_CONST' => true,
// Expressions are handled partially.
'EXPRESSION_CONST' => false,
'FUNCTION_CONST' => mktime(hour: 12, minute: 33, second: 00),
'AAAAAAAAAA' => true
],
$reflectionFileNamespace->getConstants(true)
);
}
public function testGetDocComment(): void
{
$docComment = $this->parsedRefFileNamespace->getDocComment();
$this->assertNotEmpty($docComment);
}
public function testGetEndLine(): void
{
$endLine = $this->parsedRefFileNamespace->getEndLine();
$this->assertSame(\Go\ParserReflection\Stub\END_MARKER + 1, $endLine);
}
public function testGetFileName(): void
{
$fileName = $this->parsedRefFileNamespace->getFileName();
$this->assertEquals(\Go\ParserReflection\Stub\FILE_NAME, $fileName);
}
public function testGetFunction(): void
{
$refFunction = $this->parsedRefFileNamespace->getFunction('testFunctionBar');
$this->assertInstanceOf(\ReflectionFunction::class, $refFunction);
$this->assertSame('testFunctionBar', $refFunction->name);
$this->expectException(ReflectionException::class);
$this->expectExceptionMessageMatches('/^Could not find the function Unknown in the file/');
$this->parsedRefFileNamespace->getFunction('Unknown');
}
public function testGetFunctions(): void
{
$refFunctions = $this->parsedRefFileNamespace->getFunctions();
$this->assertCount(1, $refFunctions);
}
public function testGetName(): void
{
$namespaceName = $this->parsedRefFileNamespace->getName();
$this->assertSame(\Go\ParserReflection\Stub\NAMESPACE_NAME, $namespaceName);
}
public function testGetNameOfGlobalNamespace(): void
{
$fileName = stream_resolve_include_path(__DIR__ . self::STUB_GLOBAL_FILE);
$reflectionFile = new ReflectionFile($fileName);
$reflectionFileNamespace = $reflectionFile->getFileNamespace('');
$this->assertSame('', $reflectionFileNamespace->getName());
}
public function testGetNamespaceAliases(): void
{
$expectedAliases = [
'SomeClass\WithoutAlias' => 'WithoutAlias',
'ReflectionClass' => 'UnusedReflectionClass',
'PhpParser\Node' => 'UnusedNode',
'PhpParser\Node\Expr' => 'UnusedNodeExpr'
];
$realAliases = $this->parsedRefFileNamespace->getNamespaceAliases();
$this->assertSame($expectedAliases, $realAliases);
}
public function testGetStartLine(): void
{
$startLine = $this->parsedRefFileNamespace->getStartLine();
$this->assertSame(\Go\ParserReflection\Stub\START_MARKER - 2, $startLine);
}
public function testHasClass(): void
{
$hasClass = $this->parsedRefFileNamespace->hasClass('Unknown');
$this->assertFalse($hasClass);
$hasClass = $this->parsedRefFileNamespace->hasClass(TestNamespaceClassFoo::class);
$this->assertTrue($hasClass);
}
public function testHasConstant(): void
{
$hasConstant = $this->parsedRefFileNamespace->hasConstant('Unknown');
$this->assertFalse($hasConstant);
$hasConstant = $this->parsedRefFileNamespace->hasConstant('NAMESPACE_NAME');
$this->assertTrue($hasConstant);
}
public function testHasFunction(): void
{
$hasFunction = $this->parsedRefFileNamespace->hasFunction('Unknown');
$this->assertFalse($hasFunction);
$hasFunction = $this->parsedRefFileNamespace->hasFunction('testFunctionBar');
$this->assertTrue($hasFunction);
}
}