|
9 | 9 | use Laminas\Code\Generator\PropertyGenerator; |
10 | 10 | use PHPUnit\Framework\TestCase; |
11 | 11 | use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator; |
| 12 | +use ProxyManagerTestAsset\ClassWithMixedProperties; |
12 | 13 | use ReflectionClass; |
13 | 14 |
|
| 15 | +use function sprintf; |
| 16 | + |
14 | 17 | /** |
15 | 18 | * Tests for {@see \ProxyManager\ProxyGenerator\Util\PublicScopeSimulator} |
16 | 19 | * |
@@ -43,7 +46,7 @@ public function testSimpleGet(): void |
43 | 46 | } |
44 | 47 |
|
45 | 48 | $targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
46 | | -$accessor = function & () use ($targetObject, $name) { |
| 49 | +$accessor = function & () use ($targetObject, $foo) { |
47 | 50 | return $targetObject->$foo; |
48 | 51 | }; |
49 | 52 | $backtrace = debug_backtrace(true, 2); |
@@ -72,13 +75,13 @@ public function testSimpleSet(): void |
72 | 75 | if (! $realInstanceReflection->hasProperty($foo)) { |
73 | 76 | $targetObject = $this; |
74 | 77 |
|
75 | | - return $targetObject->$foo = $baz; |
| 78 | + $targetObject->$foo = $baz; return $targetObject->$foo; |
76 | 79 | return; |
77 | 80 | } |
78 | 81 |
|
79 | 82 | $targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
80 | | -$accessor = function & () use ($targetObject, $name, $value) { |
81 | | - return $targetObject->$foo = $baz; |
| 83 | +$accessor = function & () use ($targetObject, $foo, $baz) { |
| 84 | + $targetObject->$foo = $baz; return $targetObject->$foo; |
82 | 85 | }; |
83 | 86 | $backtrace = debug_backtrace(true, 2); |
84 | 87 | $scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); |
@@ -111,7 +114,7 @@ public function testSimpleIsset(): void |
111 | 114 | } |
112 | 115 |
|
113 | 116 | $targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
114 | | -$accessor = function () use ($targetObject, $name) { |
| 117 | +$accessor = function () use ($targetObject, $foo) { |
115 | 118 | return isset($targetObject->$foo); |
116 | 119 | }; |
117 | 120 | $backtrace = debug_backtrace(true, 2); |
@@ -145,7 +148,7 @@ public function testSimpleUnset(): void |
145 | 148 | } |
146 | 149 |
|
147 | 150 | $targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
148 | | -$accessor = function () use ($targetObject, $name) { |
| 151 | +$accessor = function () use ($targetObject, $foo) { |
149 | 152 | unset($targetObject->$foo); |
150 | 153 | }; |
151 | 154 | $backtrace = debug_backtrace(true, 2); |
@@ -187,13 +190,13 @@ public function testDelegatesToValueHolderWhenAvailable(): void |
187 | 190 | if (! $realInstanceReflection->hasProperty($foo)) { |
188 | 191 | $targetObject = $this->valueHolder; |
189 | 192 |
|
190 | | - return $targetObject->$foo = $baz; |
| 193 | + $targetObject->$foo = $baz; return $targetObject->$foo; |
191 | 194 | return; |
192 | 195 | } |
193 | 196 |
|
194 | 197 | $targetObject = $this->valueHolder; |
195 | | -$accessor = function & () use ($targetObject, $name, $value) { |
196 | | - return $targetObject->$foo = $baz; |
| 198 | +$accessor = function & () use ($targetObject, $foo, $baz) { |
| 199 | + $targetObject->$foo = $baz; return $targetObject->$foo; |
197 | 200 | }; |
198 | 201 | $backtrace = debug_backtrace(true, 2); |
199 | 202 | $scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); |
@@ -244,7 +247,7 @@ public function testWillReturnDirectlyWithNoReturnParam(): void |
244 | 247 | } |
245 | 248 |
|
246 | 249 | $targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
247 | | -$accessor = function & () use ($targetObject, $name) { |
| 250 | +$accessor = function & () use ($targetObject, $foo) { |
248 | 251 | return $targetObject->$foo; |
249 | 252 | }; |
250 | 253 | $backtrace = debug_backtrace(true, 2); |
@@ -282,4 +285,35 @@ public function testWillNotAttemptToGetParentClassWhenReflectionClassIsGivenUpfr |
282 | 285 | ) |
283 | 286 | ); |
284 | 287 | } |
| 288 | + |
| 289 | + /** |
| 290 | + * @group #632 |
| 291 | + * @group #645 |
| 292 | + * @group #646 |
| 293 | + */ |
| 294 | + public function testFunctional(): void |
| 295 | + { |
| 296 | + /** @psalm-var ClassWithMixedProperties $sut */ |
| 297 | + $sut = eval( |
| 298 | + sprintf( |
| 299 | + 'return new class() extends %s |
| 300 | + { |
| 301 | + public function doGet($prop) { %s } |
| 302 | + public function doSet($prop, $val) { %s } |
| 303 | + public function doIsset($prop) { %s } |
| 304 | + public function doUnset($prop) { %s } |
| 305 | + };', |
| 306 | + ClassWithMixedProperties::class, |
| 307 | + PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_GET, 'prop'), |
| 308 | + PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_SET, 'prop', 'val'), |
| 309 | + PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_ISSET, 'prop'), |
| 310 | + PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_UNSET, 'prop') |
| 311 | + ) |
| 312 | + ); |
| 313 | + |
| 314 | + $this->assertSame('publicProperty0', $sut->doGet('publicProperty0')); |
| 315 | + $this->assertSame('bar', $sut->doSet('publicProperty0', 'bar')); |
| 316 | + $this->assertTrue($sut->doIsset('publicProperty0')); |
| 317 | + $this->assertNull($sut->doUnset('publicProperty0')); |
| 318 | + } |
285 | 319 | } |
0 commit comments