-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathAttributeInstanceOfFilterTest.php
More file actions
221 lines (183 loc) · 8.99 KB
/
Copy pathAttributeInstanceOfFilterTest.php
File metadata and controls
221 lines (183 loc) · 8.99 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
<?php
declare(strict_types=1);
namespace Go\ParserReflection;
use Go\ParserReflection\Locator\CallableLocator;
use Go\ParserReflection\Locator\ComposerLocator;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
use ReflectionAttribute as InternalReflectionAttribute;
class AttributeInstanceOfFilterTest extends TestCase
{
private const STUB_FILE = __DIR__ . '/Stub/FileWithAttributeHierarchy80.php';
private const STUB_NAMESPACE = 'Go\ParserReflection\Stub\AttributeHierarchy';
protected function tearDown(): void
{
ReflectionEngine::init(new ComposerLocator());
}
/**
* The whole hierarchy is resolved from the AST only, so none of the involved classes may be loaded
*/
public function testInstanceOfFilterResolvesHierarchyWithoutLoadingClasses(): void
{
$this->setUpAstOnlyLocator();
$parsedClass = new ReflectionClass(self::STUB_NAMESPACE . '\HookedClass');
$childAttributes = $parsedClass->getAttributes(
self::STUB_NAMESPACE . '\ChildAttribute',
InternalReflectionAttribute::IS_INSTANCEOF
);
$this->assertCount(1, $childAttributes);
$this->assertSame(self::STUB_NAMESPACE . '\ChildAttribute', $childAttributes[0]->getName());
$this->assertSame(['class-level'], $childAttributes[0]->getArguments());
$baseAttributes = $parsedClass->getAttributes(
self::STUB_NAMESPACE . '\BaseAttribute',
InternalReflectionAttribute::IS_INSTANCEOF
);
$this->assertCount(1, $baseAttributes);
$this->assertSame(self::STUB_NAMESPACE . '\ChildAttribute', $baseAttributes[0]->getName());
$this->assertCount(0, $parsedClass->getAttributes(self::STUB_NAMESPACE . '\BaseAttribute'));
$parsedProperty = $parsedClass->getProperty('hookedProperty');
$inheritedAttributes = $parsedProperty->getAttributes(
self::STUB_NAMESPACE . '\BaseAttribute',
InternalReflectionAttribute::IS_INSTANCEOF
);
$this->assertCount(1, $inheritedAttributes);
$this->assertSame(self::STUB_NAMESPACE . '\GrandChildAttribute', $inheritedAttributes[0]->getName());
$parsedMethod = $parsedClass->getMethod('hookedMethod');
$this->assertCount(1, $parsedMethod->getAttributes(
self::STUB_NAMESPACE . '\MarkerInterface',
InternalReflectionAttribute::IS_INSTANCEOF
));
$this->assertCount(1, $parsedMethod->getAttributes(
self::STUB_NAMESPACE . '\ExtendedMarkerInterface',
InternalReflectionAttribute::IS_INSTANCEOF
));
$this->assertCount(0, $parsedMethod->getAttributes(
self::STUB_NAMESPACE . '\BaseAttribute',
InternalReflectionAttribute::IS_INSTANCEOF
));
$this->assertStubHierarchyIsNotLoaded();
}
public function testInstanceOfFilterIgnoresUnknownAttributeClasses(): void
{
$parsedFile = new ReflectionFile(self::STUB_FILE);
$parsedClass = $parsedFile
->getFileNamespace(self::STUB_NAMESPACE)
->getClass(self::STUB_NAMESPACE . '\HookedClass');
ReflectionEngine::init(new CallableLocator(fn(string $className): false|string => false));
$this->assertCount(0, $parsedClass->getAttributes(
self::STUB_NAMESPACE . '\BaseAttribute',
InternalReflectionAttribute::IS_INSTANCEOF
));
$this->assertCount(2, $parsedClass->getAttributes());
$this->assertStubHierarchyIsNotLoaded();
}
public function testInvalidFilterFlagThrowsValueError(): void
{
$this->setUpAstOnlyLocator();
$parsedClass = new ReflectionClass(self::STUB_NAMESPACE . '\HookedClass');
$this->expectException(\ValueError::class);
$this->expectExceptionMessage(
'ReflectionClass::getAttributes(): Argument #2 ($flags) must be a valid attribute filter flag'
);
$parsedClass->getAttributes(self::STUB_NAMESPACE . '\BaseAttribute', 3);
}
public function testInvalidFilterFlagThrowsValueErrorForFunctionLike(): void
{
$this->setUpAstOnlyLocator();
$parsedMethod = (new ReflectionClass(self::STUB_NAMESPACE . '\HookedClass'))->getMethod('hookedMethod');
$this->expectException(\ValueError::class);
$this->expectExceptionMessage(
'ReflectionFunctionAbstract::getAttributes(): Argument #2 ($flags) must be a valid attribute filter flag'
);
$parsedMethod->getAttributes(self::STUB_NAMESPACE . '\BaseAttribute', 3);
}
/**
* Runs isolated to keep the stub hierarchy unloaded for the AST-only test cases
*/
#[RunInSeparateProcess]
#[PreserveGlobalState(false)]
public function testInstanceOfFilterMatchesNativeReflectionForLoadedClasses(): void
{
require_once self::STUB_FILE;
$className = self::STUB_NAMESPACE . '\HookedClass';
$parsedClass = new ReflectionClass($className);
$nativeClass = new \ReflectionClass($className);
$filterNames = [
self::STUB_NAMESPACE . '\BaseAttribute',
self::STUB_NAMESPACE . '\ChildAttribute',
self::STUB_NAMESPACE . '\GrandChildAttribute',
self::STUB_NAMESPACE . '\MarkerInterface',
self::STUB_NAMESPACE . '\ExtendedMarkerInterface',
self::STUB_NAMESPACE . '\UnrelatedAttribute',
];
foreach ($filterNames as $filterName) {
$this->assertSame(
$this->describeAttributes($nativeClass->getAttributes($filterName, InternalReflectionAttribute::IS_INSTANCEOF)),
$this->describeAttributes($parsedClass->getAttributes($filterName, InternalReflectionAttribute::IS_INSTANCEOF)),
"Class attributes filtered by $filterName"
);
$this->assertSame(
$this->describeAttributes($nativeClass->getMethod('hookedMethod')->getAttributes($filterName, InternalReflectionAttribute::IS_INSTANCEOF)),
$this->describeAttributes($parsedClass->getMethod('hookedMethod')->getAttributes($filterName, InternalReflectionAttribute::IS_INSTANCEOF)),
"Method attributes filtered by $filterName"
);
$this->assertSame(
$this->describeAttributes($nativeClass->getProperty('hookedProperty')->getAttributes($filterName, InternalReflectionAttribute::IS_INSTANCEOF)),
$this->describeAttributes($parsedClass->getProperty('hookedProperty')->getAttributes($filterName, InternalReflectionAttribute::IS_INSTANCEOF)),
"Property attributes filtered by $filterName"
);
$this->assertSame(
$this->describeAttributes($nativeClass->getAttributes($filterName)),
$this->describeAttributes($parsedClass->getAttributes($filterName)),
"Class attributes filtered by exact name $filterName"
);
}
$this->assertSame(
$this->describeAttributes($nativeClass->getAttributes(null, InternalReflectionAttribute::IS_INSTANCEOF)),
$this->describeAttributes($parsedClass->getAttributes(null, InternalReflectionAttribute::IS_INSTANCEOF)),
'Unfiltered attributes are not affected by the flag'
);
// Unlike the engine, an unresolvable filter class never triggers autoloading and matches nothing
$this->assertSame([], $parsedClass->getAttributes(
self::STUB_NAMESPACE . '\MissingAttribute',
InternalReflectionAttribute::IS_INSTANCEOF
));
}
/**
* @param \ReflectionAttribute<object>[] $attributes
*
* @return array<int, array{string, array<int|string, mixed>}>
*/
private function describeAttributes(array $attributes): array
{
$description = [];
foreach ($attributes as $attribute) {
$description[] = [$attribute->getName(), $attribute->getArguments()];
}
return $description;
}
private function setUpAstOnlyLocator(): void
{
ReflectionEngine::init(new CallableLocator(
fn(string $className): false|string => str_starts_with($className, self::STUB_NAMESPACE . '\\')
? self::STUB_FILE
: false
));
}
private function assertStubHierarchyIsNotLoaded(): void
{
foreach (['HookedClass', 'BaseAttribute', 'ChildAttribute', 'GrandChildAttribute', 'InterfaceAttribute'] as $shortName) {
$this->assertFalse(
class_exists(self::STUB_NAMESPACE . '\\' . $shortName, false),
"Class $shortName must not be loaded by the AST-based instanceof resolution"
);
}
foreach (['MarkerInterface', 'ExtendedMarkerInterface'] as $shortName) {
$this->assertFalse(
interface_exists(self::STUB_NAMESPACE . '\\' . $shortName, false),
"Interface $shortName must not be loaded by the AST-based instanceof resolution"
);
}
}
}