1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Tests\Subscriber; |
13
|
|
|
|
14
|
|
|
use Elastica\Query; |
15
|
|
|
use FOS\ElasticaBundle\Paginator\PartialResultsInterface; |
16
|
|
|
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter; |
17
|
|
|
use FOS\ElasticaBundle\Subscriber\PaginateElasticaQuerySubscriber; |
18
|
|
|
use Knp\Component\Pager\Event\ItemsEvent; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
21
|
|
|
|
22
|
|
|
class PaginateElasticaQuerySubscriberTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
protected function getAdapterMock() |
25
|
|
|
{ |
26
|
|
|
return $this->getMockBuilder(RawPaginatorAdapter::class) |
27
|
|
|
->disableOriginalConstructor() |
28
|
|
|
->getMock(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function getResultSetMock() |
32
|
|
|
{ |
33
|
|
|
return $this->getMockBuilder(PartialResultsInterface::class) |
34
|
|
|
->disableOriginalConstructor() |
35
|
|
|
->getMock(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testShouldDoNothingIfSortParamIsEmpty() |
39
|
|
|
{ |
40
|
|
|
$subscriber = new PaginateElasticaQuerySubscriber($this->getRequestStack(new Request())); |
41
|
|
|
|
42
|
|
|
$adapter = $this->getAdapterMock(); |
43
|
|
|
$adapter->expects($this->never()) |
44
|
|
|
->method('getQuery'); |
45
|
|
|
$adapter->method('getResults') |
46
|
|
|
->willReturn($this->getResultSetMock()); |
47
|
|
|
|
48
|
|
|
$event = new ItemsEvent(0, 10); |
49
|
|
|
$event->target = $adapter; |
50
|
|
|
|
51
|
|
|
$subscriber->items($event); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function sortCases() |
55
|
|
|
{ |
56
|
|
|
$tests = array(); |
57
|
|
|
|
58
|
|
|
$expected = array( |
59
|
|
|
'createdAt' => array( |
60
|
|
|
'order' => 'asc', |
61
|
|
|
), |
62
|
|
|
); |
63
|
|
|
$tests[] = array($expected, new Request()); |
64
|
|
|
|
65
|
|
|
$expected = array( |
66
|
|
|
'name' => array( |
67
|
|
|
'order' => 'desc', |
68
|
|
|
), |
69
|
|
|
); |
70
|
|
|
$tests[] = array($expected, new Request(array('ord' => 'name', 'az' => 'desc'))); |
71
|
|
|
|
72
|
|
|
$expected = array( |
73
|
|
|
'updatedAt' => array( |
74
|
|
|
'order' => 'asc', |
75
|
|
|
), |
76
|
|
|
); |
77
|
|
|
$tests[] = array($expected, new Request(array('ord' => 'updatedAt', 'az' => 'invalid'))); |
78
|
|
|
|
79
|
|
|
return $tests; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @dataProvider sortCases |
84
|
|
|
*/ |
85
|
|
|
public function testShouldSort(array $expected, Request $request) |
86
|
|
|
{ |
87
|
|
|
$subscriber = new PaginateElasticaQuerySubscriber($this->getRequestStack($request)); |
88
|
|
|
|
89
|
|
|
$query = new Query(); |
90
|
|
|
$adapter = $this->getAdapterMock(); |
91
|
|
|
$adapter->method('getQuery') |
92
|
|
|
->willReturn($query); |
93
|
|
|
|
94
|
|
|
$adapter->method('getResults') |
95
|
|
|
->willReturn($this->getResultSetMock()); |
96
|
|
|
|
97
|
|
|
$event = new ItemsEvent(0, 10); |
98
|
|
|
$event->target = $adapter; |
99
|
|
|
$event->options = array( |
100
|
|
|
'defaultSortFieldName' => 'createdAt', |
101
|
|
|
'sortFieldParameterName' => 'ord', |
102
|
|
|
'sortDirectionParameterName' => 'az', |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$subscriber->items($event); |
106
|
|
|
|
107
|
|
|
$this->assertSame($expected, $query->getParam('sort')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @expectedException \UnexpectedValueException |
112
|
|
|
*/ |
113
|
|
|
public function testShouldThrowIfFieldIsNotWhitelisted() |
114
|
|
|
{ |
115
|
|
|
$subscriber = new PaginateElasticaQuerySubscriber($this->getRequestStack(new Request(array('ord' => 'owner')))); |
116
|
|
|
|
117
|
|
|
$query = new Query(); |
118
|
|
|
$adapter = $this->getAdapterMock(); |
119
|
|
|
$adapter->method('getQuery') |
120
|
|
|
->willReturn($query); |
121
|
|
|
|
122
|
|
|
$adapter->method('getResults') |
123
|
|
|
->willReturn($this->getResultSetMock()); |
124
|
|
|
|
125
|
|
|
$event = new ItemsEvent(0, 10); |
126
|
|
|
$event->target = $adapter; |
127
|
|
|
$event->options = array( |
128
|
|
|
'defaultSortFieldName' => 'createdAt', |
129
|
|
|
'sortFieldParameterName' => 'ord', |
130
|
|
|
'sortDirectionParameterName' => 'az', |
131
|
|
|
'sortFieldWhitelist' => array('createdAt', 'updatedAt'), |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$subscriber->items($event); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function testShouldAddNestedPath() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$subscriber = new PaginateElasticaQuerySubscriber($this->getRequestStack(new Request(array('ord' => 'owner.name')))); |
140
|
|
|
|
141
|
|
|
$query = new Query(); |
142
|
|
|
$adapter = $this->getAdapterMock(); |
143
|
|
|
$adapter->method('getQuery') |
144
|
|
|
->willReturn($query); |
145
|
|
|
|
146
|
|
|
$adapter->method('getResults') |
147
|
|
|
->willReturn($this->getResultSetMock()); |
148
|
|
|
|
149
|
|
|
$event = new ItemsEvent(0, 10); |
150
|
|
|
$event->target = $adapter; |
151
|
|
|
$event->options = array( |
152
|
|
|
'defaultSortFieldName' => 'createdAt', |
153
|
|
|
'sortFieldParameterName' => 'ord', |
154
|
|
|
'sortDirectionParameterName' => 'az', |
155
|
|
|
'sortNestedPath' => 'owner', |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$subscriber->items($event); |
159
|
|
|
$this->assertSame(array( |
160
|
|
|
'owner.name' => array( |
161
|
|
|
'order' => 'asc', |
162
|
|
|
'nested_path' => 'owner', |
163
|
|
|
), |
164
|
|
|
), $query->getParam('sort')); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
View Code Duplication |
public function testShouldInvokeCallableNestedPath() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$subscriber = new PaginateElasticaQuerySubscriber($this->getRequestStack(new Request(array('ord' => 'owner.name')))); |
170
|
|
|
|
171
|
|
|
$query = new Query(); |
172
|
|
|
$adapter = $this->getAdapterMock(); |
173
|
|
|
$adapter->method('getQuery') |
174
|
|
|
->willReturn($query); |
175
|
|
|
|
176
|
|
|
$adapter->method('getResults') |
177
|
|
|
->willReturn($this->getResultSetMock()); |
178
|
|
|
|
179
|
|
|
$event = new ItemsEvent(0, 10); |
180
|
|
|
$event->target = $adapter; |
181
|
|
|
$event->options = array( |
182
|
|
|
'defaultSortFieldName' => 'createdAt', |
183
|
|
|
'sortFieldParameterName' => 'ord', |
184
|
|
|
'sortDirectionParameterName' => 'az', |
185
|
|
|
'sortNestedPath' => function ($sortField) { |
186
|
|
|
$this->assertSame('owner.name', $sortField); |
187
|
|
|
|
188
|
|
|
return 'owner'; |
189
|
|
|
}, |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$subscriber->items($event); |
193
|
|
|
$this->assertSame(array( |
194
|
|
|
'owner.name' => array( |
195
|
|
|
'order' => 'asc', |
196
|
|
|
'nested_path' => 'owner', |
197
|
|
|
), |
198
|
|
|
), $query->getParam('sort')); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
View Code Duplication |
public function testShouldAddNestedFilter() |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
$subscriber = new PaginateElasticaQuerySubscriber($this->getRequestStack(new Request(array('ord' => 'owner.name')))); |
204
|
|
|
|
205
|
|
|
$query = new Query(); |
206
|
|
|
$adapter = $this->getAdapterMock(); |
207
|
|
|
$adapter->method('getQuery') |
208
|
|
|
->willReturn($query); |
209
|
|
|
|
210
|
|
|
$adapter->method('getResults') |
211
|
|
|
->willReturn($this->getResultSetMock()); |
212
|
|
|
|
213
|
|
|
$event = new ItemsEvent(0, 10); |
214
|
|
|
$event->target = $adapter; |
215
|
|
|
$event->options = array( |
216
|
|
|
'defaultSortFieldName' => 'createdAt', |
217
|
|
|
'sortFieldParameterName' => 'ord', |
218
|
|
|
'sortDirectionParameterName' => 'az', |
219
|
|
|
'sortNestedPath' => 'owner', |
220
|
|
|
'sortNestedFilter' => new Query\Term(array('enabled' => array('value' => true))), |
221
|
|
|
); |
222
|
|
|
|
223
|
|
|
$subscriber->items($event); |
224
|
|
|
$this->assertSame(array( |
225
|
|
|
'sort' => array( |
226
|
|
|
'owner.name' => array( |
227
|
|
|
'order' => 'asc', |
228
|
|
|
'nested_path' => 'owner', |
229
|
|
|
'nested_filter' => array( |
230
|
|
|
'term' => array( |
231
|
|
|
'enabled' => array('value' => true), |
232
|
|
|
), |
233
|
|
|
), |
234
|
|
|
), |
235
|
|
|
), |
236
|
|
|
'query' => array( |
237
|
|
|
'match_all' => new \stdClass(), |
238
|
|
|
), |
239
|
|
|
), $query->toArray()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
View Code Duplication |
public function testShouldInvokeNestedFilterCallable() |
|
|
|
|
243
|
|
|
{ |
244
|
|
|
$subscriber = new PaginateElasticaQuerySubscriber($this->getRequestStack(new Request(array('ord' => 'owner.name')))); |
245
|
|
|
|
246
|
|
|
$query = new Query(); |
247
|
|
|
$adapter = $this->getAdapterMock(); |
248
|
|
|
$adapter->method('getQuery') |
249
|
|
|
->willReturn($query); |
250
|
|
|
|
251
|
|
|
$adapter->method('getResults') |
252
|
|
|
->willReturn($this->getResultSetMock()); |
253
|
|
|
|
254
|
|
|
$event = new ItemsEvent(0, 10); |
255
|
|
|
$event->target = $adapter; |
256
|
|
|
$event->options = array( |
257
|
|
|
'defaultSortFieldName' => 'createdAt', |
258
|
|
|
'sortFieldParameterName' => 'ord', |
259
|
|
|
'sortDirectionParameterName' => 'az', |
260
|
|
|
'sortNestedPath' => 'owner', |
261
|
|
|
'sortNestedFilter' => function ($sortField) { |
262
|
|
|
$this->assertSame('owner.name', $sortField); |
263
|
|
|
|
264
|
|
|
return new Query\Term(array('enabled' => array('value' => true))); |
265
|
|
|
}, |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
$subscriber->items($event); |
269
|
|
|
$this->assertSame(array( |
270
|
|
|
'sort' => array( |
271
|
|
|
'owner.name' => array( |
272
|
|
|
'order' => 'asc', |
273
|
|
|
'nested_path' => 'owner', |
274
|
|
|
'nested_filter' => array( |
275
|
|
|
'term' => array( |
276
|
|
|
'enabled' => array('value' => true), |
277
|
|
|
), |
278
|
|
|
), |
279
|
|
|
), |
280
|
|
|
), |
281
|
|
|
'query' => array( |
282
|
|
|
'match_all' => new \stdClass(), |
283
|
|
|
), |
284
|
|
|
), $query->toArray()); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
private function getRequestStack(Request $request = null) |
288
|
|
|
{ |
289
|
|
|
$stack = new RequestStack(); |
290
|
|
|
|
291
|
|
|
if ($request) { |
292
|
|
|
$stack->push($request); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $stack; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
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.