-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathReflectionPropertyTest.php
More file actions
190 lines (170 loc) · 7.57 KB
/
ReflectionPropertyTest.php
File metadata and controls
190 lines (170 loc) · 7.57 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
<?php
declare(strict_types=1);
namespace Go\ParserReflection;
use Go\ParserReflection\Stub\SimplePhp50ClassWithProperties;
use PHPUnit\Framework\Attributes\DataProvider;
class ReflectionPropertyTest extends AbstractTestCase
{
/**
* Class to test
*/
protected static string $reflectionClassToTest = \ReflectionProperty::class;
/**
* Class to load
*/
protected static string $defaultClassToLoad = SimplePhp50ClassWithProperties::class;
/**
* Performs method-by-method comparison with original reflection
*
* @param ReflectionClass $parsedClass Parsed class
* @param \ReflectionProperty $refProperty Property to analyze
* @param string $getterName Name of the reflection method to test
*/
#[DataProvider('reflectionGetterDataProvider')]
public function testReflectionGetterParity(
ReflectionClass $parsedClass,
\ReflectionProperty $refProperty,
string $getterName
): void
{
$propertyName = $refProperty->getName();
$className = $parsedClass->getName();
$parsedProperty = $parsedClass->getProperty($propertyName);
$expectedValue = $refProperty->$getterName();
$actualValue = $parsedProperty->$getterName();
// I would like to completely stop maintaining the __toString method
if ($expectedValue !== $actualValue && $getterName === '__toString') {
$this->markTestSkipped("__toString for property {$className}->{$propertyName} is not equal:\n{$expectedValue}{$actualValue}");
}
$this->assertSame(
$expectedValue,
$actualValue,
"{$getterName}() for property {$className}->{$propertyName} should be equal"
);
}
/**
* Provides full test-case list in the form [ReflectionClass, \ReflectionProperty, getter name to check]
*/
public static function reflectionGetterDataProvider(): \Generator
{
$allNameGetters = self::getGettersToCheck();
foreach (self::propertiesDataProvider() as $prefix => [$parsedClass, $classProperty]) {
foreach ($allNameGetters as $getterName) {
// We could check isInitialized only for static properties
if ($getterName === 'isInitialized' && !$classProperty->isStatic()) {
continue;
}
yield $prefix . ', ' . $getterName => [
$parsedClass,
$classProperty,
$getterName
];
}
}
}
#[DataProvider('propertiesDataProvider')]
public function testGetDefaultValue(ReflectionClass $parsedRefClass, \ReflectionProperty $originalRefProperty): void
{
$propertyName = $originalRefProperty->getName();
$parsedProperty = $parsedRefClass->getProperty($propertyName);
$className = $parsedRefClass->getName();
$this->assertSame(
$originalRefProperty->hasDefaultValue(),
$parsedProperty->hasDefaultValue(),
"Presence of default value for property {$className}:{$propertyName} should be equal"
);
if ($originalRefProperty->isStatic()) {
$actualValue = $parsedProperty->getValue();
$this->assertSame($originalRefProperty->getValue(), $actualValue);
} elseif ($originalRefProperty->hasDefaultValue() && $parsedRefClass->isInstantiable()) {
$instance = $parsedRefClass->newInstanceWithoutConstructor();
$actualValue = $parsedProperty->getValue(); // Let's try not to fallback to internal reflection, pure AST
$this->assertSame($originalRefProperty->getValue($instance), $actualValue);
}
}
public function testGetSetValueForObjectMethods(): void
{
$parsedProperty = $this->parsedRefClass->getProperty('protectedProperty');
$className = $this->parsedRefClass->getName();
$obj = new $className;
$value = $parsedProperty->getValue($obj);
$this->assertSame('a', $value);
$parsedProperty->setValue($obj, 43);
$value = $parsedProperty->getValue($obj);
$this->assertSame(43, $value);
}
public function testCompatibilityWithOriginalConstructor(): void
{
$parsedRefProperty = new ReflectionProperty($this->parsedRefClass->getName(), 'publicStaticProperty');
$originalValue = $parsedRefProperty->getValue();
$this->assertSame(M_PI, $originalValue);
}
#[DataProvider('propertiesDataProvider')]
public function testDebugInfoMethod(ReflectionClass $parsedRefClass, \ReflectionProperty $originalRefProperty): void
{
$propertyName = $originalRefProperty->getName();
$className = $parsedRefClass->getName();
$parsedRefProperty = $parsedRefClass->getProperty($propertyName);
if (!$parsedRefProperty instanceof ReflectionProperty) {
$this->markTestSkipped("Native reflection property {$className}->{$propertyName} represented similarly");
}
$expectedValue = (array) $originalRefProperty;
$this->assertSame($expectedValue, $parsedRefProperty->__debugInfo());
}
#[DataProvider('propertiesDataProvider')]
public function testGetTypeMethod(ReflectionClass $parsedRefClass, \ReflectionProperty $originalRefProperty): void
{
$propertyName = $originalRefProperty->getName();
$className = $parsedRefClass->getName();
$parsedRefProperty = $parsedRefClass->getProperty($propertyName);
$hasType = $parsedRefProperty->hasType();
$this->assertSame(
$originalRefProperty->hasType(),
$hasType,
"Presence of type for property {$className}:{$propertyName} should be equal"
);
$message= "Type information for {$className}::$propertyName not equals to the original reflection";
if ($hasType) {
$parsedType = $parsedRefProperty->getType();
$originalType = $originalRefProperty->getType();
$this->assertSame($originalType->allowsNull(), $parsedType->allowsNull(), $message);
$this->assertSame($originalType->__toString(), $parsedType->__toString(), $message);
} else {
$this->assertSame(
$originalRefProperty->getType(),
$parsedRefProperty->getType(),
$message
);
}
}
/**
* Provides full test-case list in the form [ParsedClass, \ReflectionProperty to check]
*/
public static function propertiesDataProvider(): \Generator
{
foreach (self::classesDataProvider() as $prefix => [$parsedClass, $refClass]) {
foreach ($refClass->getProperties() as $classProperty) {
$fullPropertyName = $parsedClass->getName() . '->' . $classProperty->getName();
yield $prefix . ' ' . $fullPropertyName => [
$parsedClass,
$classProperty,
];
}
}
}
/**
* Returns list of ReflectionMethod getters that be checked directly without additional arguments
*/
protected static function getGettersToCheck(): array
{
$getters = [
'isDefault', 'getName', 'getModifiers', 'getDocComment',
'isPrivate', 'isProtected', 'isPublic', 'isStatic', 'isReadOnly', 'isInitialized',
'hasType', 'hasDefaultValue', 'getDefaultValue', '__toString'
];
if (PHP_VERSION_ID >= 80400) {
array_push($getters, 'isAbstract', 'isProtectedSet', 'isPrivateSet', 'isFinal');
}
return $getters;
}
}