Completed
Push — master ( c114e4...0fa665 )
by Eric
9s
created

CachedParameterResolverTest::testResolveCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\Tests\Routing;
13
14
use Lug\Bundle\ResourceBundle\Routing\CachedParameterResolver;
15
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface;
16
use Lug\Component\Resource\Model\ResourceInterface;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class CachedParameterResolverTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var CachedParameterResolver
25
     */
26
    private $cachedParameterResolver;
27
28
    /**
29
     * @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface
30
     */
31
    private $parameterResolver;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function setUp()
37
    {
38
        $this->parameterResolver = $this->createParameterResolverMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createParameterResolverMock() can also be of type object<Lug\Bundle\Resour...meterResolverInterface>. However, the property $parameterResolver is declared as type object<PHPUnit_Framework_MockObject_MockObject>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39
        $this->cachedParameterResolver = new CachedParameterResolver($this->parameterResolver);
40
    }
41
42
    public function testInheritance()
43
    {
44
        $this->assertInstanceOf(ParameterResolverInterface::class, $this->cachedParameterResolver);
45
    }
46
47 View Code Duplication
    public function testResolveApi()
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...
48
    {
49
        $this->parameterResolver
50
            ->expects($this->once())
51
            ->method('resolveApi')
52
            ->will($this->returnValue(true));
53
54
        $this->assertTrue($this->cachedParameterResolver->resolveApi());
55
        $this->assertTrue($this->cachedParameterResolver->resolveApi());
56
    }
57
58 View Code Duplication
    public function testResolveCriteria()
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...
59
    {
60
        $this->parameterResolver
61
            ->expects($this->once())
62
            ->method('resolveCriteria')
63
            ->with($this->identicalTo($mandatory = false))
64
            ->will($this->returnValue($criteria = ['criteria']));
65
66
        $this->assertSame($criteria, $this->cachedParameterResolver->resolveCriteria($mandatory));
67
        $this->assertSame($criteria, $this->cachedParameterResolver->resolveCriteria($mandatory));
68
    }
69
70
    public function testResolveCriteriaMissing()
71
    {
72
        $this->parameterResolver
73
            ->expects($this->at(0))
74
            ->method('resolveCriteria')
75
            ->with($this->identicalTo($firstMandatory = false))
76
            ->will($this->returnValue($criteria = []));
77
78
        $this->parameterResolver
79
            ->expects($this->at(1))
80
            ->method('resolveCriteria')
81
            ->with($this->identicalTo($secondMandatory = true))
82
            ->will($this->throwException($exception = new \Exception()));
83
84
        $this->assertSame($criteria, $this->cachedParameterResolver->resolveCriteria($firstMandatory));
85
86
        try {
87
            $this->cachedParameterResolver->resolveCriteria($secondMandatory);
88
        } catch (\Exception $e) {
89
            $this->assertSame($exception, $e);
90
        }
91
    }
92
93 View Code Duplication
    public function testResolveCurrentPage()
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...
94
    {
95
        $this->parameterResolver
96
            ->expects($this->once())
97
            ->method('resolveCurrentPage')
98
            ->will($this->returnValue($currentPage = 2));
99
100
        $this->assertSame($currentPage, $this->cachedParameterResolver->resolveCurrentPage());
101
        $this->assertSame($currentPage, $this->cachedParameterResolver->resolveCurrentPage());
102
    }
103
104 View Code Duplication
    public function testResolveForm()
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...
105
    {
106
        $this->parameterResolver
107
            ->expects($this->once())
108
            ->method('resolveForm')
109
            ->with($this->identicalTo($resource = $this->createResourceMock()))
110
            ->will($this->returnValue($form = 'form'));
111
112
        $this->assertSame($form, $this->cachedParameterResolver->resolveForm($resource));
0 ignored issues
show
Bug introduced by
It seems like $resource defined by $this->createResourceMock() on line 109 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...Resolver::resolveForm() does only seem to accept object<Lug\Component\Res...odel\ResourceInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
113
        $this->assertSame($form, $this->cachedParameterResolver->resolveForm($resource));
0 ignored issues
show
Bug introduced by
It seems like $resource defined by $this->createResourceMock() on line 109 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...Resolver::resolveForm() does only seem to accept object<Lug\Component\Res...odel\ResourceInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
114
    }
