1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\ProxyGenerator\Util; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Tests for {@see \ProxyManager\ProxyGenerator\Util\PublicScopeSimulator} |
14
|
|
|
* |
15
|
|
|
* @covers \ProxyManager\ProxyGenerator\Util\PublicScopeSimulator |
16
|
|
|
* @group Coverage |
17
|
|
|
*/ |
18
|
|
|
final class PublicScopeSimulatorTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testSimpleGet() : void |
21
|
|
|
{ |
22
|
|
|
$expected = <<<'PHP' |
23
|
|
|
$realInstanceReflection = new \ReflectionClass(get_parent_class($this)); |
24
|
|
|
|
25
|
|
|
if (! $realInstanceReflection->hasProperty($foo)) { |
26
|
|
|
$targetObject = $this; |
27
|
|
|
|
28
|
|
|
$backtrace = debug_backtrace(false); |
29
|
|
|
trigger_error( |
30
|
|
|
sprintf( |
31
|
|
|
'Undefined property: %s::$%s in %s on line %s', |
32
|
|
|
get_parent_class($this), |
33
|
|
|
$foo, |
34
|
|
|
$backtrace[0]['file'], |
35
|
|
|
$backtrace[0]['line'] |
36
|
|
|
), |
37
|
|
|
\E_USER_NOTICE |
38
|
|
|
); |
39
|
|
|
return $targetObject->$foo; |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
44
|
|
|
$accessor = function & () use ($targetObject, $name) { |
45
|
|
|
return $targetObject->$foo; |
46
|
|
|
}; |
47
|
|
|
$backtrace = debug_backtrace(true); |
48
|
|
|
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); |
49
|
|
|
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); |
50
|
|
|
$bar = & $accessor(); |
51
|
|
|
PHP; |
52
|
|
|
|
53
|
|
|
self::assertSame( |
|
|
|
|
54
|
|
|
$expected, |
55
|
|
|
PublicScopeSimulator::getPublicAccessSimulationCode( |
56
|
|
|
PublicScopeSimulator::OPERATION_GET, |
57
|
|
|
'foo', |
58
|
|
|
null, |
59
|
|
|
null, |
60
|
|
|
'bar' |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testSimpleSet() : void |
66
|
|
|
{ |
67
|
|
|
$expected = <<<'PHP' |
68
|
|
|
$realInstanceReflection = new \ReflectionClass(get_parent_class($this)); |
69
|
|
|
|
70
|
|
|
if (! $realInstanceReflection->hasProperty($foo)) { |
71
|
|
|
$targetObject = $this; |
72
|
|
|
|
73
|
|
|
return $targetObject->$foo = $baz; |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
78
|
|
|
$accessor = function & () use ($targetObject, $name, $value) { |
79
|
|
|
return $targetObject->$foo = $baz; |
80
|
|
|
}; |
81
|
|
|
$backtrace = debug_backtrace(true); |
82
|
|
|
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); |
83
|
|
|
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); |
84
|
|
|
$bar = & $accessor(); |
85
|
|
|
PHP; |
86
|
|
|
|
87
|
|
|
self::assertSame( |
|
|
|
|
88
|
|
|
$expected, |
89
|
|
|
PublicScopeSimulator::getPublicAccessSimulationCode( |
90
|
|
|
PublicScopeSimulator::OPERATION_SET, |
91
|
|
|
'foo', |
92
|
|
|
'baz', |
93
|
|
|
null, |
94
|
|
|
'bar' |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testSimpleIsset() : void |
100
|
|
|
{ |
101
|
|
|
$expected = <<<'PHP' |
102
|
|
|
$realInstanceReflection = new \ReflectionClass(get_parent_class($this)); |
103
|
|
|
|
104
|
|
|
if (! $realInstanceReflection->hasProperty($foo)) { |
105
|
|
|
$targetObject = $this; |
106
|
|
|
|
107
|
|
|
return isset($targetObject->$foo); |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
112
|
|
|
$accessor = function () use ($targetObject, $name) { |
113
|
|
|
return isset($targetObject->$foo); |
114
|
|
|
}; |
115
|
|
|
$backtrace = debug_backtrace(true); |
116
|
|
|
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); |
117
|
|
|
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); |
118
|
|
|
$bar = $accessor(); |
119
|
|
|
PHP; |
120
|
|
|
|
121
|
|
|
self::assertSame( |
|
|
|
|
122
|
|
|
$expected, |
123
|
|
|
PublicScopeSimulator::getPublicAccessSimulationCode( |
124
|
|
|
PublicScopeSimulator::OPERATION_ISSET, |
125
|
|
|
'foo', |
126
|
|
|
null, |
127
|
|
|
null, |
128
|
|
|
'bar' |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testSimpleUnset() : void |
134
|
|
|
{ |
135
|
|
|
$expected = <<<'PHP' |
136
|
|
|
$realInstanceReflection = new \ReflectionClass(get_parent_class($this)); |
137
|
|
|
|
138
|
|
|
if (! $realInstanceReflection->hasProperty($foo)) { |
139
|
|
|
$targetObject = $this; |
140
|
|
|
|
141
|
|
|
unset($targetObject->$foo); |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
146
|
|
|
$accessor = function () use ($targetObject, $name) { |
147
|
|
|
unset($targetObject->$foo); |
148
|
|
|
}; |
149
|
|
|
$backtrace = debug_backtrace(true); |
150
|
|
|
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); |
151
|
|
|
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); |
152
|
|
|
$bar = $accessor(); |
153
|
|
|
PHP; |
154
|
|
|
|
155
|
|
|
self::assertSame( |
|
|
|
|
156
|
|
|
$expected, |
157
|
|
|
PublicScopeSimulator::getPublicAccessSimulationCode( |
158
|
|
|
PublicScopeSimulator::OPERATION_UNSET, |
159
|
|
|
'foo', |
160
|
|
|
null, |
161
|
|
|
null, |
162
|
|
|
'bar' |
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testSetRequiresValueParameterName() : void |
168
|
|
|
{ |
169
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
|
|
|
170
|
|
|
|
171
|
|
|
PublicScopeSimulator::getPublicAccessSimulationCode( |
172
|
|
|
PublicScopeSimulator::OPERATION_SET, |
173
|
|
|
'foo', |
174
|
|
|
null, |
175
|
|
|
null, |
176
|
|
|
'bar' |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testDelegatesToValueHolderWhenAvailable() : void |
181
|
|
|
{ |
182
|
|
|
$expected = <<<'PHP' |
183
|
|
|
$realInstanceReflection = new \ReflectionClass(get_parent_class($this)); |
184
|
|
|
|
185
|
|
|
if (! $realInstanceReflection->hasProperty($foo)) { |
186
|
|
|
$targetObject = $this->valueHolder; |
187
|
|
|
|
188
|
|
|
return $targetObject->$foo = $baz; |
189
|
|
|
return; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$targetObject = $this->valueHolder; |
193
|
|
|
$accessor = function & () use ($targetObject, $name, $value) { |
194
|
|
|
return $targetObject->$foo = $baz; |
195
|
|
|
}; |
196
|
|
|
$backtrace = debug_backtrace(true); |
197
|
|
|
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); |
198
|
|
|
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); |
199
|
|
|
$bar = & $accessor(); |
200
|
|
|
PHP; |
201
|
|
|
|
202
|
|
|
self::assertSame( |
|
|
|
|
203
|
|
|
$expected, |
204
|
|
|
PublicScopeSimulator::getPublicAccessSimulationCode( |
205
|
|
|
PublicScopeSimulator::OPERATION_SET, |
206
|
|
|
'foo', |
207
|
|
|
'baz', |
208
|
|
|
new PropertyGenerator('valueHolder'), |
209
|
|
|
'bar' |
210
|
|
|
) |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testSetRequiresValidOperation() : void |
215
|
|
|
{ |
216
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
|
|
|
217
|
|
|
|
218
|
|
|
PublicScopeSimulator::getPublicAccessSimulationCode('invalid', 'foo'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testWillReturnDirectlyWithNoReturnParam() : void |
222
|
|
|
{ |
223
|
|
|
$expected = <<<'PHP' |
224
|
|
|
$realInstanceReflection = new \ReflectionClass(get_parent_class($this)); |
225
|
|
|
|
226
|
|
|
if (! $realInstanceReflection->hasProperty($foo)) { |
227
|
|
|
$targetObject = $this; |
228
|
|
|
|
229
|
|
|
$backtrace = debug_backtrace(false); |
230
|
|
|
trigger_error( |
231
|
|
|
sprintf( |
232
|
|
|
'Undefined property: %s::$%s in %s on line %s', |
233
|
|
|
get_parent_class($this), |
234
|
|
|
$foo, |
235
|
|
|
$backtrace[0]['file'], |
236
|
|
|
$backtrace[0]['line'] |
237
|
|
|
), |
238
|
|
|
\E_USER_NOTICE |
239
|
|
|
); |
240
|
|
|
return $targetObject->$foo; |
241
|
|
|
return; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor(); |
245
|
|
|
$accessor = function & () use ($targetObject, $name) { |
246
|
|
|
return $targetObject->$foo; |
247
|
|
|
}; |
248
|
|
|
$backtrace = debug_backtrace(true); |
249
|
|
|
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); |
250
|
|
|
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); |
251
|
|
|
$returnValue = & $accessor(); |
252
|
|
|
|
253
|
|
|
return $returnValue; |
254
|
|
|
PHP; |
255
|
|
|
|
256
|
|
|
self::assertSame( |
|
|
|
|
257
|
|
|
$expected, |
258
|
|
|
PublicScopeSimulator::getPublicAccessSimulationCode( |
259
|
|
|
PublicScopeSimulator::OPERATION_GET, |
260
|
|
|
'foo' |
261
|
|
|
) |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.