Passed
Push — master ( 3f1e18...b03604 )
by Gerrit
05:04
created

ArgumentCompilerTest::shouldBuildArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
28
final class ArgumentCompilerTest extends TestCase
29
{
30
31
    /**
32
     * @var ArgumentCompiler
33
     */
34
    private $argumentCompiler;
35
36
    /**
37
     * @var ContainerInterface
38
     */
39
    private $container;
40
41
    /**
42
     * @var EntityManagerInterface
43
     */
44
    private $entityManager;
45
46
    public function setUp()
47
    {
48
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Psr\C...tainerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Psr\Container\ContainerInterface> of property $container.

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..

Loading history...
49
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...anagerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManagerInterface> of property $entityManager.

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..

Loading history...
50
51
        $this->argumentCompiler = new ArgumentCompiler($this->container, $this->entityManager);
0 ignored issues
show
Documentation introduced by
$this->container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->entityManager is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function shouldBuildArguments()
58
    {
59
        /** @var Request $request */
60
        $request = $this->createMock(Request::class);
61
        $request->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not exist on Symfony\Component\HttpFoundation\Request. Did you maybe mean enableHttpMethodParameterOverride()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
            ['reqFoo', null, 'ipsum'],
63
        ]));
64
65
        $someObject = new stdClass();
66
67
        /** @var Serializable $someService */
68
        $someService = $this->createMock(Serializable::class);
69
        $someService->method('serialize')->willReturn($someObject);
70
71
        $this->container->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Psr\Container\ContainerInterface>.

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.

Loading history...
72
            ['some.service', $someService],
73
        ]));
74
75
        /** @var array<string, mixed> $expectedRouteArguments */
76
        $expectedRouteArguments = array(
77
            'foo' => 'ipsum',
78
            'bar' => $someObject
79
        );
80
81
        /** @var array<string, mixed> $actualRouteArguments */
82
        $actualRouteArguments = $this->argumentCompiler->buildArguments([
83
            'foo' => '$reqFoo',
84
            'bar' => '@some.service::serialize',
85
        ], $request);
86
87
        $this->assertEquals($expectedRouteArguments, $actualRouteArguments);
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function shouldBuildCallArguments()
94
    {
95
        /** @var ReflectionMethod $methodReflection */
96
        $methodReflection = $this->createMock(ReflectionMethod::class);
97
98
        /** @var ReflectionParameter $parameterFooReflection */
99
        $parameterFooReflection = $this->createMock(ReflectionParameter::class);
100
        $parameterFooReflection->method('getName')->willReturn("foo");
101
102
        /** @var ReflectionParameter $parameterBarReflection */
103
        $parameterBarReflection = $this->createMock(ReflectionParameter::class);
104
        $parameterBarReflection->method('getName')->willReturn("bar");
105
106
        /** @var ReflectionParameter $parameterBazReflection */
107
        $parameterBazReflection = $this->createMock(ReflectionParameter::class);
108
        $parameterBazReflection->method('getName')->willReturn("baz");
109
110
        /** @var ReflectionType $parameterType */
111
        $parameterType = $this->createMock(ReflectionType::class);
112
        $parameterType->method('__toString')->willReturn(SampleService::class);
113
114
        /** @var ReflectionParameter $parameterFazReflection */
115
        $parameterFazReflection = $this->createMock(ReflectionParameter::class);
116
        $parameterFazReflection->method('getName')->willReturn("faz");
117
        $parameterFazReflection->method('hasType')->willReturn(true);
118
        $parameterFazReflection->method('getType')->willReturn($parameterType);
119
120
        $methodReflection->method("getParameters")->willReturn([
121
            $parameterFooReflection,
122
            $parameterBarReflection,
123
            $parameterBazReflection,
124
            $parameterFazReflection
125
        ]);
126
127
        /** @var Request $request */
128
        $request = $this->createMock(Request::class);
129
        $request->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not exist on Symfony\Component\HttpFoundation\Request. Did you maybe mean enableHttpMethodParameterOverride()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
130
            ['lorem', null, 'ipsum'],
131
            ['bar', null, 'blah'],
132
        ]));
