Passed
Push — master ( 9fb777...c455df )
by Gerrit
01:50
created

shouldBuildCallArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...ArgumentFactory::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...actory\ArgumentFactory> of property $argumentFactory.

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...
51
        $this->requestStack = $this->createMock(RequestStack::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...on\RequestStack::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...oundation\RequestStack> of property $requestStack.

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...
52
        $this->argumentContext = $this->createMock(ArgumentContextInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...ontextInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...gumentContextInterface> of property $argumentContext.

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...
53
54
        $this->compiler = new NewArgumentCompiler(
55
            $this->argumentFactory,
0 ignored issues
show
Documentation introduced by
$this->argumentFactory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
56
            $this->requestStack,
0 ignored issues
show
Documentation introduced by
$this->requestStack is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...oundation\RequestStack>.

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...
57
            $this->argumentContext
0 ignored issues
show
Documentation introduced by
$this->argumentContext is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...gumentContextInterface>.

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...
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);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

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...
71
72
        $this->argumentContext->expects($this->once())->method('clear');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...gumentContextInterface>.

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...
73
        $this->argumentContext->expects($this->once())->method('set')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...gumentContextInterface>.

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...
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');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
81
82
        $this->argumentFactory->method('understandsString')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
83
        $this->argumentFactory->method('createArgumentFromString')->willReturn($argument);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
84
        $this->argumentFactory->method('understandsArray')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
85
        $this->argumentFactory->method('createArgumentFromArray')->willReturn($argument);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
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()
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...
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);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

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...
117
118
        /** @var Argument $argument */
119
        $argument = $this->createMock(Argument::class);
0 ignored issues
show
Unused Code introduced by
$argument 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...
120
121
        $this->argumentFactory->method('understandsString')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
122
123
        $this->compiler->buildArguments(['lorem' => 'dolor'], $request, []);
124
    }
125
126
    /**
127
     * @test
128
     */
129 View Code Duplication
    public function shouldExpectArgumentFactoryToUnderstandArray()
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...
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);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

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...
137
138
        /** @var Argument $argument */
139
        $argument = $this->createMock(Argument::class);
0 ignored issues
show
Unused Code introduced by
$argument 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...
140
141
        $this->argumentFactory->method('understandsString')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
142
143
        $this->compiler->buildArguments(['lorem' => ['dolor']], $request, []);
144
    }
145
146
    /**
147
     * @test
148
     */
149 View Code Duplication
    public function shouldExpectArgumentToBeArrayOrString()
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...
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);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

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...
157
158
        /** @var Argument $argument */
159
        $argument = $this->createMock(Argument::class);
0 ignored issues
show
Unused Code introduced by
$argument 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...
160
161
        $this->argumentFactory->method('understandsString')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
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);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

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...
176
177
        $this->argumentContext->expects($this->once())->method('clear');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...gumentContextInterface>.

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...
178
        $this->argumentContext->expects($this->once())->method('set')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...gumentContextInterface>.

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...
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');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
190
191
        $this->argumentFactory->method('understandsString')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
192
        $this->argumentFactory->method('createArgumentFromString')->willReturn($argument);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
193
        $this->argumentFactory->method('understandsArray')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
194
        $this->argumentFactory->method('createArgumentFromArray')->willReturn($argument);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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