|
25 | 25 | use ProxyManagerTestAsset\ClassWithSelfHint;
|
26 | 26 | use ProxyManagerTestAsset\EmptyClass;
|
27 | 27 | use ProxyManagerTestAsset\OtherObjectAccessClass;
|
| 28 | +use ProxyManagerTestAsset\ReferenceIncrementDecrementClass; |
28 | 29 | use ProxyManagerTestAsset\VoidCounter;
|
29 | 30 | use ReflectionClass;
|
30 | 31 | use ReflectionProperty;
|
@@ -711,6 +712,79 @@ public function testWillInterceptAndReturnEarlyOnVoidMethod(): void
|
711 | 712 | self::assertSame($increment + $addMore + 1, $object->counter);
|
712 | 713 | }
|
713 | 714 |
|
| 715 | + public function testByReferencePassedArgumentsAreGivenAsReferenceToInterceptorCallbacks(): void |
| 716 | + { |
| 717 | + $proxy = (new AccessInterceptorValueHolderFactory())->createProxy( |
| 718 | + new ReferenceIncrementDecrementClass(), |
| 719 | + [ |
| 720 | + 'incrementReference' => static function ( |
| 721 | + $proxy, |
| 722 | + ReferenceIncrementDecrementClass $instance, |
| 723 | + string $method, |
| 724 | + array $args, |
| 725 | + bool &$returnEarly |
| 726 | + ): void { |
| 727 | + self::assertSame(0, $args['reference']); |
| 728 | + |
| 729 | + $returnEarly = true; |
| 730 | + $args['reference'] = 5; |
| 731 | + }, |
| 732 | + ] |
| 733 | + ); |
| 734 | + |
| 735 | + $number = 0; |
| 736 | + |
| 737 | + $proxy->incrementReference($number); |
| 738 | + |
| 739 | + self::assertSame(5, $number, 'Number was changed by interceptor'); |
| 740 | + } |
| 741 | + |
| 742 | + public function testByReferenceArgumentsAreForwardedThroughInterceptorsAndSubject(): void |
| 743 | + { |
| 744 | + $proxy = (new AccessInterceptorValueHolderFactory())->createProxy( |
| 745 | + new ReferenceIncrementDecrementClass(), |
| 746 | + [ |
| 747 | + 'incrementReference' => static function ( |
| 748 | + $proxy, |
| 749 | + ReferenceIncrementDecrementClass $instance, |
| 750 | + string $method, |
| 751 | + array $args, |
| 752 | + bool &$returnEarly |
| 753 | + ): void { |
| 754 | + self::assertSame(0, $args['reference']); |
| 755 | + |
| 756 | + $returnEarly = false; |
| 757 | + $args['reference'] = 5; |
| 758 | + }, |
| 759 | + ], |
| 760 | + [ |
| 761 | + 'incrementReference' => static function ( |
| 762 | + $proxy, |
| 763 | + ReferenceIncrementDecrementClass $instance, |
| 764 | + string $method, |
| 765 | + array $args, |
| 766 | + $returnValue, |
| 767 | + bool &$returnEarly |
| 768 | + ): void { |
| 769 | + self::assertIsInt($args['reference']); |
| 770 | + |
| 771 | + $returnEarly = false; |
| 772 | + $args['reference'] *= 2; |
| 773 | + }, |
| 774 | + ] |
| 775 | + ); |
| 776 | + |
| 777 | + $number = 0; |
| 778 | + |
| 779 | + $proxy->incrementReference($number); |
| 780 | + |
| 781 | + self::assertSame( |
| 782 | + 12, |
| 783 | + $number, |
| 784 | + 'Number was changed by prefix interceptor, then incremented, then doubled by suffix interceptor' |
| 785 | + ); |
| 786 | + } |
| 787 | + |
714 | 788 | private static function assertByRefVariableValueSame($expected, & $actual): void
|
715 | 789 | {
|
716 | 790 | self::assertSame($expected, $actual);
|
|
0 commit comments