133
134
        /** @var array<string, mixed> $argumentsConfiguration */
135
        $argumentsConfiguration = array(
136
            "foo" => '$lorem',
137
            "baz" => '@some.service',
138
            "faz" => [
139
                'service-id' => 'some.service',
140
                'method' => 'someCall',
141
                'arguments' => [
142
                    'foo' => '$lorem'
143
                ]
144
            ]
145
        );
146
147
        $someService = new SampleService();
148
149
        $this->container->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Psr\Container\ContainerInterface>.

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.

Loading history...
150
            ['some.service', $someService],
151
        ]));
152
153
        $this->entityManager->expects($this->once())->method('find')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ORM\EntityManagerInterface>.

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.

Loading history...
154
            $this->equalTo(SampleService::class),
155
            $this->equalTo('ipsum')
156
        )->willReturn($someService);
157
158
        /** @var array<int, mixed> $expectedCallArguments */
159
        $expectedCallArguments = array(
160
            'ipsum',
161
            'blah',
162
            $someService,
163
            $someService
164
        );
165
166
        /** @var array<int, mixed> $actualCallArguments */
167
        $actualCallArguments = $this->argumentCompiler->buildCallArguments(
168
            $methodReflection,
169
            $argumentsConfiguration,
170
            $request
171
        );
172
173
        $this->assertEquals($expectedCallArguments, $actualCallArguments);
174
        $this->assertEquals('ipsum', $someService->foo);
175
    }
176
177
    /**
178
     * @test
179
     */
180
    public function shouldRejectNonExistingFactoryMethod()
181
    {
182
        $this->expectException(InvalidArgumentException::class);
183
184
        /** @var Request $request */
185
        $request = $this->createMock(Request::class);
186
187
        /** @var Serializable $someService */
188
        $someService = $this->createMock(Serializable::class);
189
190
        $this->container->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Psr\Container\ContainerInterface>.

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.

Loading history...
191
            ['some.service', $someService],
192
        ]));
193
194
        $this->argumentCompiler->buildArguments([
195
            'foo' => '$reqFoo',
196
            'bar' => '@some.service::doesNotExist',
197
        ], $request);
198
    }
199
200
    /**
201
     * @test
202
     */
203 View Code Duplication
    public function shouldRejectNonExistingAdditionalDataKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        $this->expectException(InvalidArgumentException::class);
206
207
        /** @var Request $request */
208
        $request = $this->createMock(Request::class);
209
210
        $this->argumentCompiler->buildArguments([
211
            'foo' => '%keyFoo',
212
        ], $request);
213
    }
214
215
    /**
216
     * @test
217
     */
218 View Code Duplication
    public function shouldGetAdditionalDataKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
    {
220
        /** @var Request $request */
221
        $request = $this->createMock(Request::class);
222
223
        /** @var array $actualResult */
224
        $actualResult = $this->argumentCompiler->buildArguments([
225
            'foo' => '%keyFoo',
226
        ], $request, [
227
            'keyFoo' => 'bar'
228
        ]);
229
230
        $this->assertEquals(['foo' => 'bar'], $actualResult);
231
    }
232
233
    /**
234
     * @test
235
     */
236 View Code Duplication
    public function shouldRejectMissingMethodOnAdditionalDataKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    {
238
        $this->expectException(InvalidArgumentException::class);
239
240
        /** @var Request $request */
241
        $request = $this->createMock(Request::class);
242
243
        $this->argumentCompiler->buildArguments([
244
            'foo' => '%keyFoo.doSomethingImpossible',
245
        ], $request, [
246
            'keyFoo' => $this->createMock(stdClass::class)
247
        ]);
248
    }
