GridHandlerTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 206
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A testInheritance() 0 4 1
A testHandle() 0 68 1
A createServiceRegistryMock() 0 4 1
A createGridViewFactoryMock() 0 4 1
A createFiltererMock() 0 4 1
A createSorterMock() 0 4 1
A createSlicerMock() 0 4 1
A createGridMock() 0 4 1
A createResourceMock() 0 4 1
A createRepositoryMock() 0 4 1
A createDataSourceBuilderMock() 0 4 1
A createGridViewMock() 0 4 1
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\Component\Grid\Tests\Handler;
13
14
use Lug\Component\Grid\DataSource\DataSourceBuilderInterface;
15
use Lug\Component\Grid\Filter\FiltererInterface;
16
use Lug\Component\Grid\Handler\GridHandler;
17
use Lug\Component\Grid\Handler\GridHandlerInterface;
18
use Lug\Component\Grid\Model\GridInterface;
19
use Lug\Component\Grid\Slicer\SlicerInterface;
20
use Lug\Component\Grid\Sort\SorterInterface;
21
use Lug\Component\Grid\View\GridViewFactoryInterface;
22
use Lug\Component\Grid\View\GridViewInterface;
23
use Lug\Component\Registry\Model\RegistryInterface;
24
use Lug\Component\Resource\Model\ResourceInterface;
25
use Lug\Component\Resource\Repository\RepositoryInterface;
26
27
/**
28
 * @author GeLo <[email protected]>
29
 */
