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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
238 | |||
239 | /** |
||
240 | * Verifies that accessing a public property containing an array behaves like in a normal context |
||
241 | */ |
||
242 | public function testCanWriteToArrayKeysInPublicProperty() : void |
||
259 | |||
260 | /** |
||
261 | * Verifies that public properties retrieved via `__get` don't get modified in the object itself |
||
262 | */ |
||
263 | public function testWillNotModifyRetrievedPublicProperties() : void |
||
280 | |||
281 | /** |
||
282 | * Verifies that public properties references retrieved via `__get` modify in the object state |
||
283 | */ |
||
284 | public function testWillModifyByRefRetrievedPublicProperties() : void |
||
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 |
||
323 | |||
324 | /** |
||
325 | * @group 115 |
||
326 | * @group 175 |
||
327 | */ |
||
328 | public function testWillBehaveLikeObjectWithNormalConstructor() : void |
||
349 | |||
350 | /** |
||
351 | * @group 265 |
||
352 | */ |
||
353 | public function testWillForwardVariadicByRefArguments() : void |
||
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 |
||
390 | |||
391 | /** |
||
392 | * Generates a proxy for the given class name, and retrieves its class name |
||
393 | * |
||
394 | * @param string $parentClassName |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | private function generateProxy(string $parentClassName) : string |
||
410 | |||
411 | /** |
||
412 | * @param string $className |
||
413 | * @param object $realInstance |
||
414 | * @param Mock $initializerMatcher |
||
415 | * |
||
416 | * @return callable |
||
417 | */ |
||
418 | private function createInitializer(string $className, $realInstance, Mock $initializerMatcher = null) : callable |
||
419 | { |
||
420 | /* @var $initializerMatcher callable|Mock */ |
||
421 | if (! $initializerMatcher) { |
||
422 | $initializerMatcher = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock(); |
||
423 | |||
424 | $initializerMatcher |
||
425 | ->expects(self::once()) |
||
426 | ->method('__invoke') |
||
427 | ->with( |
||
428 | self::logicalAnd( |
||
429 | self::isInstanceOf(VirtualProxyInterface::class), |
||
430 | self::isInstanceOf($className) |
||
431 | ), |
||
432 | $realInstance |
||
433 | ); |
||
434 | } |
||
435 | |||
436 | return function ( |
||
437 | & $wrappedObject, |
||
438 | VirtualProxyInterface $proxy, |
||
439 | $method, |
||
440 | $params, |
||
441 | & $initializer |
||
442 | ) use ( |
||
443 | $initializerMatcher, |
||
444 | $realInstance |
||
445 | ) : void { |
||
446 | $initializer = null; |
||
447 | $wrappedObject = $realInstance; |
||
448 | |||
449 | $initializerMatcher($proxy, $wrappedObject, $method, $params); |
||
450 | }; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Generates a list of object | invoked method | parameters | expected result |
||
455 | * |
||
456 | * @return array |
||
457 | */ |
||
458 | public function getProxyMethods() : array |
||
557 | |||
558 | /** |
||
559 | * Generates proxies and instances with a public property to feed to the property accessor methods |
||
560 | * |
||
561 | * @return array |
||
562 | */ |
||
563 | public function getPropertyAccessProxies() : array |
||
589 | |||
590 | /** |
||
591 | * @group 276 |
||
592 | * |
||
593 | * @dataProvider getMethodsThatAccessPropertiesOnOtherObjectsInTheSameScope |
||
594 | * |
||
595 | * @param object $callerObject |
||
596 | * @param object $realInstance |
||
597 | * @param string $method |
||
598 | * @param string $expectedValue |
||
599 | */ |
||
600 | public function testWillLazyLoadMembersOfOtherProxiesWithTheSamePrivateScope( |
||
601 | $callerObject, |
||
602 | $realInstance, |
||
603 | string $method, |
||
604 | string $expectedValue |
||
605 | ) : void { |
||
606 | $proxyName = $this->generateProxy(get_class($realInstance)); |
||
607 | /* @var $proxy OtherObjectAccessClass|LazyLoadingInterface */ |
||
608 | $proxy = $proxyName::staticProxyConstructor($this->createInitializer(get_class($realInstance), $realInstance)); |
||
609 | |||
610 | /* @var $accessor callable */ |
||
611 | $accessor = [$callerObject, $method]; |
||
612 | |||
613 | self::assertFalse($proxy->isProxyInitialized()); |
||
614 | self::assertSame($expectedValue, $accessor($proxy)); |
||
615 | self::assertTrue($proxy->isProxyInitialized()); |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * @group 276 |
||
620 | * |
||
621 | * @dataProvider getMethodsThatAccessPropertiesOnOtherObjectsInTheSameScope |
||
622 | * |
||
623 | * @param object $callerObject |
||
624 | * @param object $realInstance |
||
625 | * @param string $method |
||
626 | * @param string $expectedValue |
||
627 | */ |
||
628 | public function testWillFetchMembersOfOtherDeSerializedProxiesWithTheSamePrivateScope( |
||
629 | $callerObject, |
||
630 | $realInstance, |
||
631 | string $method, |
||
632 | string $expectedValue |
||
633 | ) : void { |
||
634 | $proxyName = $this->generateProxy(get_class($realInstance)); |
||
635 | /* @var $proxy OtherObjectAccessClass|LazyLoadingInterface */ |
||
636 | $proxy = unserialize(serialize( |
||
637 | $proxyName::staticProxyConstructor($this->createInitializer(get_class($realInstance), $realInstance)) |
||
638 | )); |
||
639 | |||
640 | /* @var $accessor callable */ |
||
641 | $accessor = [$callerObject, $method]; |
||
642 | |||
643 | self::assertTrue($proxy->isProxyInitialized()); |
||
644 | self::assertSame($expectedValue, $accessor($proxy)); |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * @group 276 |
||
649 | * |
||
650 | * @dataProvider getMethodsThatAccessPropertiesOnOtherObjectsInTheSameScope |
||
651 | * |
||
652 | * @param object $callerObject |
||
653 | * @param object $realInstance |
||
654 | * @param string $method |
||
655 | * @param string $expectedValue |
||
656 | */ |
||
657 | public function testWillFetchMembersOfOtherClonedProxiesWithTheSamePrivateScope( |
||
658 | $callerObject, |
||
659 | $realInstance, |
||
660 | string $method, |
||
661 | string $expectedValue |
||
662 | ) : void { |
||
663 | $proxyName = $this->generateProxy(get_class($realInstance)); |
||
664 | /* @var $proxy OtherObjectAccessClass|LazyLoadingInterface */ |
||
665 | $proxy = clone $proxyName::staticProxyConstructor( |
||
666 | $this->createInitializer(get_class($realInstance), $realInstance) |
||
667 | ); |
||
668 | |||
669 | /* @var $accessor callable */ |
||
670 | $accessor = [$callerObject, $method]; |
||
671 | |||
672 | self::assertTrue($proxy->isProxyInitialized()); |
||
673 | self::assertSame($expectedValue, $accessor($proxy)); |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * @group 327 |
||
678 | */ |
||
679 | public function testWillExecuteLogicInAVoidMethod() : void |
||
691 | |||
692 | public function getMethodsThatAccessPropertiesOnOtherObjectsInTheSameScope() : \Generator |
||
722 | |||
723 | /** |
||
724 | * @param object $instance |
||
725 | * @param array $values |
||
726 | * |
||
727 | * @return object |
||
728 | */ |
||
729 | private function buildInstanceWithValues($instance, array $values) |
||
741 | } |
||
742 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.