249
250
    /**
251
     * @test
252
     */
253 View Code Duplication
    public function shouldRejectMissingUploadedFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
    {
255
        $this->expectException(InvalidArgumentException::class);
256
257
        /** @var Request $request */
258
        $request = $this->createMock(Request::class);
259
        $request->files = $this->createMock(ParameterBagInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...terBagInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component\HttpFoundation\FileBag> of property $files.

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..

Loading history...
260
261
        $this->argumentCompiler->buildArguments([
262
            'foo' => '$files.something_missing.content',
263
        ], $request);
264
    }
265
266
    /**
267
     * @test
268
     */
269
    public function shouldGetRequestPayload()
270
    {
271
        /** @var Request $request */
272
        $request = $this->createMock(Request::class);
273
        $request->expects($this->once())->method('getContent')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\HttpFoundation\Request>.

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.

Loading history...
274
            $this->equalTo(false)
275
        );
276
277
        $this->argumentCompiler->buildArguments([
278
            'foo' => '$',
279
        ], $request);
280
    }
281
282
    /**
283
     * @test
284
     */
285 View Code Duplication
    public function shouldResolveLiteralString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
286
    {
287
        /** @var Request $request */
288
        $request = $this->createMock(Request::class);
289
290
        /** @var array $actualResult */
291
        $actualResult = $this->argumentCompiler->buildArguments([
292
            'foo' => '\'bar\'',
293
        ], $request);
294
295
        $this->assertEquals(['foo' => 'bar'], $actualResult);
296
    }
297
298
    /**
299
     * @test
300
     */
301
    public function shouldThrowExceptionWhenFetchingUnknownService()
302
    {
303
        $this->expectException(InvalidArgumentException::class);
304
305
        /** @var ReflectionMethod $methodReflection */
306
        $methodReflection = $this->createMock(ReflectionMethod::class);
307
308
        /** @var ReflectionParameter $parameterFazReflection */
309
        $parameterFazReflection = $this->createMock(ReflectionParameter::class);
310
        $parameterFazReflection->method('getName')->willReturn("faz");
311
312
        $methodReflection->method("getParameters")->willReturn([
313
            $parameterFazReflection
314
        ]);
315
316
        /** @var Request $request */
317
        $request = $this->createMock(Request::class);
318
319
        /** @var array<string, mixed> $argumentsConfiguration */
320
        $argumentsConfiguration = array(
321
            "faz" => [
322
                'service-id' => 'some.non-existing.service',
323
            ]
324
        );
325
326
        /** @var array<int, mixed> $actualCallArguments */
327
        $actualCallArguments = $this->argumentCompiler->buildCallArguments(
0 ignored issues
show
Unused Code introduced by
$actualCallArguments is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
328
            $methodReflection,
329
            $argumentsConfiguration,
330
            $request
331
        );
332
    }
333
334
    /**
335
     * @test
336
     */
337
    public function shouldThrowExceptionWhenCallArgumentIsMissing()
338
    {
339
        $this->expectException(InvalidArgumentException::class);
340
        $this->expectExceptionMessage("Missing argument 'foo' for the call to 'someCall'!");
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
        $someService = new SampleService();
357
358
        $this->container->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Psr\Container\ContainerInterface>.

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.

Loading history...
359
            ['some.service', $someService],
360
        ]));
361
362
        /** @var array<string, mixed> $argumentsConfiguration */
363
        $argumentsConfiguration = array(
364
            "faz" => [
365
                'service-id' => 'some.service',
366
                'method' => 'someCall',
367
            ]
368
        );
369
370
        /** @var array<int, mixed> $actualCallArguments */
371
        $actualCallArguments = $this->argumentCompiler->buildCallArguments(
0 ignored issues
show
Unused Code introduced by
$actualCallArguments is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
372
            $methodReflection,
373
            $argumentsConfiguration,
374
            $request
375
        );
376
    }
377
378
}
379