1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\Util; |
6
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\ParameterGenerator; |
8
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use ProxyManager\Generator\MethodGenerator; |
12
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\Util\InterceptorGenerator; |
13
|
|
|
use ProxyManagerTestAsset\BaseClass; |
14
|
|
|
use ProxyManagerTestAsset\VoidMethodTypeHintedInterface; |
15
|
|
|
use ReflectionMethod; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Tests for {@see \ProxyManager\ProxyGenerator\AccessInterceptorValueHolderGenerator} |
19
|
|
|
* |
20
|
|
|
* @group Coverage |
21
|
|
|
* @covers \ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\Util\InterceptorGenerator |
22
|
|
|
*/ |
23
|
|
|
final class InterceptorGeneratorTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function testInterceptorGenerator() : void |
26
|
|
|
{ |
27
|
|
|
$method = $this->createMock(MethodGenerator::class); |
|
|
|
|
28
|
|
|
$bar = $this->createMock(ParameterGenerator::class); |
|
|
|
|
29
|
|
|
$baz = $this->createMock(ParameterGenerator::class); |
|
|
|
|
30
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
31
|
|
|
$prefixInterceptors = $this->createMock(PropertyGenerator::class); |
|
|
|
|
32
|
|
|
$suffixInterceptors = $this->createMock(PropertyGenerator::class); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$bar->method('getName')->willReturn('bar'); |
|
|
|
|
35
|
|
|
$baz->method('getName')->willReturn('baz'); |
|
|
|
|
36
|
|
|
$method->method('getName')->willReturn('fooMethod'); |
|
|
|
|
37
|
|
|
$method->method('getParameters')->will(self::returnValue([$bar, $baz])); |
|
|
|
|
38
|
|
|
$valueHolder->method('getName')->willReturn('foo'); |
|
|
|
|
39
|
|
|
$prefixInterceptors->method('getName')->willReturn('pre'); |
|
|
|
|
40
|
|
|
$suffixInterceptors->method('getName')->willReturn('post'); |
|
|
|
|
41
|
|
|
|
42
|
|
|
// @codingStandardsIgnoreStart |
43
|
|
|
$expected = <<<'PHP' |
44
|
|
|
if (isset($this->pre['fooMethod'])) { |
45
|
|
|
$returnEarly = false; |
46
|
|
|
$prefixReturnValue = $this->pre['fooMethod']->__invoke($this, $this->foo, 'fooMethod', array('bar' => $bar, 'baz' => $baz), $returnEarly); |
47
|
|
|
|
48
|
|
|
if ($returnEarly) { |
49
|
|
|
return $prefixReturnValue; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$returnValue = "foo"; |
54
|
|
|
|
55
|
|
|
if (isset($this->post['fooMethod'])) { |
56
|
|
|
$returnEarly = false; |
57
|
|
|
$suffixReturnValue = $this->post['fooMethod']->__invoke($this, $this->foo, 'fooMethod', array('bar' => $bar, 'baz' => $baz), $returnValue, $returnEarly); |
58
|
|
|
|
59
|
|
|
if ($returnEarly) { |
60
|
|
|
return $suffixReturnValue; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $returnValue; |
65
|
|
|
PHP; |
66
|
|
|
// @codingStandardsIgnoreEnd |
67
|
|
|
|
68
|
|
|
self::assertSame( |
|
|
|
|
69
|
|
|
$expected, |
70
|
|
|
InterceptorGenerator::createInterceptedMethodBody( |
71
|
|
|
'$returnValue = "foo";', |
72
|
|
|
$method, |
|
|
|
|
73
|
|
|
$valueHolder, |
|
|
|
|
74
|
|
|
$prefixInterceptors, |
|
|
|
|
75
|
|
|
$suffixInterceptors, |
|
|
|
|
76
|
|
|
null |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testInterceptorGeneratorWithVoidMethod() : void |
82
|
|
|
{ |
83
|
|
|
$method = $this->createMock(MethodGenerator::class); |
|
|
|
|
84
|
|
|
$bar = $this->createMock(ParameterGenerator::class); |
|
|
|
|
85
|
|
|
$baz = $this->createMock(ParameterGenerator::class); |
|
|
|
|
86
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
87
|
|
|
$prefixInterceptors = $this->createMock(PropertyGenerator::class); |
|
|
|
|
88
|
|
|
$suffixInterceptors = $this->createMock(PropertyGenerator::class); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$bar->method('getName')->willReturn('bar'); |
|
|
|
|
91
|
|
|
$baz->method('getName')->willReturn('baz'); |
|
|
|
|
92
|
|
|
$method->method('getName')->willReturn('fooMethod'); |
|
|
|
|
93
|
|
|
$method->method('getParameters')->will(self::returnValue([$bar, $baz])); |
|
|
|
|
94
|
|
|
$valueHolder->method('getName')->willReturn('foo'); |
|
|
|
|
95
|
|
|
$prefixInterceptors->method('getName')->willReturn('pre'); |
|
|
|
|
96
|
|
|
$suffixInterceptors->method('getName')->willReturn('post'); |
|
|
|
|
97
|
|
|
|
98
|
|
|
// @codingStandardsIgnoreStart |
99
|
|
|
$expected = <<<'PHP' |
100
|
|
|
if (isset($this->pre['fooMethod'])) { |
101
|
|
|
$returnEarly = false; |
102
|
|
|
$prefixReturnValue = $this->pre['fooMethod']->__invoke($this, $this->foo, 'fooMethod', array('bar' => $bar, 'baz' => $baz), $returnEarly); |
103
|
|
|
|
104
|
|
|
if ($returnEarly) { |
105
|
|
|
$prefixReturnValue; |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$returnValue = "foo"; |
111
|
|
|
|
112
|
|
|
if (isset($this->post['fooMethod'])) { |
113
|
|
|
$returnEarly = false; |
114
|
|
|
$suffixReturnValue = $this->post['fooMethod']->__invoke($this, $this->foo, 'fooMethod', array('bar' => $bar, 'baz' => $baz), $returnValue, $returnEarly); |
115
|
|
|
|
116
|
|
|
if ($returnEarly) { |
117
|
|
|
$suffixReturnValue; |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$returnValue; |
123
|
|
|
return; |
124
|
|
|
PHP; |
125
|
|
|
// @codingStandardsIgnoreEnd |
126
|
|
|
|
127
|
|
|
self::assertSame( |
|
|
|
|
128
|
|
|
$expected, |
129
|
|
|
InterceptorGenerator::createInterceptedMethodBody( |
130
|
|
|
'$returnValue = "foo";', |
131
|
|
|
$method, |
|
|
|
|
132
|
|
|
$valueHolder, |
|
|
|
|
133
|
|
|
$prefixInterceptors, |
|
|
|
|
134
|
|
|
$suffixInterceptors, |
|
|
|
|
135
|
|
|
new ReflectionMethod(VoidMethodTypeHintedInterface::class, 'returnVoid') |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testInterceptorGeneratorWithNonVoidOriginalMethod() : void |
141
|
|
|
{ |
142
|
|
|
$method = $this->createMock(MethodGenerator::class); |
|
|
|
|
143
|
|
|
$bar = $this->createMock(ParameterGenerator::class); |
|
|
|
|
144
|
|
|
$baz = $this->createMock(ParameterGenerator::class); |
|
|
|
|
145
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
146
|
|
|
$prefixInterceptors = $this->createMock(PropertyGenerator::class); |
|
|
|
|
147
|
|
|
$suffixInterceptors = $this->createMock(PropertyGenerator::class); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$bar->method('getName')->willReturn('bar'); |
|
|
|
|
150
|
|
|
$baz->method('getName')->willReturn('baz'); |
|
|
|
|
151
|
|
|
$method->method('getName')->willReturn('fooMethod'); |
|
|
|
|
152
|
|
|
$method->method('getParameters')->will(self::returnValue([$bar, $baz])); |
|
|
|
|
153
|
|
|
$valueHolder->method('getName')->willReturn('foo'); |
|
|
|
|
154
|
|
|
$prefixInterceptors->method('getName')->willReturn('pre'); |
|
|
|
|
155
|
|
|
$suffixInterceptors->method('getName')->willReturn('post'); |
|
|
|
|
156
|
|
|
|
157
|
|
|
// @codingStandardsIgnoreStart |
158
|
|
|
$expected = <<<'PHP' |
159
|
|
|
if (isset($this->pre['fooMethod'])) { |
160
|
|
|
$returnEarly = false; |
161
|
|
|
$prefixReturnValue = $this->pre['fooMethod']->__invoke($this, $this->foo, 'fooMethod', array('bar' => $bar, 'baz' => $baz), $returnEarly); |
162
|
|
|
|
163
|
|
|
if ($returnEarly) { |
164
|
|
|
return $prefixReturnValue; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$returnValue = "foo"; |
169
|
|
|
|
170
|
|
|
if (isset($this->post['fooMethod'])) { |
171
|
|
|
$returnEarly = false; |
172
|
|
|
$suffixReturnValue = $this->post['fooMethod']->__invoke($this, $this->foo, 'fooMethod', array('bar' => $bar, 'baz' => $baz), $returnValue, $returnEarly); |
173
|
|
|
|
174
|
|
|
if ($returnEarly) { |
175
|
|
|
return $suffixReturnValue; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $returnValue; |
180
|
|
|
PHP; |
181
|
|
|
// @codingStandardsIgnoreEnd |
182
|
|
|
|
183
|
|
|
self::assertSame( |
|
|
|
|
184
|
|
|
$expected, |
185
|
|
|
InterceptorGenerator::createInterceptedMethodBody( |
186
|
|
|
'$returnValue = "foo";', |
187
|
|
|
$method, |
|
|
|
|
188
|
|
|
$valueHolder, |
|
|
|
|
189
|
|
|
$prefixInterceptors, |
|
|
|
|
190
|
|
|
$suffixInterceptors, |
|
|
|
|
191
|
|
|
new ReflectionMethod(BaseClass::class, 'publicMethod') |
192
|
|
|
) |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
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.