Completed
Pull Request — master (#41)
by Eric
19:26 queued 11:35
created

ResourceViewSubscriberTest::createSerializationContextMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\Rest\View;
13
14
use FOS\RestBundle\Context\Context;
15
use FOS\RestBundle\View\View;
16
use Lug\Bundle\ResourceBundle\Rest\RestEvents;
17
use Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber\ResourceViewSubscriber;
18
use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent;
19
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface;
20
use Lug\Component\Resource\Model\ResourceInterface;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
23
/**
24
 * @author GeLo <[email protected]>
25
 */
26
class ResourceViewSubscriberTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * @var ResourceViewSubscriber
30
     */
31
    private $subscriber;
32
33
    /**
34
     * @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface
35
     */
36
    private $parameterResolver;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function setUp()
42
    {
43
        $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...
44
45
        $this->subscriber = new ResourceViewSubscriber($this->parameterResolver);
46
    }
47
48
    public function testInheritance()
49
    {
50
        $this->assertInstanceOf(EventSubscriberInterface::class, $this->subscriber);
51
    }
52
53 View Code Duplication
    public function testSubscribedEvents()
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...
54
    {
55
        $this->assertSame(
56
            [RestEvents::VIEW => [
57
                ['onApi', -4000],
58
                ['onView', -4000],
59
            ]],
60
            $this->subscriber->getSubscribedEvents()
61
        );
62
    }
63
64 View Code Duplication
    public function testApiWithoutApi()
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...
65
    {
66
        $this->parameterResolver
67
            ->expects($this->once())
68
            ->method('resolveApi')
69
            ->will($this->returnValue(false));
70
71
        $event = $this->createViewEventMock();
72
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Bundle\ResourceBundle\Rest\View\ViewEvent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73
            ->expects($this->never())
74
            ->method('getView');
75
76
        $this->subscriber->onApi($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createViewEventMock() on line 71 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...ViewSubscriber::onApi() does only seem to accept object<Lug\Bundle\Resour...le\Rest\View\ViewEvent>, 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...
77
    }
78
79
    public function testApi()
80
    {
81
        $this->parameterResolver
82
            ->expects($this->once())
83
            ->method('resolveApi')
84
            ->will($this->returnValue(true));
85
86
        $this->parameterResolver
87
            ->expects($this->once())
88
            ->method('resolveSerializerGroups')
89
            ->with($this->identicalTo($resource = $this->createResourceMock()))
90
            ->will($this->returnValue($groups = ['group']));
91
92
        $this->parameterResolver
93
            ->expects($this->once())
94
            ->method('resolveSerializerNull')
95
            ->will($this->returnValue($null = true));
96
97
        $event = $this->createViewEventMock();
98
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Bundle\ResourceBundle\Rest\View\ViewEvent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
99
            ->expects($this->once())
100
            ->method('getView')
101
            ->will($this->returnValue($view = $this->createViewMock()));
102
103
        $event
104
            ->expects($this->once())
105
            ->method('getResource')
106
            ->will($this->returnValue($resource));
107
108
        $view
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\RestBundle\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
109
            ->expects($this->exactly(2))
110
            ->method('getContext')
111
            ->will($this->returnValue($context = new Context()));
112
113
        $this->subscriber->onApi($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createViewEventMock() on line 97 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...ViewSubscriber::onApi() does only seem to accept object<Lug\Bundle\Resour...le\Rest\View\ViewEvent>, 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
        $this->assertSame($groups, $context->getGroups());
116
        $this->assertSame($null, $context->getSerializeNull());
117
    }
118
119 View Code Duplication
    public function testViewWithApi()
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...
120
    {
121
        $this->parameterResolver
122
            ->expects($this->once())
123
            ->method('resolveApi')
124
            ->will($this->returnValue(true));
125
126
        $event = $this->createViewEventMock();
127
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Bundle\ResourceBundle\Rest\View\ViewEvent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
128
            ->expects($this->never())
129
            ->method('getView');
130
131
        $this->subscriber->onView($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createViewEventMock() on line 126 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...iewSubscriber::onView() does only seem to accept object<Lug\Bundle\Resour...le\Rest\View\ViewEvent>, 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...
132
    }
133
134 View Code Duplication
    public function testViewWithoutTemplateVar()
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...
135
    {
136
        $this->parameterResolver
137
            ->expects($this->once())
138
            ->method('resolveApi')
139
            ->will($this->returnValue(false));
140
141
        $event = $this->createViewEventMock();
142
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Bundle\ResourceBundle\Rest\View\ViewEvent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
143
            ->expects($this->once())
144
            ->method('getView')
145
            ->will($this->returnValue($view = $this->createViewMock()));
146
147
        $view
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\RestBundle\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
148
            ->expects($this->once())
149
            ->method('getData')
150
            ->will($this->returnValue($data = new \stdClass()));
151
152
        $this->parameterResolver
153
            ->expects($this->once())
154
            ->method('resolveTemplate')
155
            ->will($this->returnValue($template = 'template'));
156
157
        $view
158
            ->expects($this->once())
159
            ->method('setTemplate')
160
            ->with($this->identicalTo($template))
161
            ->will($this->returnSelf());
162
163
        $event
164
            ->expects($this->once())
165
            ->method('getResource')
166
            ->will($this->returnValue($resource = $this->createResourceMock()));
167
168
        $view
169
            ->expects($this->once())
170
            ->method('getTemplateVar')
171
            ->will($this->returnValue(null));
172
173
        $view
174
            ->expects($this->once())
175
            ->method('setData')
176
            ->with($this->identicalTo(['data' => $data, 'resource' => $resource]));
177
178
        $this->subscriber->onView($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createViewEventMock() on line 141 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...iewSubscriber::onView() does only seem to accept object<Lug\Bundle\Resour...le\Rest\View\ViewEvent>, 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...
179
    }
180
181 View Code Duplication
    public function testViewWithTemplateVar()
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...
182
    {
183
        $this->parameterResolver
184
            ->expects($this->once())
185
            ->method('resolveApi')
186
            ->will($this->returnValue(false));
187
188
        $event = $this->createViewEventMock();
189
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Bundle\ResourceBundle\Rest\View\ViewEvent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
190
            ->expects($this->once())
191
            ->method('getView')
192
            ->will($this->returnValue($view = $this->createViewMock()));
193
194
        $view
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\RestBundle\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
195
            ->expects($this->once())
196
            ->method('getData')
197
            ->will($this->returnValue($data = new \stdClass()));
198
199
        $this->parameterResolver
200
            ->expects($this->once())
201
            ->method('resolveTemplate')
202
            ->will($this->returnValue($template = 'template'));
203
204
        $view
205
            ->expects($this->once())
206
            ->method('setTemplate')
207
            ->with($this->identicalTo($template))
208
            ->will($this->returnSelf());
209
210
        $event
211
            ->expects($this->once())
212
            ->method('getResource')
213
            ->will($this->returnValue($resource = $this->createResourceMock()));
214
215
        $view
216
            ->expects($this->once())
217
            ->method('getTemplateVar')
218
            ->will($this->returnValue($templateVar = 'template_var'));
219
220
        $view
221
            ->expects($this->once())
222
            ->method('setData')
223
            ->with($this->identicalTo([$templateVar => $data, 'resource' => $resource]));
224
225
        $this->subscriber->onView($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createViewEventMock() on line 188 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...iewSubscriber::onView() does only seem to accept object<Lug\Bundle\Resour...le\Rest\View\ViewEvent>, 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...
226
    }
227
228
    /**
229
     * @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface
230
     */
231
    private function createParameterResolverMock()
232
    {
233
        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...
234
    }
235
236
    /**
237
     * @return \PHPUnit_Framework_MockObject_MockObject|ViewEvent
238
     */
239
    private function createViewEventMock()
240
    {
241
        return $this->getMockBuilder(ViewEvent::class)
242
            ->disableOriginalConstructor()
243
            ->getMock();
244
    }
245
246
    /**
247
     * @return \PHPUnit_Framework_MockObject_MockObject|View
248
     */
249
    private function createViewMock()
250
    {
251
        return $this->getMock(View::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...
252
    }
253
254
    /**
255
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
256
     */
257
    private function createResourceMock()
258
    {
259
        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...
260
    }
261
}
262