30
class GridHandlerTest extends \PHPUnit_Framework_TestCase
31
{
32
    /**
33
     * @var GridHandler
34
     */
35
    private $handler;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
39
     */
40
    private $repositoryRegistry;
41
42
    /**
43
     * @var \PHPUnit_Framework_MockObject_MockObject|GridViewFactoryInterface
44
     */
45
    private $gridViewFactory;
46
47
    /**
48
     * @var \PHPUnit_Framework_MockObject_MockObject|FiltererInterface
49
     */
50
    private $filterer;
51
52
    /**
53
     * @var \PHPUnit_Framework_MockObject_MockObject|SorterInterface
54
     */
55
    private $sorter;
56
57
    /**
58
     * @var \PHPUnit_Framework_MockObject_MockObject|SlicerInterface
59
     */
60
    private $slicer;
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function setUp()
66
    {
67
        $this->repositoryRegistry = $this->createServiceRegistryMock();
68
        $this->gridViewFactory = $this->createGridViewFactoryMock();
69
        $this->filterer = $this->createFiltererMock();
70
        $this->sorter = $this->createSorterMock();
71
        $this->slicer = $this->createSlicerMock();
72
73
        $this->handler = new GridHandler(
74
            $this->repositoryRegistry,
75
            $this->gridViewFactory,
76
            $this->filterer,
77
            $this->sorter,
78
            $this->slicer
79
        );
80
    }
81
82
    public function testInheritance()
83
    {
84
        $this->assertInstanceOf(GridHandlerInterface::class, $this->handler);
85
    }
86
87
    public function testHandle()
88
    {
89
        $grid = $this->createGridMock();
90
        $grid
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Grid\Model\GridInterface.

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...
91
            ->expects($this->once())
92
            ->method('getResource')
93
            ->will($this->returnValue($resource = $this->createResourceMock()));
94
95
        $grid
96
            ->expects($this->once())
97
            ->method('getOptions')
98
            ->will($this->returnValue($options = ['foo' => 'bar']));
99
100
        $resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

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...
101
            ->expects($this->once())
102
            ->method('getName')
103
            ->will($this->returnValue($name = 'name'));
104
105
        $this->repositoryRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

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...
106
            ->expects($this->once())
107
            ->method('offsetGet')
108
            ->with($this->identicalTo($name))
109
            ->will($this->returnValue($repository = $this->createRepositoryMock()));
110
111
        $repository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\R...ory\RepositoryInterface.

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...
112
            ->expects($this->once())
113
            ->method('createDataSourceBuilder')
114
            ->with($this->identicalTo($options))
115
            ->will($this->returnValue($builder = $this->createDataSourceBuilderMock()));
116
117
        $this->filterer
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Grid\Filter\FiltererInterface.

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...
118
            ->expects($this->once())
119
            ->method('filter')
120
            ->with(
121
                $this->identicalTo($builder),
122
                $this->identicalTo($grid),
123
                $this->identicalTo($filters = ['filter'])
124
            );
125
126
        $this->sorter
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Grid\Sort\SorterInterface.

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...
127
            ->expects($this->once())
128
            ->method('sort')
129
            ->with(
130
                $this->identicalTo($builder),
131
                $this->identicalTo($grid),
132
                $this->identicalTo($sorting = ['sort'])
133
            );
134
135
        $this->slicer
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Grid\Slicer\SlicerInterface.

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...
136
            ->expects($this->once())
137
            ->method('slice')
138
            ->with(
139
                $this->identicalTo($builder),
140
                $this->identicalTo($grid),
141
                $this->identicalTo($slicing = ['slice'])
142
            );
143
144
        $this->gridViewFactory
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Grid\View\GridViewFactoryInterface.

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...
145
            ->expects($this->once())
146
            ->method('create')
147
            ->with(
148
                $this->identicalTo($grid),
149
                $this->identicalTo($builder)
150
            )
151
            ->will($this->returnValue($view = $this->createGridViewMock()));
152
153
        $this->assertSame($view, $this->handler->handle($grid, $filters, $sorting, $slicing));
0 ignored issues
show
Bug introduced by
It seems like $grid defined by $this->createGridMock() on line 89 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Component\Grid\Handler\GridHandler::handle() does only seem to accept object<Lug\Component\Grid\Model\GridInterface>, 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...
154
    }
155
156
    /**
157
     * @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
158
     */
159
    private function createServiceRegistryMock()
160
    {
161
        return $this->createMock(RegistryInterface::class);
162
    }
163
164
    /**
165
     * @return \PHPUnit_Framework_MockObject_MockObject|GridViewFactoryInterface
166
     */
167
    private function createGridViewFactoryMock()
168
    {
169
        return $this->createMock(GridViewFactoryInterface::class);
170
    }
171
172
    /**
173
     * @return \PHPUnit_Framework_MockObject_MockObject|FiltererInterface
174
     */
175
    private function createFiltererMock()
176
    {
177
        return $this->createMock(FiltererInterface::class);
178
    }
179
180
    /**
181
     * @return \PHPUnit_Framework_MockObject_MockObject|SorterInterface
182
     */
183
    private function createSorterMock()
184
    {
185
        return $this->createMock(SorterInterface::class);
186
    }
187
188
    /**
189
     * @return \PHPUnit_Framework_MockObject_MockObject|SlicerInterface
190
     */
191
    private function createSlicerMock()
192
    {
193
        return $this->createMock(SlicerInterface::class);
194
    }
195
196
    /**
197
     * @return \PHPUnit_Framework_MockObject_MockObject|GridInterface
198
     */
199
    private function createGridMock()
200
    {
201
        return $this->createMock(GridInterface::class);
202
    }
203
204
    /**
205
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
206
     */
207
    private function createResourceMock()
208
    {
209
        return $this->createMock(ResourceInterface::class);
210
    }
211
212
    /**
213
     * @return \PHPUnit_Framework_MockObject_MockObject|RepositoryInterface
214
     */
215
    private function createRepositoryMock()
216
    {
217
        return $this->createMock(RepositoryInterface::class);
218
    }
219
220
    /**
221
     * @return \PHPUnit_Framework_MockObject_MockObject|DataSourceBuilderInterface
222
     */
223
    private function createDataSourceBuilderMock()
224
    {
225
        return $this->createMock(DataSourceBuilderInterface::class);
226
    }
227
228
    /**
229
     * @return \PHPUnit_Framework_MockObject_MockObject|GridViewInterface
230
     */
231
    private function createGridViewMock()
232
    {
233
        return $this->createMock(GridViewInterface::class);
234
    }
235
}
236