|
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;
|
@@ -696,6 +697,79 @@ public function testWillInterceptAndReturnEarlyOnVoidMethod(): void
|
696 | 697 | self::assertSame($increment + $addMore + 1, $object->counter);
|
697 | 698 | }
|
698 | 699 |
|
| 700 | + public function testByReferencePassedArgumentsAreGivenAsReferenceToInterceptorCallbacks(): void |
| 701 | + { |
| 702 | + $proxy = (new AccessInterceptorValueHolderFactory())->createProxy( |
| 703 | + new ReferenceIncrementDecrementClass(), |
| 704 | + [ |
| 705 | + 'incrementReference' => static function ( |
| 706 | + object $proxy, |
| 707 | + ReferenceIncrementDecrementClass $instance, |
| 708 | + string $method, |
| 709 | + array $args, |
| 710 | + bool &$returnEarly |
| 711 | + ): void { |
| 712 | + self::assertSame(0, $args['reference']); |
| 713 | + |
| 714 | + $returnEarly = true; |
| 715 | + $args['reference'] = 5; |
| 716 | + }, |
| 717 | + ] |
| 718 | + ); |
| 719 | + |
| 720 | + $number = 0; |
| 721 | + |
| 722 | + $proxy->incrementReference($number); |
| 723 | + |
| 724 | + self::assertSame(5, $number, 'Number was changed by interceptor'); |
| 725 | + } |
| 726 | + |
| 727 | + public function testByReferenceArgumentsAreForwardedThroughInterceptorsAndSubject(): void |
| 728 | + { |
| 729 | + $proxy = (new AccessInterceptorValueHolderFactory())->createProxy( |
| 730 | + new ReferenceIncrementDecrementClass(), |
| 731 | + [ |
| 732 | + 'incrementReference' => static function ( |
| 733 | + object $proxy, |
| 734 | + ReferenceIncrementDecrementClass $instance, |
| 735 | + string $method, |
| 736 | + array $args, |
| 737 | + bool &$returnEarly |
| 738 | + ): void { |
| 739 | + self::assertSame(0, $args['reference']); |
| 740 | + |
| 741 | + $returnEarly = false; |
| 742 | + $args['reference'] = 5; |
| 743 | + }, |
| 744 | + ], |
| 745 | + [ |
| 746 | + 'incrementReference' => static function ( |
| 747 | + object $proxy, |
| 748 | + ReferenceIncrementDecrementClass $instance, |
| 749 | + string $method, |
| 750 | + array $args, |
| 751 | + mixed $returnValue, |
| 752 | + bool &$returnEarly |
| 753 | + ): void { |
| 754 | + self::assertIsInt($args['reference']); |
| 755 | + |
| 756 | + $returnEarly = false; |
| 757 | + $args['reference'] *= 2; |
| 758 | + }, |
| 759 | + ] |
| 760 | + ); |
| 761 | + |
| 762 | + $number = 0; |
| 763 | + |
| 764 | + $proxy->incrementReference($number); |
| 765 | + |
| 766 | + self::assertSame( |
| 767 | + 12, |
| 768 | + $number, |
| 769 | + 'Number was changed by prefix interceptor, then incremented, then doubled by suffix interceptor' |
| 770 | + ); |
| 771 | + } |
| 772 | + |
699 | 773 | private static function assertByRefVariableValueSame(mixed $expected, mixed & $actual): void
|
700 | 774 | {
|
701 | 775 | self::assertSame($expected, $actual);
|
|
0 commit comments