1 | <?php |
||
57 | class LazyLoadingValueHolderFunctionalTest extends PHPUnit_Framework_TestCase |
||
58 | { |
||
59 | /** |
||
60 | * @dataProvider getProxyMethods |
||
61 | * |
||
62 | * @param string $className |
||
63 | * @param object $instance |
||
64 | * @param string $method |
||
65 | * @param mixed[] $params |
||
66 | * @param mixed $expectedValue |
||
67 | */ |
||
68 | public function testMethodCalls(string $className, $instance, string $method, array $params, $expectedValue) : void |
||
69 | { |
||
70 | $proxyName = $this->generateProxy($className); |
||
71 | |||
72 | /* @var $proxy VirtualProxyInterface */ |
||
73 | $proxy = $proxyName::staticProxyConstructor($this->createInitializer($className, $instance)); |
||
74 | |||
75 | self::assertFalse($proxy->isProxyInitialized()); |
||
76 | |||
77 | /* @var $callProxyMethod callable */ |
||
78 | $callProxyMethod = [$proxy, $method]; |
||
79 | $parameterValues = array_values($params); |
||
80 | |||
81 | self::assertSame($expectedValue, $callProxyMethod(...$parameterValues)); |
||
82 | self::assertTrue($proxy->isProxyInitialized()); |
||
83 | self::assertSame($instance, $proxy->getWrappedValueHolderValue()); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @dataProvider getProxyMethods |
||
88 | * |
||
89 | * @param string $className |
||
90 | * @param object $instance |
||
91 | * @param string $method |
||
92 | * @param mixed[] $params |
||
93 | * @param mixed $expectedValue |
||
94 | */ |
||
95 | public function testMethodCallsAfterUnSerialization( |
||
96 | string $className, |
||
97 | $instance, |
||
98 | string $method, |
||
99 | array $params, |
||
100 | $expectedValue |
||
101 | ) : void { |
||
102 | $proxyName = $this->generateProxy($className); |
||
103 | |||
104 | /* @var $proxy VirtualProxyInterface */ |
||
105 | $proxy = unserialize(serialize($proxyName::staticProxyConstructor( |
||
106 | $this->createInitializer($className, $instance) |
||
107 | ))); |
||
108 | |||
109 | self::assertTrue($proxy->isProxyInitialized()); |
||
110 | |||
111 | /* @var $callProxyMethod callable */ |
||
112 | $callProxyMethod = [$proxy, $method]; |
||
113 | $parameterValues = array_values($params); |
||
114 | |||
115 | self::assertInternalType('callable', $callProxyMethod); |
||
116 | |||
117 | self::assertSame($expectedValue, $callProxyMethod(...$parameterValues)); |
||
118 | self::assertEquals($instance, $proxy->getWrappedValueHolderValue()); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @dataProvider getProxyMethods |
||
123 | * |
||
124 | * @param string $className |
||
125 | * @param object $instance |
||
126 | * @param string $method |
||
127 | * @param mixed[] $params |
||
128 | * @param mixed $expectedValue |
||
129 | */ |
||
130 | public function testMethodCallsAfterCloning( |
||
131 | string $className, |
||
132 | $instance, |
||
133 | string $method, |
||
134 | array $params, |
||
135 | $expectedValue |
||
136 | ) : void { |
||
137 | $proxyName = $this->generateProxy($className); |
||
138 | |||
139 | /* @var $proxy VirtualProxyInterface */ |
||
140 | $proxy = $proxyName::staticProxyConstructor($this->createInitializer($className, $instance)); |
||
141 | $cloned = clone $proxy; |
||
142 | |||
143 | self::assertTrue($cloned->isProxyInitialized()); |
||
144 | self::assertNotSame($proxy->getWrappedValueHolderValue(), $cloned->getWrappedValueHolderValue()); |
||
145 | |||
146 | /* @var $callProxyMethod callable */ |
||
147 | $callProxyMethod = [$cloned, $method]; |
||
148 | $parameterValues = array_values($params); |
||
149 | |||
150 | self::assertInternalType('callable', $callProxyMethod); |
||
151 | |||
152 | self::assertSame($expectedValue, $callProxyMethod(...$parameterValues)); |
||
153 | self::assertEquals($instance, $cloned->getWrappedValueHolderValue()); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @dataProvider getPropertyAccessProxies |
||
158 | * |
||
159 | * @param object $instance |
||
160 | * @param VirtualProxyInterface $proxy |
||
161 | * @param string $publicProperty |
||
162 | * @param mixed $propertyValue |
||
163 | */ |
||
164 | public function testPropertyReadAccess( |
||
165 | $instance, |
||
166 | VirtualProxyInterface $proxy, |
||
167 | string $publicProperty, |
||
168 | $propertyValue |
||
169 | ) : void { |
||
170 | self::assertSame($propertyValue, $proxy->$publicProperty); |
||
171 | self::assertTrue($proxy->isProxyInitialized()); |
||
172 | self::assertEquals($instance, $proxy->getWrappedValueHolderValue()); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @dataProvider getPropertyAccessProxies |
||
177 | * |
||
178 | * @param object $instance |
||
179 | * @param VirtualProxyInterface $proxy |
||
180 | * @param string $publicProperty |
||
181 | */ |
||
182 | public function testPropertyWriteAccess($instance, VirtualProxyInterface $proxy, string $publicProperty) : void |
||
183 | { |
||
184 | $newValue = uniqid(); |
||
185 | $proxy->$publicProperty = $newValue; |
||
186 | |||
187 | self::assertTrue($proxy->isProxyInitialized()); |
||
188 | self::assertSame($newValue, $proxy->$publicProperty); |
||
189 | self::assertSame($newValue, $proxy->getWrappedValueHolderValue()->$publicProperty); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @dataProvider getPropertyAccessProxies |
||
194 | * |
||
195 | * @param object $instance |
||
196 | * @param VirtualProxyInterface $proxy |
||
197 | * @param string $publicProperty |
||
198 | */ |
||
199 | public function testPropertyExistence($instance, VirtualProxyInterface $proxy, string $publicProperty) : void |
||
200 | { |
||
201 | self::assertSame(isset($instance->$publicProperty), isset($proxy->$publicProperty)); |
||
202 | self::assertTrue($proxy->isProxyInitialized()); |
||
203 | self::assertEquals($instance, $proxy->getWrappedValueHolderValue()); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @dataProvider getPropertyAccessProxies |
||
208 | * |
||
209 | * @param object $instance |
||
210 | * @param VirtualProxyInterface $proxy |
||
211 | * @param string $publicProperty |
||
212 | */ |
||
213 | public function testPropertyAbsence($instance, VirtualProxyInterface $proxy, string $publicProperty) : void |
||
214 | { |
||
215 | $instance = $proxy->getWrappedValueHolderValue() ?: $instance; |
||
216 | $instance->$publicProperty = null; |
||
217 | self::assertFalse(isset($proxy->$publicProperty)); |
||
218 | self::assertTrue($proxy->isProxyInitialized()); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @dataProvider getPropertyAccessProxies |
||
223 | * |
||
224 | * @param object $instance |
||
225 | * @param VirtualProxyInterface $proxy |
||
226 | * @param string $publicProperty |
||
227 | */ |
||
228 | public function testPropertyUnset($instance, VirtualProxyInterface $proxy, string $publicProperty) : void |
||
229 | { |
||
230 | $instance = $proxy->getWrappedValueHolderValue() ?: $instance; |
||
231 | unset($proxy->$publicProperty); |
||
232 | |||
233 | self::assertTrue($proxy->isProxyInitialized()); |
||
234 | |||
235 | self::assertFalse(isset($instance->$publicProperty)); |
||
236 | self::assertFalse(isset($proxy->$publicProperty)); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Verifies that accessing a public property containing an array behaves like in a normal context |
||
241 | */ |
||
242 | public function testCanWriteToArrayKeysInPublicProperty() : void |
||
243 | { |
||
244 | $instance = new ClassWithPublicArrayProperty(); |
||
245 | $className = get_class($instance); |
||
246 | $initializer = $this->createInitializer($className, $instance); |
||
247 | $proxyName = $this->generateProxy($className); |
||
248 | /* @var $proxy ClassWithPublicArrayProperty */ |
||
249 | $proxy = $proxyName::staticProxyConstructor($initializer); |
||
250 | |||
251 | $proxy->arrayProperty['foo'] = 'bar'; |
||
252 | |||
253 | self::assertSame('bar', $proxy->arrayProperty['foo']); |
||
254 | |||
255 | $proxy->arrayProperty = ['tab' => 'taz']; |
||
256 | |||
257 | self::assertSame(['tab' => 'taz'], $proxy->arrayProperty); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Verifies that public properties retrieved via `__get` don't get modified in the object itself |
||
262 | */ |
||
263 | public function testWillNotModifyRetrievedPublicProperties() : void |
||
264 | { |
||
265 | $instance = new ClassWithPublicProperties(); |
||
266 | $className = get_class($instance); |
||
267 | $initializer = $this->createInitializer($className, $instance); |
||
268 | $proxyName = $this->generateProxy($className); |
||
269 | /* @var $proxy ClassWithPublicProperties */ |
||
270 | $proxy = $proxyName::staticProxyConstructor($initializer); |
||
271 | $variable = $proxy->property0; |
||
272 | |||
273 | self::assertSame('property0', $variable); |
||
274 | |||
275 | $variable = 'foo'; |
||
276 | |||
277 | self::assertSame('property0', $proxy->property0); |
||
278 | self::assertSame('foo', $variable); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Verifies that public properties references retrieved via `__get` modify in the object state |
||
283 | */ |
||
284 | public function testWillModifyByRefRetrievedPublicProperties() : void |
||
285 | { |
||
286 | $instance = new ClassWithPublicProperties(); |
||
287 | $className = get_class($instance); |
||
288 | $initializer = $this->createInitializer($className, $instance); |
||
289 | $proxyName = $this->generateProxy($className); |
||
290 | /* @var $proxy ClassWithPublicProperties */ |
||
291 | $proxy = $proxyName::staticProxyConstructor($initializer); |
||
292 | $variable = & $proxy->property0; |
||
293 | |||
294 | self::assertSame('property0', $variable); |
||
295 | |||
296 | $variable = 'foo'; |
||
297 | |||
298 | self::assertSame('foo', $proxy->property0); |
||
299 | self::assertSame('foo', $variable); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @group 16 |
||
304 | * |
||
305 | * Verifies that initialization of a value holder proxy may happen multiple times |
||
306 | */ |
||
307 | public function testWillAllowMultipleProxyInitialization() : void |
||
308 | { |
||
309 | $proxyClass = $this->generateProxy(BaseClass::class); |
||
310 | $counter = 0; |
||
311 | |||
312 | /* @var $proxy BaseClass */ |
||
313 | $proxy = $proxyClass::staticProxyConstructor(function (& $wrappedInstance) use (& $counter) { |
||
314 | $wrappedInstance = new BaseClass(); |
||
315 | |||
316 | $wrappedInstance->publicProperty = (string) ($counter += 1); |
||
317 | }); |
||
318 | |||
319 | self::assertSame('1', $proxy->publicProperty); |
||
320 | self::assertSame('2', $proxy->publicProperty); |
||
321 | self::assertSame('3', $proxy->publicProperty); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @group 115 |
||
326 | * @group 175 |
||
327 | */ |
||
328 | public function testWillBehaveLikeObjectWithNormalConstructor() : void |
||
329 | { |
||
330 | $instance = new ClassWithCounterConstructor(10); |
||
331 | |||
332 | self::assertSame(10, $instance->amount, 'Verifying that test asset works as expected'); |
||
333 | self::assertSame(10, $instance->getAmount(), 'Verifying that test asset works as expected'); |
||
334 | $instance->__construct(3); |
||
335 | self::assertSame(13, $instance->amount, 'Verifying that test asset works as expected'); |
||
336 | self::assertSame(13, $instance->getAmount(), 'Verifying that test asset works as expected'); |
||
337 | |||
338 | $proxyName = $this->generateProxy(get_class($instance)); |
||
339 | |||
340 | /* @var $proxy ClassWithCounterConstructor */ |
||
341 | $proxy = new $proxyName(15); |
||
342 | |||
343 | self::assertSame(15, $proxy->amount, 'Verifying that the proxy constructor works as expected'); |
||
344 | self::assertSame(15, $proxy->getAmount(), 'Verifying that the proxy constructor works as expected'); |
||
345 | $proxy->__construct(5); |
||
346 | self::assertSame(20, $proxy->amount, 'Verifying that the proxy constructor works as expected'); |
||
347 | self::assertSame(20, $proxy->getAmount(), 'Verifying that the proxy constructor works as expected'); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @group 265 |
||
352 | */ |
||
353 | public function testWillForwardVariadicByRefArguments() : void |
||
354 | { |
||
355 | $proxyName = $this->generateProxy(ClassWithMethodWithByRefVariadicFunction::class); |
||
356 | /* @var $object ClassWithMethodWithByRefVariadicFunction */ |
||
357 | $object = $proxyName::staticProxyConstructor(function (& $wrappedInstance) { |
||
358 | $wrappedInstance = new ClassWithMethodWithByRefVariadicFunction(); |
||
359 | }); |
||
360 | |||
361 | $parameters = ['a', 'b', 'c']; |
||
362 | |||
363 | // first, testing normal variadic behavior (verifying we didn't screw up in the test asset) |
||
364 | self::assertSame(['a', 'changed', 'c'], (new ClassWithMethodWithByRefVariadicFunction())->tuz(...$parameters)); |
||
365 | self::assertSame(['a', 'changed', 'c'], $object->tuz(...$parameters)); |
||
366 | self::assertSame(['a', 'changed', 'c'], $parameters, 'by-ref variadic parameter was changed'); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * This test documents a known limitation: `func_get_args()` (and similars) don't work in proxied APIs. |
||
371 | * If you manage to make this test pass, then please do send a patch |
||
372 | * |
||
373 | * @group 265 |
||
374 | */ |
||
375 | public function testWillNotForwardDynamicArguments() : void |
||
376 | { |
||
377 | $proxyName = $this->generateProxy(ClassWithDynamicArgumentsMethod::class); |
||
378 | |||
379 | /* @var $object ClassWithDynamicArgumentsMethod */ |
||
380 | $object = $proxyName::staticProxyConstructor(function (& $wrappedInstance) { |
||
381 | $wrappedInstance = new ClassWithDynamicArgumentsMethod(); |
||
382 | }); |
||
383 | |||
384 | self::assertSame(['a', 'b'], (new ClassWithDynamicArgumentsMethod())->dynamicArgumentsMethod('a', 'b')); |
||
385 | |||
386 | $this->expectException(\PHPUnit_Framework_ExpectationFailedException::class); |
||
387 | |||
388 | self::assertSame(['a', 'b'], $object->dynamicArgumentsMethod('a', 'b')); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Generates a proxy for the given class name, and retrieves its class name |
||
393 | */ |
||
394 | private function generateProxy(string $parentClassName) : string |
||
406 | |||
407 | /** |
||
408 | * @param string $className |
||
409 | * @param object $realInstance |
||
410 | * @param Mock $initializerMatcher |
||
411 | * |
||
412 | * @return callable |
||
413 | */ |
||
414 | private function createInitializer(string $className, $realInstance, Mock $initializerMatcher = null) : callable |
||
415 | { |
||
416 | /* @var $initializerMatcher callable|Mock */ |
||
417 | if (! $initializerMatcher) { |
||
418 | $initializerMatcher = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock(); |
||
419 | |||
420 | $initializerMatcher |
||
421 | ->expects(self::once()) |
||
448 | |||
449 | /** |
||
450 | * Generates a list of object | invoked method | parameters | expected result |
||
451 | */ |
||
452 | public function getProxyMethods() : array |
||
551 | |||
552 | /** |
||
553 | * Generates proxies and instances with a public property to feed to the property accessor methods |
||
554 | * |
||
555 | * @return array |
||
556 | */ |
||
557 | public function getPropertyAccessProxies() : array |
||
583 | |||
584 | /** |
||
585 | * @group 276 |
||
586 | * |
||
587 | * @dataProvider getMethodsThatAccessPropertiesOnOtherObjectsInTheSameScope |
||
588 | * |
||
589 | * @param object $callerObject |
||
590 | * @param object $realInstance |
||
591 | * @param string $method |
||
592 | * @param string $expectedValue |
||
593 | */ |
||
594 | public function testWillLazyLoadMembersOfOtherProxiesWithTheSamePrivateScope( |
||
611 | |||
612 | /** |
||
613 | * @group 276 |
||
614 | * |
||
615 | * @dataProvider getMethodsThatAccessPropertiesOnOtherObjectsInTheSameScope |
||
616 | * |
||
617 | * @param object $callerObject |
||
618 | * @param object $realInstance |
||
619 | * @param string $method |
||
620 | * @param string $expectedValue |
||
621 | */ |
||
622 | public function testWillFetchMembersOfOtherDeSerializedProxiesWithTheSamePrivateScope( |
||
640 | |||
641 | /** |
||
642 | * @group 276 |
||
643 | * |
||
644 | * @dataProvider getMethodsThatAccessPropertiesOnOtherObjectsInTheSameScope |
||
645 | * |
||
646 | * @param object $callerObject |
||
647 | * @param object $realInstance |
||
648 | * @param string $method |
||
649 | * @param string $expectedValue |
||
650 | */ |
||
651 | public function testWillFetchMembersOfOtherClonedProxiesWithTheSamePrivateScope( |
||
669 | |||
670 | /** |
||
671 | * @group 327 |
||
672 | */ |
||
673 | public function testWillExecuteLogicInAVoidMethod() : void |
||
685 | |||
686 | public function getMethodsThatAccessPropertiesOnOtherObjectsInTheSameScope() : \Generator |
||
716 | |||
717 | /** |
||
718 | * @param object $instance |
||
719 | * @param array $values |
||
720 | * |
||
721 | * @return object |
||
722 | */ |
||
723 | private function buildInstanceWithValues($instance, array $values) |
||
735 | } |
||
736 |