1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\ProxyGenerator\LazyLoadingGhost\MethodGenerator; |
6
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer; |
11
|
|
|
use ProxyManager\ProxyGenerator\Util\Properties; |
12
|
|
|
use ProxyManagerTestAsset\ClassWithMixedProperties; |
13
|
|
|
use ProxyManagerTestAsset\ClassWithMixedTypedProperties; |
14
|
|
|
use ReflectionClass; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer} |
18
|
|
|
* |
19
|
|
|
* @group Coverage |
20
|
|
|
* @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer |
21
|
|
|
*/ |
22
|
|
|
final class CallInitializerTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function testBodyStructure() : void |
25
|
|
|
{ |
26
|
|
|
$initializer = $this->createMock(PropertyGenerator::class); |
|
|
|
|
27
|
|
|
$initializationTracker = $this->createMock(PropertyGenerator::class); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$initializer->method('getName')->willReturn('init'); |
|
|
|
|
30
|
|
|
$initializationTracker->method('getName')->willReturn('track'); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$callInitializer = new CallInitializer( |
33
|
|
|
$initializer, |
|
|
|
|
34
|
|
|
$initializationTracker, |
|
|
|
|
35
|
|
|
Properties::fromReflectionClass(new ReflectionClass(ClassWithMixedProperties::class)) |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$expectedCode = <<<'PHP' |
39
|
|
|
if ($this->track || ! $this->init) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->track = true; |
44
|
|
|
|
45
|
|
|
$this->publicProperty0 = 'publicProperty0'; |
46
|
|
|
$this->publicProperty1 = 'publicProperty1'; |
47
|
|
|
$this->publicProperty2 = 'publicProperty2'; |
48
|
|
|
$this->protectedProperty0 = 'protectedProperty0'; |
49
|
|
|
$this->protectedProperty1 = 'protectedProperty1'; |
50
|
|
|
$this->protectedProperty2 = 'protectedProperty2'; |
51
|
|
|
static $cacheProxyManagerTestAsset_ClassWithMixedProperties; |
52
|
|
|
|
53
|
|
|
$cacheProxyManagerTestAsset_ClassWithMixedProperties ?: $cacheProxyManagerTestAsset_ClassWithMixedProperties = \Closure::bind(static function ($instance) { |
54
|
|
|
$instance->privateProperty0 = 'privateProperty0'; |
55
|
|
|
$instance->privateProperty1 = 'privateProperty1'; |
56
|
|
|
$instance->privateProperty2 = 'privateProperty2'; |
57
|
|
|
}, null, 'ProxyManagerTestAsset\\ClassWithMixedProperties'); |
58
|
|
|
|
59
|
|
|
$cacheProxyManagerTestAsset_ClassWithMixedProperties($this); |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
$properties = [ |
65
|
|
|
'publicProperty0' => & $this->publicProperty0, |
66
|
|
|
'publicProperty1' => & $this->publicProperty1, |
67
|
|
|
'publicProperty2' => & $this->publicProperty2, |
68
|
|
|
'' . "\0" . '*' . "\0" . 'protectedProperty0' => & $this->protectedProperty0, |
69
|
|
|
'' . "\0" . '*' . "\0" . 'protectedProperty1' => & $this->protectedProperty1, |
70
|
|
|
'' . "\0" . '*' . "\0" . 'protectedProperty2' => & $this->protectedProperty2, |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
static $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties; |
74
|
|
|
|
75
|
|
|
$cacheFetchProxyManagerTestAsset_ClassWithMixedProperties ?: $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties = \Closure::bind(function ($instance, array & $properties) { |
76
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty0'] = & $instance->privateProperty0; |
77
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty1'] = & $instance->privateProperty1; |
78
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty2'] = & $instance->privateProperty2; |
79
|
|
|
}, $this, 'ProxyManagerTestAsset\\ClassWithMixedProperties'); |
80
|
|
|
|
81
|
|
|
$cacheFetchProxyManagerTestAsset_ClassWithMixedProperties($this, $properties); |
82
|
|
|
|
83
|
|
|
$result = $this->init->__invoke($this, $methodName, $parameters, $this->init, $properties); |
84
|
|
|
$this->track = false; |
85
|
|
|
|
86
|
|
|
return $result; |
87
|
|
|
PHP; |
88
|
|
|
|
89
|
|
|
self::assertSame( |
|
|
|
|
90
|
|
|
$expectedCode, |
91
|
|
|
$callInitializer->getBody() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testBodyStructureWithTypedProperties() : void |
96
|
|
|
{ |
97
|
|
|
$initializer = $this->createMock(PropertyGenerator::class); |
|
|
|
|
98
|
|
|
$initializationTracker = $this->createMock(PropertyGenerator::class); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$initializer->method('getName')->willReturn('init'); |
|
|
|
|
101
|
|
|
$initializationTracker->method('getName')->willReturn('track'); |
|
|
|
|
102
|
|
|
|
103
|
|
|
$callInitializer = new CallInitializer( |
104
|
|
|
$initializer, |
|
|
|
|
105
|
|
|
$initializationTracker, |
|
|
|
|
106
|
|
|
Properties::fromReflectionClass(new ReflectionClass(ClassWithMixedTypedProperties::class)) |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$expectedCode = <<<'PHP' |
110
|
|
|
if ($this->track || ! $this->init) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->track = true; |
115
|
|
|
|
116
|
|
|
$this->publicUnTypedProperty = 'publicUnTypedProperty'; |
117
|
|
|
$this->publicUnTypedPropertyWithoutDefaultValue = NULL; |
118
|
|
|
$this->publicBoolProperty = true; |
119
|
|
|
$this->publicNullableBoolProperty = true; |
120
|
|
|
$this->publicIntProperty = 123; |
121
|
|
|
$this->publicNullableIntProperty = 123; |
122
|
|
|
$this->publicFloatProperty = 123.456; |
123
|
|
|
$this->publicNullableFloatProperty = 123.456; |
124
|
|
|
$this->publicStringProperty = 'publicStringProperty'; |
125
|
|
|
$this->publicNullableStringProperty = 'publicStringProperty'; |
126
|
|
|
$this->publicArrayProperty = array ( |
127
|
|
|
0 => 'publicArrayProperty', |
128
|
|
|
); |
129
|
|
|
$this->publicNullableArrayProperty = array ( |
130
|
|
|
0 => 'publicArrayProperty', |
131
|
|
|
); |
132
|
|
|
$this->publicIterableProperty = array ( |
133
|
|
|
0 => 'publicIterableProperty', |
134
|
|
|
); |
135
|
|
|
$this->publicNullableIterableProperty = array ( |
136
|
|
|
0 => 'publicIterableProperty', |
137
|
|
|
); |
138
|
|
|
$this->protectedUnTypedProperty = 'protectedUnTypedProperty'; |
139
|
|
|
$this->protectedUnTypedPropertyWithoutDefaultValue = NULL; |
140
|
|
|
$this->protectedBoolProperty = true; |
141
|
|
|
$this->protectedNullableBoolProperty = true; |
142
|
|
|
$this->protectedIntProperty = 123; |
143
|
|
|
$this->protectedNullableIntProperty = 123; |
144
|
|
|
$this->protectedFloatProperty = 123.456; |
145
|
|
|
$this->protectedNullableFloatProperty = 123.456; |
146
|
|
|
$this->protectedStringProperty = 'protectedStringProperty'; |
147
|
|
|
$this->protectedNullableStringProperty = 'protectedStringProperty'; |
148
|
|
|
$this->protectedArrayProperty = array ( |
149
|
|
|
0 => 'protectedArrayProperty', |
150
|
|
|
); |
151
|
|
|
$this->protectedNullableArrayProperty = array ( |
152
|
|
|
0 => 'protectedArrayProperty', |
153
|
|
|
); |
154
|
|
|
$this->protectedIterableProperty = array ( |
155
|
|
|
0 => 'protectedIterableProperty', |
156
|
|
|
); |
157
|
|
|
$this->protectedNullableIterableProperty = array ( |
158
|
|
|
0 => 'protectedIterableProperty', |
159
|
|
|
); |
160
|
|
|
static $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties; |
161
|
|
|
|
162
|
|
|
$cacheProxyManagerTestAsset_ClassWithMixedTypedProperties ?: $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(static function ($instance) { |
163
|
|
|
$instance->privateUnTypedProperty = 'privateUnTypedProperty'; |
164
|
|
|
$instance->privateUnTypedPropertyWithoutDefaultValue = NULL; |
165
|
|
|
$instance->privateBoolProperty = true; |
166
|
|
|
$instance->privateNullableBoolProperty = true; |
167
|
|
|
$instance->privateIntProperty = 123; |
168
|
|
|
$instance->privateNullableIntProperty = 123; |
169
|
|
|
$instance->privateFloatProperty = 123.456; |
170
|
|
|
$instance->privateNullableFloatProperty = 123.456; |
171
|
|
|
$instance->privateStringProperty = 'privateStringProperty'; |
172
|
|
|
$instance->privateNullableStringProperty = 'privateStringProperty'; |
173
|
|
|
$instance->privateArrayProperty = array ( |
174
|
|
|
0 => 'privateArrayProperty', |
175
|
|
|
); |
176
|
|
|
$instance->privateNullableArrayProperty = array ( |
177
|
|
|
0 => 'privateArrayProperty', |
178
|
|
|
); |
179
|
|
|
$instance->privateIterableProperty = array ( |
180
|
|
|
0 => 'privateIterableProperty', |
181
|
|
|
); |
182
|
|
|
$instance->privateNullableIterableProperty = array ( |
183
|
|
|
0 => 'privateIterableProperty', |
184
|
|
|
); |
185
|
|
|
}, null, 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties'); |
186
|
|
|
|
187
|
|
|
$cacheProxyManagerTestAsset_ClassWithMixedTypedProperties($this); |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
|
191
|
|
|
|
192
|
|
|
$properties = [ |
193
|
|
|
'publicUnTypedProperty' => & $this->publicUnTypedProperty, |
194
|
|
|
'publicUnTypedPropertyWithoutDefaultValue' => & $this->publicUnTypedPropertyWithoutDefaultValue, |
195
|
|
|
'publicBoolProperty' => & $this->publicBoolProperty, |
196
|
|
|
'publicNullableBoolProperty' => & $this->publicNullableBoolProperty, |
197
|
|
|
'publicIntProperty' => & $this->publicIntProperty, |
198
|
|
|
'publicNullableIntProperty' => & $this->publicNullableIntProperty, |
199
|
|
|
'publicFloatProperty' => & $this->publicFloatProperty, |
200
|
|
|
'publicNullableFloatProperty' => & $this->publicNullableFloatProperty, |
201
|
|
|
'publicStringProperty' => & $this->publicStringProperty, |
202
|
|
|
'publicNullableStringProperty' => & $this->publicNullableStringProperty, |
203
|
|
|
'publicArrayProperty' => & $this->publicArrayProperty, |
204
|
|
|
'publicNullableArrayProperty' => & $this->publicNullableArrayProperty, |
205
|
|
|
'publicIterableProperty' => & $this->publicIterableProperty, |
206
|
|
|
'publicNullableIterableProperty' => & $this->publicNullableIterableProperty, |
207
|
|
|
'' . "\0" . '*' . "\0" . 'protectedUnTypedProperty' => & $this->protectedUnTypedProperty, |
208
|
|
|
'' . "\0" . '*' . "\0" . 'protectedUnTypedPropertyWithoutDefaultValue' => & $this->protectedUnTypedPropertyWithoutDefaultValue, |
209
|
|
|
'' . "\0" . '*' . "\0" . 'protectedBoolProperty' => & $this->protectedBoolProperty, |
210
|
|
|
'' . "\0" . '*' . "\0" . 'protectedNullableBoolProperty' => & $this->protectedNullableBoolProperty, |
211
|
|
|
'' . "\0" . '*' . "\0" . 'protectedIntProperty' => & $this->protectedIntProperty, |
212
|
|
|
'' . "\0" . '*' . "\0" . 'protectedNullableIntProperty' => & $this->protectedNullableIntProperty, |
213
|
|
|
'' . "\0" . '*' . "\0" . 'protectedFloatProperty' => & $this->protectedFloatProperty, |
214
|
|
|
'' . "\0" . '*' . "\0" . 'protectedNullableFloatProperty' => & $this->protectedNullableFloatProperty, |
215
|
|
|
'' . "\0" . '*' . "\0" . 'protectedStringProperty' => & $this->protectedStringProperty, |
216
|
|
|
'' . "\0" . '*' . "\0" . 'protectedNullableStringProperty' => & $this->protectedNullableStringProperty, |
217
|
|
|
'' . "\0" . '*' . "\0" . 'protectedArrayProperty' => & $this->protectedArrayProperty, |
218
|
|
|
'' . "\0" . '*' . "\0" . 'protectedNullableArrayProperty' => & $this->protectedNullableArrayProperty, |
219
|
|
|
'' . "\0" . '*' . "\0" . 'protectedIterableProperty' => & $this->protectedIterableProperty, |
220
|
|
|
'' . "\0" . '*' . "\0" . 'protectedNullableIterableProperty' => & $this->protectedNullableIterableProperty, |
221
|
|
|
]; |
222
|
|
|
|
223
|
|
|
static $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties; |
224
|
|
|
|
225
|
|
|
$cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties ?: $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(function ($instance, array & $properties) { |
226
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateUnTypedProperty'] = & $instance->privateUnTypedProperty; |
227
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateUnTypedPropertyWithoutDefaultValue'] = & $instance->privateUnTypedPropertyWithoutDefaultValue; |
228
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateBoolProperty'] = & $instance->privateBoolProperty; |
229
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableBoolProperty'] = & $instance->privateNullableBoolProperty; |
230
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateIntProperty'] = & $instance->privateIntProperty; |
231
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableIntProperty'] = & $instance->privateNullableIntProperty; |
232
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateFloatProperty'] = & $instance->privateFloatProperty; |
233
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableFloatProperty'] = & $instance->privateNullableFloatProperty; |
234
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateStringProperty'] = & $instance->privateStringProperty; |
235
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableStringProperty'] = & $instance->privateNullableStringProperty; |
236
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateArrayProperty'] = & $instance->privateArrayProperty; |
237
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableArrayProperty'] = & $instance->privateNullableArrayProperty; |
238
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateIterableProperty'] = & $instance->privateIterableProperty; |
239
|
|
|
$properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableIterableProperty'] = & $instance->privateNullableIterableProperty; |
240
|
|
|
}, $this, 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties'); |
241
|
|
|
|
242
|
|
|
$cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties($this, $properties); |
243
|
|
|
|
244
|
|
|
$result = $this->init->__invoke($this, $methodName, $parameters, $this->init, $properties); |
245
|
|
|
$this->track = false; |
246
|
|
|
|
247
|
|
|
return $result; |
248
|
|
|
PHP; |
249
|
|
|
|
250
|
|
|
self::assertSame( |
|
|
|
|
251
|
|
|
$expectedCode, |
252
|
|
|
$callInitializer->getBody() |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.