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 |
|
|
|
|
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 |
|
|
|
|
101
|
|
|
->expects($this->once()) |
102
|
|
|
->method('getName') |
103
|
|
|
->will($this->returnValue($name = 'name')); |
104
|
|
|
|
105
|
|
|
$this->repositoryRegistry |
|
|
|
|
106
|
|
|
->expects($this->once()) |
107
|
|
|
->method('offsetGet') |
108
|
|
|
->with($this->identicalTo($name)) |
109
|
|
|
->will($this->returnValue($repository = $this->createRepositoryMock())); |
110
|
|
|
|
111
|
|
|
$repository |
|
|
|
|
112
|
|
|
->expects($this->once()) |
113
|
|
|
->method('createDataSourceBuilder') |
114
|
|
|
->with($this->identicalTo($options)) |
115
|
|
|
->will($this->returnValue($builder = $this->createDataSourceBuilderMock())); |
116
|
|
|
|
117
|
|
|
$this->filterer |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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)); |
|
|
|
|
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
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: