Skip to content

Commit 199466f

Browse files
committed
Improve ClassReflectionPropertyHooksTest for PHPUnit 11
1 parent 7389a6d commit 199466f

File tree

1 file changed

+84
-50
lines changed

1 file changed

+84
-50
lines changed

tests/PHPStan/Reflection/ClassReflectionPropertyHooksTest.php

Lines changed: 84 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static function dataPropertyHooks(): iterable
1919
$reflectionProvider = self::createReflectionProvider();
2020

2121
yield [
22-
$reflectionProvider->getClass('PropertyHooksTypes\\Foo'),
22+
'PropertyHooksTypes\\Foo',
2323
'i',
2424
'set',
2525
['int'],
@@ -28,7 +28,7 @@ public static function dataPropertyHooks(): iterable
2828
];
2929

3030
yield [
31-
$reflectionProvider->getClass('PropertyHooksTypes\\Foo'),
31+
'PropertyHooksTypes\\Foo',
3232
'i',
3333
'get',
3434
[],
@@ -37,7 +37,7 @@ public static function dataPropertyHooks(): iterable
3737
];
3838

3939
yield [
40-
$reflectionProvider->getClass('PropertyHooksTypes\\Foo'),
40+
'PropertyHooksTypes\\Foo',
4141
'l',
4242
'get',
4343
[],
@@ -46,7 +46,7 @@ public static function dataPropertyHooks(): iterable
4646
];
4747

4848
yield [
49-
$reflectionProvider->getClass('PropertyHooksTypes\\Foo'),
49+
'PropertyHooksTypes\\Foo',
5050
'n',
5151
'set',
5252
['array<string>|int'],
@@ -55,7 +55,7 @@ public static function dataPropertyHooks(): iterable
5555
];
5656

5757
yield [
58-
$reflectionProvider->getClass('PropertyHooksTypes\\FooShort'),
58+
'PropertyHooksTypes\\FooShort',
5959
'i',
6060
'set',
6161
['int'],
@@ -64,7 +64,7 @@ public static function dataPropertyHooks(): iterable
6464
];
6565

6666
yield [
67-
$reflectionProvider->getClass('PropertyHooksTypes\\FooShort'),
67+
'PropertyHooksTypes\\FooShort',
6868
'k',
6969
'set',
7070
['int|string'],
@@ -73,7 +73,7 @@ public static function dataPropertyHooks(): iterable
7373
];
7474

7575
yield [
76-
$reflectionProvider->getClass('PropertyHooksTypes\\FooShort'),
76+
'PropertyHooksTypes\\FooShort',
7777
'l',
7878
'set',
7979
['array<string>'],
@@ -82,7 +82,7 @@ public static function dataPropertyHooks(): iterable
8282
];
8383

8484
yield [
85-
$reflectionProvider->getClass('PropertyHooksTypes\\FooShort'),
85+
'PropertyHooksTypes\\FooShort',
8686
'm',
8787
'set',
8888
['array<string>'],
@@ -91,7 +91,7 @@ public static function dataPropertyHooks(): iterable
9191
];
9292

9393
yield [
94-
$reflectionProvider->getClass('PropertyHooksTypes\\FooShort'),
94+
'PropertyHooksTypes\\FooShort',
9595
'n',
9696
'set',
9797
['array<string>|int'],
@@ -100,7 +100,7 @@ public static function dataPropertyHooks(): iterable
100100
];
101101

102102
yield [
103-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'),
103+
'PropertyHooksTypes\\FooConstructor',
104104
'i',
105105
'set',
106106
['int'],
@@ -109,7 +109,7 @@ public static function dataPropertyHooks(): iterable
109109
];
110110

111111
yield [
112-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'),
112+
'PropertyHooksTypes\\FooConstructor',
113113
'j',
114114
'set',
115115
['int'],
@@ -118,7 +118,7 @@ public static function dataPropertyHooks(): iterable
118118
];
119119

120120
yield [
121-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'),
121+
'PropertyHooksTypes\\FooConstructor',
122122
'k',
123123
'set',
124124
['int|string'],
@@ -127,7 +127,7 @@ public static function dataPropertyHooks(): iterable
127127
];
128128

129129
yield [
130-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'),
130+
'PropertyHooksTypes\\FooConstructor',
131131
'l',
132132
'set',
133133
['array<string>'],
@@ -136,7 +136,7 @@ public static function dataPropertyHooks(): iterable
136136
];
137137

138138
yield [
139-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'),
139+
'PropertyHooksTypes\\FooConstructor',
140140
'l',
141141
'get',
142142
[],
@@ -145,7 +145,7 @@ public static function dataPropertyHooks(): iterable
145145
];
146146

147147
yield [
148-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'),
148+
'PropertyHooksTypes\\FooConstructor',
149149
'm',
150150
'set',
151151
['array<string>'],
@@ -154,7 +154,7 @@ public static function dataPropertyHooks(): iterable
154154
];
155155

156156
yield [
157-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'),
157+
'PropertyHooksTypes\\FooConstructor',
158158
'n',
159159
'set',
160160
['array<string>|int'],
@@ -163,7 +163,7 @@ public static function dataPropertyHooks(): iterable
163163
];
164164

165165
yield [
166-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructorWithParam'),
166+
'PropertyHooksTypes\\FooConstructorWithParam',
167167
'l',
168168
'set',
169169
['array<string>'],
@@ -172,7 +172,7 @@ public static function dataPropertyHooks(): iterable
172172
];
173173

174174
yield [
175-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructorWithParam'),
175+
'PropertyHooksTypes\\FooConstructorWithParam',
176176
'l',
177177
'get',
178178
[],
@@ -181,7 +181,7 @@ public static function dataPropertyHooks(): iterable
181181
];
182182

183183
yield [
184-
$reflectionProvider->getClass('PropertyHooksTypes\\FooConstructorWithParam'),
184+
'PropertyHooksTypes\\FooConstructorWithParam',
185185
'm',
186186
'set',
187187
['array<string>'],
@@ -190,7 +190,7 @@ public static function dataPropertyHooks(): iterable
190190
];
191191

192192
yield [
193-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'),
193+
'PropertyHooksTypes\\FooGenerics',
194194
'm',
195195
'set',
196196
['array<T of stdClass (class PropertyHooksTypes\FooGenerics, parameter)>'],
@@ -199,7 +199,7 @@ public static function dataPropertyHooks(): iterable
199199
];
200200

201201
yield [
202-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'),
202+
'PropertyHooksTypes\\FooGenerics',
203203
'n',
204204
'set',
205205
['array<T of stdClass (class PropertyHooksTypes\FooGenerics, parameter)>|int'],
@@ -208,7 +208,7 @@ public static function dataPropertyHooks(): iterable
208208
];
209209

210210
yield [
211-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'),
211+
'PropertyHooksTypes\\FooGenerics',
212212
'm',
213213
'get',
214214
[],
@@ -217,80 +217,112 @@ public static function dataPropertyHooks(): iterable
217217
];
218218

219219
yield [
220-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'),
220+
'PropertyHooksTypes\\FooGenerics',
221221
'n',
222222
'get',
223223
[],
224224
'int',
225225
true,
226226
];
227227

228-
$specificFooGenerics = (new GenericObjectType('PropertyHooksTypes\\FooGenerics', [new IntegerType()]))->getClassReflection();
229-
230228
yield [
231-
$specificFooGenerics,
229+
'PropertyHooksTypes\\FooGenerics',
232230
'n',
233-
'set',
234-
['array<int>|int'],
235-
'void',
231+
'get',
232+
[],
233+
'int',
236234
true,
237235
];
238236

239237
yield [
240-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'),
241-
'n',
238+
'PropertyHooksTypes\\FooGenerics',
239+
'm',
242240
'get',
243241
[],
244-
'int',
242+
'array<T of stdClass (class PropertyHooksTypes\FooGenerics, parameter)>',
245243
true,
246244
];
247245

248246
yield [
249-
$specificFooGenerics,
250-
'm',
247+
'PropertyHooksTypes\\FooGenericsConstructor',
248+
'l',
251249
'set',
252-
['array<int>'],
250+
['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>'],
253251
'void',
254252
true,
255253
];
256254

257255
yield [
258-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'),
256+
'PropertyHooksTypes\\FooGenericsConstructor',
259257
'm',
260-
'get',
261-
[],
262-
'array<T of stdClass (class PropertyHooksTypes\FooGenerics, parameter)>',
258+
'set',
259+
['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>'],
260+
'void',
263261
true,
264262
];
265263

266264
yield [
267-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenericsConstructor'),
268-
'l',
265+
'PropertyHooksTypes\\FooGenericsConstructor',
266+
'n',
269267
'set',
270-
['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>'],
268+
['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>|int'],
271269
'void',
272270
true,
273271
];
272+
}
273+
274+
/**
275+
* @param ExtendedPropertyReflection::HOOK_* $hookName
276+
* @param string[] $parameterTypes
277+
*/
278+
#[DataProvider('dataPropertyHooks')]
279+
public function testPropertyHooks(
280+
string $className,
281+
string $propertyName,
282+
string $hookName,
283+
array $parameterTypes,
284+
string $returnType,
285+
bool $isVirtual,
286+
): void
287+
{
288+
$reflectionProvider = $this->createReflectionProvider();
289+
$classReflection = $reflectionProvider->getClass($className);
290+
$propertyReflection = $classReflection->getNativeProperty($propertyName);
291+
$this->assertSame($isVirtual, $propertyReflection->isVirtual()->yes());
292+
293+
$hookReflection = $propertyReflection->getHook($hookName);
294+
$hookVariant = $hookReflection->getOnlyVariant();
295+
$this->assertSame($returnType, $hookVariant->getReturnType()->describe(VerbosityLevel::precise()));
296+
$this->assertCount(count($parameterTypes), $hookVariant->getParameters());
297+
298+
foreach ($hookVariant->getParameters() as $i => $parameter) {
299+
$this->assertSame($parameterTypes[$i], $parameter->getType()->describe(VerbosityLevel::precise()));
300+
}
301+
}
302+
303+
public static function dataPropertyHooksFromType(): iterable
304+
{
305+
$specificFooGenerics = new GenericObjectType('PropertyHooksTypes\\FooGenerics', [new IntegerType()]);
274306

275307
yield [
276-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenericsConstructor'),
308+
$specificFooGenerics,
277309
'm',
278310
'set',
279-
['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>'],
311+
['array<int>'],
280312
'void',
281313
true,
282314
];
283315

284316
yield [
285-
$reflectionProvider->getClass('PropertyHooksTypes\\FooGenericsConstructor'),
317+
$specificFooGenerics,
286318
'n',
287319
'set',
288-
['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>|int'],
320+
['array<int>|int'],
289321
'void',
290322
true,
291323
];
292324

293-
$specificFooGenericsConstructor = (new GenericObjectType('PropertyHooksTypes\\FooGenericsConstructor', [new IntegerType()]))->getClassReflection();
325+
$specificFooGenericsConstructor = new GenericObjectType('PropertyHooksTypes\\FooGenericsConstructor', [new IntegerType()]);
294326

295327
yield [
296328
$specificFooGenericsConstructor,
@@ -324,16 +356,18 @@ public static function dataPropertyHooks(): iterable
324356
* @param ExtendedPropertyReflection::HOOK_* $hookName
325357
* @param string[] $parameterTypes
326358
*/
327-
#[DataProvider('dataPropertyHooks')]
328-
public function testPropertyHooks(
329-
ClassReflection $classReflection,
359+
#[DataProvider('dataPropertyHooksFromType')]
360+
public function testPropertyHooksFromType(
361+
GenericObjectType $type,
330362
string $propertyName,
331363
string $hookName,
332364
array $parameterTypes,
333365
string $returnType,
334366
bool $isVirtual,
335367
): void
336368
{
369+
$classReflection = $type->getClassReflection();
370+
$this->assertNotNull($classReflection);
337371
$propertyReflection = $classReflection->getNativeProperty($propertyName);
338372
$this->assertSame($isVirtual, $propertyReflection->isVirtual()->yes());
339373

0 commit comments

Comments
 (0)