115
116 View Code Duplication
    public function testResolveGrid()
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...
117
    {
118
        $this->parameterResolver
119
            ->expects($this->once())
120
            ->method('resolveGrid')
121
            ->with($this->identicalTo($resource = $this->createResourceMock()))
122
            ->will($this->returnValue($grid = 'grid'));
123
124
        $this->assertSame($grid, $this->cachedParameterResolver->resolveGrid($resource));
0 ignored issues
show
Bug introduced by
It seems like $resource defined by $this->createResourceMock() on line 121 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...Resolver::resolveGrid() does only seem to accept object<Lug\Component\Res...odel\ResourceInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
125
        $this->assertSame($grid, $this->cachedParameterResolver->resolveGrid($resource));
0 ignored issues
show
Bug introduced by
It seems like $resource defined by $this->createResourceMock() on line 121 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...Resolver::resolveGrid() does only seem to accept object<Lug\Component\Res...odel\ResourceInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
126
    }
127
128 View Code Duplication
    public function testResolveHateoas()
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...
129
    {
130
        $this->parameterResolver
131
            ->expects($this->once())
132
            ->method('resolveHateoas')
133
            ->will($this->returnValue(true));
134
135
        $this->assertTrue($this->cachedParameterResolver->resolveHateoas());
136
        $this->assertTrue($this->cachedParameterResolver->resolveHateoas());
137
    }
138
139 View Code Duplication
    public function testResolveLocationRoute()
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...
140
    {
141
        $this->parameterResolver
142
            ->expects($this->once())
143
            ->method('resolveLocationRoute')
144
            ->will($this->returnValue($locationRoute = 'location_route'));
145
146
        $this->assertSame($locationRoute, $this->cachedParameterResolver->resolveLocationRoute());
147
        $this->assertSame($locationRoute, $this->cachedParameterResolver->resolveLocationRoute());
148
    }
149
150 View Code Duplication
    public function testResolveLocationRouteParameters()
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...
151
    {
152
        $this->parameterResolver
153
            ->expects($this->once())
154
            ->method('resolveLocationRouteParameters')
155
            ->with($this->identicalTo($object = new \stdClass()))
156
            ->will($this->returnValue($locationRouteParameters = ['location_route_param']));
157
158
        $this->assertSame(
159
            $locationRouteParameters,
160
            $this->cachedParameterResolver->resolveLocationRouteParameters($object)
161
        );
162
163
        $this->assertSame(
164
            $locationRouteParameters,
165
            $this->cachedParameterResolver->resolveLocationRouteParameters($object)
166
        );
167
    }
168
169 View Code Duplication
    public function testResolveMaxPerPage()
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...
170
    {
171
        $this->parameterResolver
172
            ->expects($this->once())
173
            ->method('resolveMaxPerPage')
174
            ->will($this->returnValue($maxPerPage = 5));
175
176
        $this->assertSame($maxPerPage, $this->cachedParameterResolver->resolveMaxPerPage());
177
        $this->assertSame($maxPerPage, $this->cachedParameterResolver->resolveMaxPerPage());
178
    }
179
180 View Code Duplication
    public function testResolveRedirectRoute()
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...
181
    {
182
        $this->parameterResolver
183
            ->expects($this->once())
184
            ->method('resolveRedirectRoute')
185
            ->will($this->returnValue($redirectRoute = 'redirect_route'));
186
187
        $this->assertSame($redirectRoute, $this->cachedParameterResolver->resolveRedirectRoute());
188
        $this->assertSame($redirectRoute, $this->cachedParameterResolver->resolveRedirectRoute());
189
    }
190
191
    public function testResolveRedirectRouteParameters()
192
    {
193
        $this->parameterResolver
194
            ->expects($this->once())
195
            ->method('resolveRedirectRouteParameters')
196
            ->with(
197
                $this->identicalTo($object = new \stdClass()),
198
                $this->identicalTo($forwardParameters = true)
199
            )
200
            ->will($this->returnValue($redirectRouteParameters = ['redirect_route_param']));
201
202
        $this->assertSame(
203
            $redirectRouteParameters,
204
            $this->cachedParameterResolver->resolveRedirectRouteParameters($object, $forwardParameters)
205
        );
206
207
        $this->assertSame(
208
            $redirectRouteParameters,
209
            $this->cachedParameterResolver->resolveRedirectRouteParameters($object, $forwardParameters)
210
        );
211
    }
212
213 View Code Duplication
    public function testResolveRedirectRouteParametersForward()
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...
214
    {
215
        $this->parameterResolver
216
            ->expects($this->once())
217
            ->method('resolveRedirectRouteParametersForward')
218
            ->will($this->returnValue(true));
219
220
        $this->assertTrue($this->cachedParameterResolver->resolveRedirectRouteParametersForward());
221
        $this->assertTrue($this->cachedParameterResolver->resolveRedirectRouteParametersForward());
222
    }
