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\SymfonyGenerics\Tests\Unit\Services; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompiler; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use Addiks\SymfonyGenerics\Services\EntityRepositoryInterface; |
17
|
|
|
use ReflectionMethod; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use ReflectionParameter; |
20
|
|
|
use ReflectionType; |
21
|
|
|
use stdClass; |
22
|
|
|
use InvalidArgumentException; |
23
|
|
|
use Serializable; |
24
|
|
|
use Addiks\SymfonyGenerics\Tests\Unit\Services\SampleService; |
25
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
26
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
27
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
28
|
|
|
use Symfony\Component\HttpFoundation\FileBag; |
29
|
|
|
|
30
|
|
|
final class ArgumentCompilerTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ArgumentCompiler |
35
|
|
|
*/ |
36
|
|
|
private $argumentCompiler; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ContainerInterface |
40
|
|
|
*/ |
41
|
|
|
private $container; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var EntityManagerInterface |
45
|
|
|
*/ |
46
|
|
|
private $entityManager; |
47
|
|
|
|
48
|
|
|
public function setUp() |
49
|
|
|
{ |
50
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
51
|
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$this->argumentCompiler = new ArgumentCompiler($this->container, $this->entityManager); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
*/ |
59
|
|
|
public function shouldBuildArguments() |
60
|
|
|
{ |
61
|
|
|
/** @var Request $request */ |
62
|
|
|
$request = $this->createMock(Request::class); |
63
|
|
|
$request->method('get')->will($this->returnValueMap([ |
|
|
|
|
64
|
|
|
['reqFoo', null, 'ipsum'], |
65
|
|
|
])); |
66
|
|
|
|
67
|
|
|
$someObject = new stdClass(); |
68
|
|
|
|
69
|
|
|
/** @var Serializable $someService */ |
70
|
|
|
$someService = $this->createMock(Serializable::class); |
71
|
|
|
$someService->method('serialize')->willReturn($someObject); |
72
|
|
|
|
73
|
|
|
$this->container->method('get')->will($this->returnValueMap([ |
|
|
|
|
74
|
|
|
['some.service', $someService], |
75
|
|
|
])); |
76
|
|
|
|
77
|
|
|
/** @var array<string, mixed> $expectedRouteArguments */ |
78
|
|
|
$expectedRouteArguments = array( |
79
|
|
|
'foo' => 'ipsum', |
80
|
|
|
'bar' => $someObject |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
/** @var array<string, mixed> $actualRouteArguments */ |
84
|
|
|
$actualRouteArguments = $this->argumentCompiler->buildArguments([ |
85
|
|
|
'foo' => '$reqFoo', |
86
|
|
|
'bar' => '@some.service::serialize', |
87
|
|
|
], $request); |
88
|
|
|
|
89
|
|
|
$this->assertEquals($expectedRouteArguments, $actualRouteArguments); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
*/ |
95
|
|
|
public function shouldBuildCallArguments() |
96
|
|
|
{ |
97
|
|
|
/** @var ReflectionMethod $methodReflection */ |
98
|
|
|
$methodReflection = $this->createMock(ReflectionMethod::class); |
99
|
|
|
|
100
|
|
|
/** @var ReflectionParameter $parameterFooReflection */ |
101
|
|
|
$parameterFooReflection = $this->createMock(ReflectionParameter::class); |
102
|
|
|
$parameterFooReflection->method('getName')->willReturn("foo"); |
103
|
|
|
|
104
|
|
|
/** @var ReflectionParameter $parameterBarReflection */ |
105
|
|
|
$parameterBarReflection = $this->createMock(ReflectionParameter::class); |
106
|
|
|
$parameterBarReflection->method('getName')->willReturn("bar"); |
107
|
|
|
|
108
|
|
|
/** @var ReflectionParameter $parameterBazReflection */ |
109
|
|
|
$parameterBazReflection = $this->createMock(ReflectionParameter::class); |
110
|
|
|
$parameterBazReflection->method('getName')->willReturn("baz"); |
111
|
|
|
|
112
|
|
|
/** @var ReflectionType $parameterType */ |
113
|
|
|
$parameterType = $this->createMock(ReflectionType::class); |
114
|
|
|
$parameterType->method('__toString')->willReturn(SampleService::class); |
115
|
|
|
|
116
|
|
|
/** @var ReflectionParameter $parameterFazReflection */ |
117
|
|
|
$parameterFazReflection = $this->createMock(ReflectionParameter::class); |
118
|
|
|
$parameterFazReflection->method('getName')->willReturn("faz"); |
119
|
|
|
$parameterFazReflection->method('hasType')->willReturn(true); |
120
|
|
|
$parameterFazReflection->method('getType')->willReturn($parameterType); |
121
|
|
|
|
122
|
|
|
$methodReflection->method("getParameters")->willReturn([ |
123
|
|
|
$parameterFooReflection, |
124
|
|
|
$parameterBarReflection, |
125
|
|
|
$parameterBazReflection, |
126
|
|
|
$parameterFazReflection |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
/** @var Request $request */ |
130
|
|
|
$request = $this->createMock(Request::class); |
131
|
|
|
$request->method('get')->will($this->returnValueMap([ |
|
|
|
|
132
|
|
|
['lorem', null, 'ipsum'], |
133
|
|
|
['bar', null, 'blah'], |
134
|
|
|
])); |
135
|
|
|
|
136
|
|
|
/** @var array<string, mixed> $argumentsConfiguration */ |
137
|
|
|
$argumentsConfiguration = array( |
138
|
|
|
"foo" => '$lorem', |
139
|
|
|
"baz" => '@some.service', |
140
|
|
|
"faz" => [ |
141
|
|
|
'service-id' => 'some.service', |
142
|
|
|
'method' => 'someCall', |
143
|
|
|
'arguments' => [ |
144
|
|
|
'foo' => '$lorem' |
145
|
|
|
] |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$someService = new SampleService(); |
150
|
|
|
|
151
|
|
|
$this->container->method('get')->will($this->returnValueMap([ |
|
|
|
|
152
|
|
|
['some.service', $someService], |
153
|
|
|
])); |
154
|
|
|
|
155
|
|
|
$this->entityManager->expects($this->once())->method('find')->with( |
|
|
|
|
156
|
|
|
$this->equalTo(SampleService::class), |
157
|
|
|
$this->equalTo('ipsum') |
158
|
|
|
)->willReturn($someService); |
159
|
|
|
|
160
|
|
|
/** @var array<int, mixed> $expectedCallArguments */ |
161
|
|
|
$expectedCallArguments = array( |
162
|
|
|
'ipsum', |
163
|
|
|
'blah', |
164
|
|
|
$someService, |
165
|
|
|
$someService |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
/** @var array<int, mixed> $actualCallArguments */ |
169
|
|
|
$actualCallArguments = $this->argumentCompiler->buildCallArguments( |
170
|
|
|
$methodReflection, |
171
|
|
|
$argumentsConfiguration, |
172
|
|
|
$request |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$this->assertEquals($expectedCallArguments, $actualCallArguments); |
176
|
|
|
$this->assertEquals('ipsum', $someService->foo); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @test |
181
|
|
|
*/ |
182
|
|
|
public function shouldRejectNonExistingFactoryMethod() |
183
|
|
|
{ |
184
|
|
|
$this->expectException(InvalidArgumentException::class); |
185
|
|
|
|
186
|
|
|
/** @var Request $request */ |
187
|
|
|
$request = $this->createMock(Request::class); |
188
|
|
|
|
189
|
|
|
/** @var Serializable $someService */ |
190
|
|
|
$someService = $this->createMock(Serializable::class); |
191
|
|
|
|
192
|
|
|
$this->container->method('get')->will($this->returnValueMap([ |
|
|
|
|
193
|
|
|
['some.service', $someService], |
194
|
|
|
])); |
195
|
|
|
|
196
|
|
|
$this->argumentCompiler->buildArguments([ |
197
|
|
|
'foo' => '$reqFoo', |
198
|
|
|
'bar' => '@some.service::doesNotExist', |
199
|
|
|
], $request); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @test |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
public function shouldRejectNonExistingAdditionalDataKey() |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
$this->expectException(InvalidArgumentException::class); |
208
|
|
|
|
209
|
|
|
/** @var Request $request */ |
210
|
|
|
$request = $this->createMock(Request::class); |
211
|
|
|
|
212
|
|
|
$this->argumentCompiler->buildArguments([ |
213
|
|
|
'foo' => '%keyFoo', |
214
|
|
|
], $request); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @test |
219
|
|
|
*/ |
220
|
|
View Code Duplication |
public function shouldGetAdditionalDataKey() |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
/** @var Request $request */ |
223
|
|
|
$request = $this->createMock(Request::class); |
224
|
|
|
|
225
|
|
|
/** @var array $actualResult */ |
226
|
|
|
$actualResult = $this->argumentCompiler->buildArguments([ |
227
|
|
|
'foo' => '%keyFoo', |
228
|
|
|
], $request, [ |
229
|
|
|
'keyFoo' => 'bar' |
230
|
|
|
]); |
231
|
|
|
|
232
|
|
|
$this->assertEquals(['foo' => 'bar'], $actualResult); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @test |
237
|
|
|
*/ |
238
|
|
View Code Duplication |
public function shouldRejectMissingMethodOnAdditionalDataKey() |
|
|
|
|
239
|
|
|
{ |
240
|
|
|
$this->expectException(InvalidArgumentException::class); |
241
|
|
|
|
242
|
|
|
/** @var Request $request */ |
243
|
|
|
$request = $this->createMock(Request::class); |
244
|
|
|
|
245
|
|
|
$this->argumentCompiler->buildArguments([ |
246
|
|
|
'foo' => '%keyFoo.doSomethingImpossible', |
247
|
|
|
], $request, [ |
248
|
|
|
'keyFoo' => $this->createMock(stdClass::class) |
249
|
|
|
]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @test |
254
|
|
|
*/ |
255
|
|
View Code Duplication |
public function shouldRejectMissingUploadedFile() |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$this->expectException(InvalidArgumentException::class); |
258
|
|
|
|
259
|
|
|
/** @var Request $request */ |
260
|
|
|
$request = $this->createMock(Request::class); |
261
|
|
|
$request->files = $this->createMock(FileBag::class); |
|
|
|
|
262
|
|
|
|
263
|
|
|
$this->argumentCompiler->buildArguments([ |
264
|
|
|
'foo' => '$files.something_missing.content', |
265
|
|
|
], $request); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @test |
270
|
|
|
*/ |
271
|
|
|
public function shouldGetUploadedFile() |
272
|
|
|
{ |
273
|
|
|
/** @var UploadedFile $file */ |
274
|
|
|
$file = $this->createMock(UploadedFile::class); |
275
|
|
|
$file->method('getClientOriginalName')->willReturn('some-original-name'); |
|
|
|
|
276
|
|
|
$file->method('getFilename')->willReturn('some-file-name'); |
|
|
|
|
277
|
|
|
$file->method('getPathname')->willReturn('data://,some-content'); |
|
|
|
|
278
|
|
|
$file->method('getMimeType')->willReturn('some-mime-type'); |
|
|
|
|
279
|
|
|
|
280
|
|
|
/** @var Request $request */ |
281
|
|
|
$request = $this->createMock(Request::class); |
282
|
|
|
$request->files = $this->createMock(FileBag::class); |
|
|
|
|
283
|
|
|
$request->files->expects($this->exactly(5))->method('get')->willReturn($file); |
284
|
|
|
|
285
|
|
|
/** @var array $actualResult */ |
286
|
|
|
$actualResult = $this->argumentCompiler->buildArguments([ |
287
|
|
|
'obj' => '$files.blah.object', |
288
|
|
|
'ogn' => '$files.blah.originalname', |
289
|
|
|
'fln' => '$files.blah.filename', |
290
|
|
|
'cnt' => '$files.blah.content', |
291
|
|
|
'mmt' => '$files.blah.mimetype', |
292
|
|
|
], $request); |
293
|
|
|
|
294
|
|
|
$this->assertEquals([ |
295
|
|
|
'obj' => $file, |
296
|
|
|
'ogn' => 'some-original-name', |
297
|
|
|
'fln' => 'some-file-name', |
298
|
|
|
'cnt' => 'some-content', |
299
|
|
|
'mmt' => 'some-mime-type', |
300
|
|
|
], $actualResult); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @test |
305
|
|
|
*/ |
306
|
|
|
public function shouldGetRequestPayload() |
307
|
|
|
{ |
308
|
|
|
/** @var Request $request */ |
309
|
|
|
$request = $this->createMock(Request::class); |
310
|
|
|
$request->expects($this->once())->method('getContent')->with( |
|
|
|
|
311
|
|
|
$this->equalTo(false) |
312
|
|
|
); |
313
|
|
|
|
314
|
|
|
$this->argumentCompiler->buildArguments([ |
315
|
|
|
'foo' => '$', |
316
|
|
|
], $request); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @test |
321
|
|
|
*/ |
322
|
|
View Code Duplication |
public function shouldResolveLiteralString() |
|
|
|
|
323
|
|
|
{ |
324
|
|
|
/** @var Request $request */ |
325
|
|
|
$request = $this->createMock(Request::class); |
326
|
|
|
|
327
|
|
|
/** @var array $actualResult */ |
328
|
|
|
$actualResult = $this->argumentCompiler->buildArguments([ |
329
|
|
|
'foo' => '\'bar\'', |
330
|
|
|
], $request); |
331
|
|
|
|
332
|
|
|
$this->assertEquals(['foo' => 'bar'], $actualResult); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @test |
337
|
|
|
*/ |
338
|
|
|
public function shouldThrowExceptionWhenFetchingUnknownService() |
339
|
|
|
{ |
340
|
|
|
$this->expectException(InvalidArgumentException::class); |
341
|
|
|
|
342
|
|
|
/** @var ReflectionMethod $methodReflection */ |
343
|
|
|
$methodReflection = $this->createMock(ReflectionMethod::class); |
344
|
|
|
|
345
|
|
|
/** @var ReflectionParameter $parameterFazReflection */ |
346
|
|
|
$parameterFazReflection = $this->createMock(ReflectionParameter::class); |
347
|
|
|
$parameterFazReflection->method('getName')->willReturn("faz"); |
348
|
|
|
|
349
|
|
|
$methodReflection->method("getParameters")->willReturn([ |
350
|
|
|
$parameterFazReflection |
351
|
|
|
]); |
352
|
|
|
|
353
|
|
|
/** @var Request $request */ |
354
|
|
|
$request = $this->createMock(Request::class); |
355
|
|
|
|
356
|
|
|
/** @var array<string, mixed> $argumentsConfiguration */ |
357
|
|
|
$argumentsConfiguration = array( |
358
|
|
|
"faz" => [ |
359
|
|
|
'service-id' => 'some.non-existing.service', |
360
|
|
|
] |
361
|
|
|
); |
362
|
|
|
|
363
|
|
|
/** @var array<int, mixed> $actualCallArguments */ |
364
|
|
|
$actualCallArguments = $this->argumentCompiler->buildCallArguments( |
|
|
|
|
365
|
|
|
$methodReflection, |
366
|
|
|
$argumentsConfiguration, |
367
|
|
|
$request |
368
|
|
|
); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @test |
373
|
|
|
*/ |
374
|
|
|
public function shouldThrowExceptionWhenCallArgumentIsMissing() |
375
|
|
|
{ |
376
|
|
|
$this->expectException(InvalidArgumentException::class); |
377
|
|
|
$this->expectExceptionMessage("Missing argument 'foo' for the call to 'someCall'!"); |
378
|
|
|
|
379
|
|
|
/** @var ReflectionMethod $methodReflection */ |
380
|
|
|
$methodReflection = $this->createMock(ReflectionMethod::class); |
381
|
|
|
|
382
|
|
|
/** @var ReflectionParameter $parameterFazReflection */ |
383
|
|
|
$parameterFazReflection = $this->createMock(ReflectionParameter::class); |
384
|
|
|
$parameterFazReflection->method('getName')->willReturn("faz"); |
385
|
|
|
|
386
|
|
|
$methodReflection->method("getParameters")->willReturn([ |
387
|
|
|
$parameterFazReflection |
388
|
|
|
]); |
389
|
|
|
|
390
|
|
|
/** @var Request $request */ |
391
|
|
|
$request = $this->createMock(Request::class); |
392
|
|
|
|
393
|
|
|
$someService = new SampleService(); |
394
|
|
|
|
395
|
|
|
$this->container->method('get')->will($this->returnValueMap([ |
|
|
|
|
396
|
|
|
['some.service', $someService], |
397
|
|
|
])); |
398
|
|
|
|
399
|
|
|
/** @var array<string, mixed> $argumentsConfiguration */ |
400
|
|
|
$argumentsConfiguration = array( |
401
|
|
|
"faz" => [ |
402
|
|
|
'service-id' => 'some.service', |
403
|
|
|
'method' => 'someCall', |
404
|
|
|
] |
405
|
|
|
); |
406
|
|
|
|
407
|
|
|
/** @var array<int, mixed> $actualCallArguments */ |
408
|
|
|
$actualCallArguments = $this->argumentCompiler->buildCallArguments( |
|
|
|
|
409
|
|
|
$methodReflection, |
410
|
|
|
$argumentsConfiguration, |
411
|
|
|
$request |
412
|
|
|
); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
} |
416
|
|
|
|
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..