1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\SymfonyGenerics\Services\NewArgumentCompiler; |
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentContextInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
20
|
|
|
use ReflectionFunctionAbstract; |
21
|
|
|
use ReflectionParameter; |
22
|
|
|
use ReflectionType; |
23
|
|
|
use ReflectionException; |
24
|
|
|
|
25
|
|
|
final class NewArgumentCompilerTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var NewArgumentCompiler |
30
|
|
|
*/ |
31
|
|
|
private $compiler; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ArgumentFactory |
35
|
|
|
*/ |
36
|
|
|
private $argumentFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var RequestStack |
40
|
|
|
*/ |
41
|
|
|
private $requestStack; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ArgumentContextInterface |
45
|
|
|
*/ |
46
|
|
|
private $argumentContext; |
47
|
|
|
|
48
|
|
|
public function setUp() |
49
|
|
|
{ |
50
|
|
|
$this->argumentFactory = $this->createMock(ArgumentFactory::class); |
|
|
|
|
51
|
|
|
$this->requestStack = $this->createMock(RequestStack::class); |
|
|
|
|
52
|
|
|
$this->argumentContext = $this->createMock(ArgumentContextInterface::class); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$this->compiler = new NewArgumentCompiler( |
55
|
|
|
$this->argumentFactory, |
|
|
|
|
56
|
|
|
$this->requestStack, |
|
|
|
|
57
|
|
|
$this->argumentContext |
|
|
|
|
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
* @dataProvider dataProviderForShouldBuildArguments |
64
|
|
|
*/ |
65
|
|
|
public function shouldBuildArguments($expectedArguments, array $argumentsConfiguration) |
66
|
|
|
{ |
67
|
|
|
/** @var Request $request */ |
68
|
|
|
$request = $this->createMock(Request::class); |
69
|
|
|
|
70
|
|
|
$this->requestStack->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->argumentContext->expects($this->once())->method('clear'); |
|
|
|
|
73
|
|
|
$this->argumentContext->expects($this->once())->method('set')->with( |
|
|
|
|
74
|
|
|
$this->equalTo('foo'), |
75
|
|
|
$this->equalTo('bar') |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
/** @var Argument $argument */ |
79
|
|
|
$argument = $this->createMock(Argument::class); |
80
|
|
|
$argument->method('resolve')->willReturn('dolor'); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$this->argumentFactory->method('understandsString')->willReturn(true); |
|
|
|
|
83
|
|
|
$this->argumentFactory->method('createArgumentFromString')->willReturn($argument); |
|
|
|
|
84
|
|
|
$this->argumentFactory->method('understandsArray')->willReturn(true); |
|
|
|
|
85
|
|
|
$this->argumentFactory->method('createArgumentFromArray')->willReturn($argument); |
|
|
|
|
86
|
|
|
|
87
|
|
|
/** @var mixed $actualArguments */ |
88
|
|
|
$actualArguments = $this->compiler->buildArguments( |
89
|
|
|
$argumentsConfiguration, |
90
|
|
|
$request, |
91
|
|
|
['foo' => 'bar'] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->assertEquals($expectedArguments, $actualArguments); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function dataProviderForShouldBuildArguments(): array |
98
|
|
|
{ |
99
|
|
|
return array( |
100
|
|
|
[[], []], |
101
|
|
|
[['lorem' => 'dolor'], ['lorem' => '$ipsum']], |
102
|
|
|
[['lorem' => 'dolor'], ['lorem' => ['$ipsum']]], |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function shouldExpectArgumentFactoryToUnderstandString() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$this->expectExceptionMessage("Argument 'dolor' could not be understood!"); |
112
|
|
|
|
113
|
|
|
/** @var Request $request */ |
114
|
|
|
$request = $this->createMock(Request::class); |
115
|
|
|
|
116
|
|
|
$this->requestStack->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
117
|
|
|
|
118
|
|
|
/** @var Argument $argument */ |
119
|
|
|
$argument = $this->createMock(Argument::class); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$this->argumentFactory->method('understandsString')->willReturn(false); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->compiler->buildArguments(['lorem' => 'dolor'], $request, []); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @test |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function shouldExpectArgumentFactoryToUnderstandArray() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$this->expectExceptionMessage("Argument 'array(0=>'dolor',)' could not be understood!"); |
132
|
|
|
|
133
|
|
|
/** @var Request $request */ |
134
|
|
|
$request = $this->createMock(Request::class); |
135
|
|
|
|
136
|
|
|
$this->requestStack->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
137
|
|
|
|
138
|
|
|
/** @var Argument $argument */ |
139
|
|
|
$argument = $this->createMock(Argument::class); |
|
|
|
|
140
|
|
|
|
141
|
|
|
$this->argumentFactory->method('understandsString')->willReturn(false); |
|
|
|
|
142
|
|
|
|
143
|
|
|
$this->compiler->buildArguments(['lorem' => ['dolor']], $request, []); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @test |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function shouldExpectArgumentToBeArrayOrString() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$this->expectExceptionMessage("Arguments must be defined as string or array!"); |
152
|
|
|
|
153
|
|
|
/** @var Request $request */ |
154
|
|
|
$request = $this->createMock(Request::class); |
155
|
|
|
|
156
|
|
|
$this->requestStack->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
157
|
|
|
|
158
|
|
|
/** @var Argument $argument */ |
159
|
|
|
$argument = $this->createMock(Argument::class); |
|
|
|
|
160
|
|
|
|
161
|
|
|
$this->argumentFactory->method('understandsString')->willReturn(false); |
|
|
|
|
162
|
|
|
|
163
|
|
|
$this->compiler->buildArguments(['lorem' => 3.1415], $request, []); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @test |
168
|
|
|
* @dataProvider dataProviderForShouldBuildCallArguments |
169
|
|
|
*/ |
170
|
|
|
public function shouldBuildCallArguments($expectedArguments, array $parameters, array $argumentsConfiguration) |
171
|
|
|
{ |
172
|
|
|
/** @var Request $request */ |
173
|
|
|
$request = $this->createMock(Request::class); |
174
|
|
|
|
175
|
|
|
$this->requestStack->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
176
|
|
|
|
177
|
|
|
$this->argumentContext->expects($this->once())->method('clear'); |
|
|
|
|
178
|
|
|
$this->argumentContext->expects($this->once())->method('set')->with( |
|
|
|
|
179
|
|
|
$this->equalTo('foo'), |
180
|
|
|
$this->equalTo('bar') |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
/** @var ReflectionFunctionAbstract $routineReflection */ |
184
|
|
|
$routineReflection = $this->createMock(ReflectionFunctionAbstract::class); |
185
|
|
|
$routineReflection->method('getParameters')->willReturn($parameters); |
186
|
|
|
|
187
|
|
|
/** @var Argument $argument */ |
188
|
|
|
$argument = $this->createMock(Argument::class); |
189
|
|
|
$argument->method('resolve')->willReturn('dolor'); |
|
|
|
|
190
|
|
|
|
191
|
|
|
$this->argumentFactory->method('understandsString')->willReturn(true); |
|
|
|
|
192
|
|
|
$this->argumentFactory->method('createArgumentFromString')->willReturn($argument); |
|
|
|
|
193
|
|
|
$this->argumentFactory->method('understandsArray')->willReturn(true); |
|
|
|
|
194
|
|
|
$this->argumentFactory->method('createArgumentFromArray')->willReturn($argument); |
|
|
|
|
195
|
|
|
|
196
|
|
|
/** @var mixed $actualArguments */ |
197
|
|
|
$actualArguments = $this->compiler->buildCallArguments( |
198
|
|
|
$routineReflection, |
199
|
|
|
$argumentsConfiguration, |
200
|
|
|
$request, |
201
|
|
|
['abc' => 'def'], |
202
|
|
|
['foo' => 'bar'] |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$this->assertEquals($expectedArguments, $actualArguments); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function dataProviderForShouldBuildCallArguments() |
209
|
|
|
{ |
210
|
|
|
/** @var ReflectionParameter $blahParameter */ |
211
|
|
|
$blahParameter = $this->createMock(ReflectionParameter::class); |
212
|
|
|
$blahParameter->method('hasType')->willReturn(false); |
213
|
|
|
$blahParameter->method('getName')->willReturn("blah"); |
214
|
|
|
|
215
|
|
|
/** @var ReflectionType $requestParameterType */ |
216
|
|
|
$requestParameterType = $this->createMock(ReflectionType::class); |
217
|
|
|
$requestParameterType->method('__toString')->willReturn(Request::class); |
218
|
|
|
|
219
|
|
|
/** @var ReflectionParameter $requestParameter */ |
220
|
|
|
$requestParameter = $this->createMock(ReflectionParameter::class); |
221
|
|
|
$requestParameter->method('hasType')->willReturn(true); |
222
|
|
|
$requestParameter->method('getType')->willReturn($requestParameterType); |
223
|
|
|
$requestParameter->method('getName')->willReturn("blah"); |
224
|
|
|
|
225
|
|
|
return array( |
226
|
|
|
[[], [], []], |
227
|
|
|
[ |
228
|
|
|
['blah' => 'dolor', 'abc' => 'def'], |
229
|
|
|
['blah' => $blahParameter, 'abc' => $blahParameter], |
230
|
|
|
['blah' => '$ipsum'] |
231
|
|
|
], |
232
|
|
|
[ |
233
|
|
|
['blah' => $this->createMock(Request::class), 'abc' => 'def', 'blubb' => "dolor"], |
234
|
|
|
['blah' => $requestParameter, 'abc' => $blahParameter, 'blubb' => $blahParameter,], |
235
|
|
|
['blubb' => '$ipsum'] |
236
|
|
|
], |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @test |
242
|
|
|
*/ |
243
|
|
|
public function shouldCatchReflectionException() |
244
|
|
|
{ |
245
|
|
|
$this->expectExceptionMessage("Missing argument 'blah' for the call to 'doSomething'!"); |
246
|
|
|
|
247
|
|
|
/** @var Request $request */ |
248
|
|
|
$request = $this->createMock(Request::class); |
249
|
|
|
|
250
|
|
|
/** @var ReflectionParameter $parameterWithoutDefaultValue */ |
251
|
|
|
$parameterWithoutDefaultValue = $this->createMock(ReflectionParameter::class); |
252
|
|
|
$parameterWithoutDefaultValue->method('hasType')->willReturn(false); |
253
|
|
|
$parameterWithoutDefaultValue->method('getName')->willReturn("blah"); |
254
|
|
|
$parameterWithoutDefaultValue->method('getDefaultValue')->will($this->returnCallback( |
255
|
|
|
function () { |
256
|
|
|
throw new ReflectionException("We don't have a default value, bro!"); |
257
|
|
|
} |
258
|
|
|
)); |
259
|
|
|
|
260
|
|
|
/** @var ReflectionFunctionAbstract $routineReflection */ |
261
|
|
|
$routineReflection = $this->createMock(ReflectionFunctionAbstract::class); |
262
|
|
|
$routineReflection->method('getParameters')->willReturn([$parameterWithoutDefaultValue]); |
263
|
|
|
$routineReflection->method('getName')->willReturn("doSomething"); |
264
|
|
|
|
265
|
|
|
$this->compiler->buildCallArguments( |
266
|
|
|
$routineReflection, |
267
|
|
|
[], |
268
|
|
|
$request, |
269
|
|
|
[], |
270
|
|
|
[] |
271
|
|
|
); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..