223
224 View Code Duplication
    public function testResolveRepositoryMethod()
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...
225
    {
226
        $this->parameterResolver
227
            ->expects($this->once())
228
            ->method('resolveRepositoryMethod')
229
            ->with($this->identicalTo($action = 'action'))
230
            ->will($this->returnValue($repositoryMethod = 'repository_method'));
231
232
        $this->assertSame($repositoryMethod, $this->cachedParameterResolver->resolveRepositoryMethod($action));
233
        $this->assertSame($repositoryMethod, $this->cachedParameterResolver->resolveRepositoryMethod($action));
234
    }
235
236 View Code Duplication
    public function testResolveSerializerGroups()
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->parameterResolver
239
            ->expects($this->once())
240
            ->method('resolveSerializerGroups')
241
            ->will($this->returnValue($groups = ['group']));
242
243
        $this->assertSame($groups, $this->cachedParameterResolver->resolveSerializerGroups());
244
        $this->assertSame($groups, $this->cachedParameterResolver->resolveSerializerGroups());
245
    }
246
247 View Code Duplication
    public function testResolveSerializerNull()
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...
248
    {
249
        $this->parameterResolver
250
            ->expects($this->once())
251
            ->method('resolveSerializerNull')
252
            ->will($this->returnValue(true));
253
254
        $this->assertTrue($this->cachedParameterResolver->resolveSerializerNull());
255
        $this->assertTrue($this->cachedParameterResolver->resolveSerializerNull());
256
    }
257
258 View Code Duplication
    public function testResolveSorting()
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...
259
    {
260
        $this->parameterResolver
261
            ->expects($this->once())
262
            ->method('resolveSorting')
263
            ->will($this->returnValue($sorting = ['sorting']));
264
265
        $this->assertSame($sorting, $this->cachedParameterResolver->resolveSorting());
266
        $this->assertSame($sorting, $this->cachedParameterResolver->resolveSorting());
267
    }
268
269 View Code Duplication
    public function testResolveTemplate()
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...
270
    {
271
        $this->parameterResolver
272
            ->expects($this->once())
273
            ->method('resolveTemplate')
274
            ->will($this->returnValue($template = 'template'));
275
276
        $this->assertSame($template, $this->cachedParameterResolver->resolveTemplate());
277
        $this->assertSame($template, $this->cachedParameterResolver->resolveTemplate());
278
    }
279
280 View Code Duplication
    public function testResolveThemes()
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...
281
    {
282
        $this->parameterResolver
283
            ->expects($this->once())
284
            ->method('resolveThemes')
285
            ->will($this->returnValue($themes = ['theme']));
286
287
        $this->assertSame($themes, $this->cachedParameterResolver->resolveThemes());
288
        $this->assertSame($themes, $this->cachedParameterResolver->resolveThemes());
289
    }
290
291 View Code Duplication
    public function testResolveTranslationDomain()
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...
292
    {
293
        $this->parameterResolver
294
            ->expects($this->once())
295
            ->method('resolveTranslationDomain')
296
            ->will($this->returnValue($translationDomain = 'translation_domain'));
297
298
        $this->assertSame($translationDomain, $this->cachedParameterResolver->resolveTranslationDomain());
299
        $this->assertSame($translationDomain, $this->cachedParameterResolver->resolveTranslationDomain());
300
    }
301
302 View Code Duplication
    public function testResolveValidationGroups()
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...
303
    {
304
        $this->parameterResolver
305
            ->expects($this->once())
306
            ->method('resolveValidationGroups')
307
            ->will($this->returnValue($groups = ['group']));
308
309
        $this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups());
310
        $this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups());
311
    }
312
313 View Code Duplication
    public function testResolveVoter()
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...
314
    {
315
        $this->parameterResolver
316
            ->expects($this->once())
317
            ->method('resolveVoter')
318
            ->will($this->returnValue(true));
319
320
        $this->assertTrue($this->cachedParameterResolver->resolveVoter());
321
        $this->assertTrue($this->cachedParameterResolver->resolveVoter());
322
    }
323
324
    /**
325
     * @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface
326
     */
327
    private function createParameterResolverMock()
328
    {
329
        return $this->getMock(ParameterResolverInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
330
    }
331
332
    /**
333
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
334
     */
335
    private function createResourceMock()
336
    {
337
        return $this->getMock(ResourceInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
338
    }
339
}
340