-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathReflectionClassTest.php
More file actions
351 lines (311 loc) · 15.1 KB
/
ReflectionClassTest.php
File metadata and controls
351 lines (311 loc) · 15.1 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
declare(strict_types=1);
namespace Go\ParserReflection;
use Go\ParserReflection\Stub\ClassWithPhp50ConstantsAndInheritance;
use Go\ParserReflection\Stub\ClassWithPhp50MagicConstants;
use Go\ParserReflection\Stub\ClassWithPhp84PropertyHooks;
use Go\ParserReflection\Stub\SimplePhp50ClassWithMethodsAndProperties;
use Go\ParserReflection\Stub\ClassWithPhp50ScalarConstants;
use Go\ParserReflection\Stub\ClassWithPhp50FinalKeyword;
use Go\ParserReflection\Stub\ClassWithPhp50ImplicitAbstractKeyword;
use Go\ParserReflection\Stub\SimplePhp50AbstractClassInheritance;
use PHPUnit\Framework\Attributes\DataProvider;
class ReflectionClassTest extends AbstractTestCase
{
/**
* Name of the class to compare
*/
protected static string $reflectionClassToTest = \ReflectionClass::class;
/**
* Tests getModifier() method
* NB: value is masked because there are many internal constants that aren't exported in the userland
*
*/
#[DataProvider('classesDataProvider')]
public function testGetModifiers(
ReflectionClass $parsedRefClass,
\ReflectionClass $originalRefClass
): void
{
$mask =
\ReflectionClass::IS_EXPLICIT_ABSTRACT
+ \ReflectionClass::IS_FINAL
+ \ReflectionClass::IS_READONLY;
$parsedModifiers = $parsedRefClass->getModifiers() & $mask;
$originalModifiers = $originalRefClass->getModifiers() & $mask;
$this->assertSame(
$originalModifiers,
$parsedModifiers,
"Modifiers for the {$parsedRefClass->name} should be equal"
);
}
/**
* Performs method-by-method comparison with original reflection
*/
#[DataProvider('reflectionGetterDataProvider')]
public function testReflectionGetterParity(
ReflectionClass $parsedClass,
\ReflectionClass $refClass,
string $getterName
): void {
$className = $parsedClass->getName();
$expectedValue = $refClass->$getterName();
$actualValue = $parsedClass->$getterName();
// I would like to completely stop maintaining the __toString method
if ($expectedValue !== $actualValue && $getterName === '__toString') {
$this->markTestSkipped("__toString for class {$className} is not equal:\n{$expectedValue}{$actualValue}");
}
// For Enum it isn't possible to statically resolve constants as well
if ($parsedClass->isEnum() && $getterName === 'getConstants') {
$this->markTestSkipped(
"getConstants for enum {$className} couldn't be resolved fully from AST.\n" .
"See https://github.com/goaop/parser-reflection/issues/132"
);
}
if ($parsedClass->getName() === ClassWithPhp84PropertyHooks::class && in_array($getterName, ['isIterable', 'isIterateable'], true)) {
$this->markTestSkipped(
"isIterable for class with hooks returns true.\n" .
"See https://github.com/php/php-src/issues/20217"
);
}
$this->assertSame(
$expectedValue,
$actualValue,
"{$getterName}() for class {$className} should be equal"
);
}
/**
* Provides full test-case list in the form [ReflectionClass, \ReflectionClass, getter name to check]
*/
public static function reflectionGetterDataProvider(): \Generator
{
$allNameGetters = self::getGettersToCheck();
foreach (self::classesDataProvider() as $prefix => [$parsedClass, $originalClass]) {
foreach ($allNameGetters as $getterName) {
yield $prefix . ', ' . $getterName => [
$parsedClass,
$originalClass,
$getterName
];
}
}
}
/**
* Tests getMethods() returns correct number of methods for the class
*/
#[DataProvider('classesDataProvider')]
public function testGetMethodCount(
ReflectionClass $parsedRefClass,
\ReflectionClass $originalRefClass
): void {
$parsedMethods = $parsedRefClass->getMethods();
$originalMethods = $originalRefClass->getMethods();
if ($parsedRefClass->getTraitAliases()) {
$this->markTestIncomplete("Adoptation methods for traits are not supported yet");
}
$this->assertCount(count($originalMethods), $parsedMethods);
}
/**
* Tests getReflectionConstants() returns correct number of reflectionConstants for the class
*/
#[DataProvider('classesDataProvider')]
public function testGetReflectionConstantCount(
ReflectionClass $parsedRefClass,
\ReflectionClass $originalRefClass
): void {
$parsedReflectionConstants = $parsedRefClass->getReflectionConstants();
$originalReflectionConstants = $originalRefClass->getReflectionConstants();
$this->assertCount(count($originalReflectionConstants), $parsedReflectionConstants);
}
/**
* Tests getProperties() returns correct number of properties for the class
*/
#[DataProvider('classesDataProvider')]
public function testGetProperties(
ReflectionClass $parsedRefClass,
\ReflectionClass $originalRefClass
): void {
$parsedProperties = $parsedRefClass->getProperties();
$originalProperties = $originalRefClass->getProperties();
$this->assertCount(count($originalProperties), $parsedProperties, "Count of properties for " . $originalRefClass->getName() . " should match");
}
/**
* Tests getInterfaces() returns correct number of interfaces for the class
*/
#[DataProvider('classesDataProvider')]
public function testGetInterfaces(
ReflectionClass $parsedRefClass,
\ReflectionClass $originalRefClass
): void {
$parsedInterfaces = $parsedRefClass->getInterfaces();
$originalInterfaces = $originalRefClass->getInterfaces();
$this->assertCount(count($originalInterfaces), $parsedInterfaces, "Count of interfaces for " . $originalRefClass->getName() . " should match");
}
/**
* Tests getConstructor() returns instance of ReflectionMethod for constructor
*/
#[DataProvider('classesDataProvider')]
public function testGetConstructor(
ReflectionClass $parsedRefClass,
\ReflectionClass $originalRefClass
): void {
$hasConstructor = $originalRefClass->hasMethod('__construct');
$this->assertSame(
$hasConstructor,
$parsedRefClass->hasMethod('__construct')
);
if ($hasConstructor) {
$parsedConstructor = $parsedRefClass->getConstructor();
$originalConstructor = $originalRefClass->getConstructor();
$this->assertSame($originalConstructor->getDeclaringClass()->name, $parsedConstructor->getDeclaringClass()->name);
}
}
/**
* Tests getParentClass() returns instance of ReflectionClass
*/
#[DataProvider('classesDataProvider')]
public function testGetParentClass(
ReflectionClass $parsedRefClass,
\ReflectionClass $originalRefClass
): void {
$originalParentClass = $originalRefClass->getParentClass();
$parsedParentClass = $parsedRefClass->getParentClass();
if (!$originalParentClass) {
$this->assertSame($originalParentClass, $parsedParentClass);
}
if ($originalParentClass) {
$this->assertSame($originalParentClass->getName(), $parsedParentClass->getName());
}
}
public function testNewInstanceMethod(): void
{
$parsedRefClass = $this->parsedRefFileNamespace->getClass(ClassWithPhp50FinalKeyword::class);
$instance = $parsedRefClass->newInstance();
$this->assertInstanceOf(ClassWithPhp50FinalKeyword::class, $instance);
$this->assertSame([], $instance->args);
}
public function testNewInstanceArgsMethod(): void
{
$someValueByRef = 5;
$arguments = [1, &$someValueByRef];
$parsedRefClass = $this->parsedRefFileNamespace->getClass(ClassWithPhp50FinalKeyword::class);
$instance = $parsedRefClass->newInstanceArgs($arguments);
$this->assertInstanceOf(ClassWithPhp50FinalKeyword::class, $instance);
$this->assertSame($arguments, $instance->args);
}
public function testNewInstanceWithoutConstructorMethod(): void
{
$arguments = [1, 2];
$parsedRefClass = $this->parsedRefFileNamespace->getClass(ClassWithPhp50FinalKeyword::class);
$instance = $parsedRefClass->newInstanceWithoutConstructor($arguments);
$this->assertInstanceOf(ClassWithPhp50FinalKeyword::class, $instance);
$this->assertSame([], $instance->args);
}
public function testSetStaticPropertyValueMethod(): void
{
$parsedRefClass1 = $this->parsedRefFileNamespace->getClass(ClassWithPhp50ConstantsAndInheritance::class);
$originalRefClass1 = new \ReflectionClass(ClassWithPhp50ConstantsAndInheritance::class);
$parsedRefClass2 = $this->parsedRefFileNamespace->getClass(ClassWithPhp50MagicConstants::class);
$originalRefClass2 = new \ReflectionClass(ClassWithPhp50MagicConstants::class);
$defaultProp1Value = $originalRefClass1->getStaticPropertyValue('h');
$defaultProp2Value = $originalRefClass2->getStaticPropertyValue('a');
$ex = null;
try {
$this->assertEqualsWithDelta(M_PI, $parsedRefClass1->getStaticPropertyValue('h'), 0.0001, 'Close to expected value of M_PI');
$this->assertEqualsWithDelta(M_PI, $originalRefClass1->getStaticPropertyValue('h'), 0.0001, 'Close to expected value of M_PI');
$this->assertEquals(
realpath(dirname(__DIR__ . parent::DEFAULT_STUB_FILENAME)),
realpath($parsedRefClass2->getStaticPropertyValue('a')),
'Expected value');
$this->assertEquals(
$originalRefClass2->getStaticPropertyValue('a'),
$parsedRefClass2->getStaticPropertyValue('a'),
'Same as native implementation');
$parsedRefClass1->setStaticPropertyValue('h', 'test');
$parsedRefClass2->setStaticPropertyValue('a', 'different value');
$this->assertSame('test', $parsedRefClass1->getStaticPropertyValue('h'));
$this->assertSame('test', $originalRefClass1->getStaticPropertyValue('h'));
$this->assertSame('different value', $parsedRefClass2->getStaticPropertyValue('a'));
$this->assertSame('different value', $originalRefClass2->getStaticPropertyValue('a'));
}
catch (\Exception $e) {
$ex = $e;
}
// I didn't want to write a tearDown() for one test.
$originalRefClass1->setStaticPropertyValue('h', $defaultProp1Value);
$originalRefClass2->setStaticPropertyValue('a', $defaultProp2Value);
if ($ex) {
throw $ex;
}
}
public function testGetMethodsFiltering(): void
{
$parsedRefClass = $this->parsedRefFileNamespace->getClass(SimplePhp50ClassWithMethodsAndProperties::class);
$originalRefClass = new \ReflectionClass(SimplePhp50ClassWithMethodsAndProperties::class);
$parsedMethods = $parsedRefClass->getMethods(\ReflectionMethod::IS_PUBLIC);
$originalMethods = $originalRefClass->getMethods(\ReflectionMethod::IS_PUBLIC);
$this->assertCount(count($originalMethods), $parsedMethods);
$parsedMethods = $parsedRefClass->getMethods(\ReflectionMethod::IS_PRIVATE | \ReflectionMethod::IS_STATIC);
$originalMethods = $originalRefClass->getMethods(\ReflectionMethod::IS_PRIVATE | \ReflectionMethod::IS_STATIC);
$this->assertCount(count($originalMethods), $parsedMethods);
}
public function testDirectMethods(): void
{
$parsedRefClass = $this->parsedRefFileNamespace->getClass(ClassWithPhp50ImplicitAbstractKeyword::class);
$originalRefClass = new \ReflectionClass(ClassWithPhp50ImplicitAbstractKeyword::class);
$this->assertEquals($originalRefClass->hasMethod('test'), $parsedRefClass->hasMethod('test'));
$this->assertCount(count($originalRefClass->getMethods()), $parsedRefClass->getMethods());
$originalMethodName = $originalRefClass->getMethod('test')->getName();
$parsedMethodName = $parsedRefClass->getMethod('test')->getName();
$this->assertSame($originalMethodName, $parsedMethodName);
}
public function testInheritedMethods(): void
{
$this->markTestIncomplete("See https://github.com/goaop/parser-reflection/issues/55");
$parsedRefClass = $this->parsedRefFileNamespace->getClass(SimplePhp50AbstractClassInheritance::class);
$originalRefClass = new \ReflectionClass(SimplePhp50AbstractClassInheritance::class);
$this->assertEquals($originalRefClass->hasMethod('test'), $parsedRefClass->hasMethod('test'));
}
public function testHasConstant(): void
{
$parsedRefClass = $this->parsedRefFileNamespace->getClass(ClassWithPhp50ScalarConstants::class);
$originalRefClass = new \ReflectionClass(ClassWithPhp50ScalarConstants::class);
$this->assertSame($originalRefClass->hasConstant('D'), $parsedRefClass->hasConstant('D'));
$this->assertSame($originalRefClass->hasConstant('E'), $parsedRefClass->hasConstant('E'));
}
public function testGetConstant(): void
{
$parsedRefClass = $this->parsedRefFileNamespace->getClass(ClassWithPhp50ScalarConstants::class);
$originalRefClass = new \ReflectionClass(ClassWithPhp50ScalarConstants::class);
$this->assertSame($originalRefClass->getConstant('D'), $parsedRefClass->getConstant('D'));
$this->assertSame($originalRefClass->getConstant('E'), $parsedRefClass->getConstant('E'));
}
public function testGetReflectionConstant(): void
{
$parsedRefClass = $this->parsedRefFileNamespace->getClass(ClassWithPhp50ScalarConstants::class);
$originalRefClass = new \ReflectionClass(ClassWithPhp50ScalarConstants::class);
$this->assertFalse($parsedRefClass->getReflectionConstant('NOT_EXISTING'));
$this->assertSame(
(string) $originalRefClass->getReflectionConstant('D'),
(string) $parsedRefClass->getReflectionConstant('D')
);
$this->assertSame(
(string) $originalRefClass->getReflectionConstant('E'),
(string) $parsedRefClass->getReflectionConstant('E')
);
}
/**
* Returns list of ReflectionMethod getters that be checked directly without additional arguments
*/
protected static function getGettersToCheck(): array
{
return [
'getFileName', 'getStartLine', 'getEndLine', 'getDocComment', 'getExtension', 'getExtensionName',
'getName', 'getNamespaceName', 'getShortName', 'inNamespace',
'isAbstract', 'isCloneable', 'isFinal', 'isInstantiable', 'isReadOnly',
'isInterface', 'isInternal', 'isIterateable', 'isIterable', 'isTrait', 'isUserDefined',
'getConstants', 'getTraitNames', 'getInterfaceNames', 'getStaticProperties',
'getDefaultProperties', 'getTraitAliases', 'isEnum'
];
}
}