20
20
class MapperTest extends \PHPUnit_Framework_TestCase
21
21
{
22
22
const INDEX_NAME = 'test_index_fulltext ' ;
23
+ private $ matchBuilder ;
23
24
24
25
/**
25
26
* @var \Magento\Framework\Search\RequestInterface|MockObject
@@ -56,11 +57,6 @@ class MapperTest extends \PHPUnit_Framework_TestCase
56
57
*/
57
58
private $ filterBuilder ;
58
59
59
- /**
60
- * @var \Magento\Framework\Search\Request\FilterInterface|MockObject
61
- */
62
- private $ filter ;
63
-
64
60
/**
65
61
* @var Mapper
66
62
*/
@@ -71,7 +67,7 @@ protected function setUp()
71
67
$ helper = new ObjectManager ($ this );
72
68
73
69
$ this ->select = $ this ->getMockBuilder ('Magento\Framework\DB\Select ' )
74
- ->setMethods (['group ' , 'limit ' , 'where ' , 'columns ' , 'from ' ])
70
+ ->setMethods (['group ' , 'limit ' , 'where ' , 'columns ' , 'from ' , ' join ' ])
75
71
->disableOriginalConstructor ()
76
72
->getMock ();
77
73
$ this ->select ->expects ($ this ->any ())
@@ -108,7 +104,7 @@ protected function setUp()
108
104
->getMockForAbstractClass ();
109
105
110
106
$ this ->queryContainer = $ this ->getMockBuilder ('Magento\Framework\Search\Adapter\Mysql\Query\QueryContainer ' )
111
- ->setMethods (['addMatchQuery ' ])
107
+ ->setMethods (['addMatchQuery ' , ' getDerivedQueries ' ])
112
108
->disableOriginalConstructor ()
113
109
->getMock ();
114
110
$ this ->queryContainer ->expects ($ this ->any ())
@@ -124,14 +120,18 @@ protected function setUp()
124
120
->method ('create ' )
125
121
->willReturn ($ this ->queryContainer );
126
122
127
- $ this ->filter = $ this ->getMockBuilder ('Magento\Framework\Search\Request\FilterInterface ' )
123
+ $ this ->filterBuilder = $ this ->getMockBuilder ('Magento\Framework\Search\Adapter\Mysql\Filter\Builder ' )
124
+ ->setMethods (['build ' ])
128
125
->disableOriginalConstructor ()
129
- ->getMockForAbstractClass ();
126
+ ->getMock ();
130
127
131
- $ this ->filterBuilder = $ this ->getMockBuilder ('Magento\Framework\Search\Adapter\Mysql\Filter \Builder ' )
128
+ $ this ->matchBuilder = $ this ->getMockBuilder ('\ Magento\Framework\Search\Adapter\Mysql\Query \Builder\Match ' )
132
129
->setMethods (['build ' ])
133
130
->disableOriginalConstructor ()
134
131
->getMock ();
132
+ $ this ->matchBuilder ->expects ($ this ->any ())
133
+ ->method ('build ' )
134
+ ->willReturnArgument (1 );
135
135
136
136
/** @var MockObject|\Magento\Framework\Search\Adapter\Mysql\IndexBuilderInterface $indexBuilder */
137
137
$ indexBuilder = $ this ->getMockBuilder ('\Magento\Framework\Search\Adapter\Mysql\IndexBuilderInterface ' )
@@ -154,7 +154,8 @@ protected function setUp()
154
154
'scoreBuilderFactory ' => $ this ->scoreBuilderFactory ,
155
155
'queryContainerFactory ' => $ queryContainerFactory ,
156
156
'filterBuilder ' => $ this ->filterBuilder ,
157
- 'indexProviders ' => [$ index => $ indexBuilder ]
157
+ 'matchBuilder ' => $ this ->matchBuilder ,
158
+ 'indexProviders ' => [$ index => $ indexBuilder ],
158
159
]
159
160
);
160
161
}
@@ -163,6 +164,10 @@ public function testBuildMatchQuery()
163
164
{
164
165
$ query = $ this ->createMatchQuery ();
165
166
167
+ $ this ->queryContainer ->expects ($ this ->once ())
168
+ ->method ('getDerivedQueries ' )
169
+ ->willReturn ([]);
170
+
166
171
$ this ->queryContainer ->expects ($ this ->any ())->method ('addMatchQuery ' )
167
172
->with (
168
173
$ this ->equalTo ($ this ->select ),
@@ -173,7 +178,7 @@ public function testBuildMatchQuery()
173
178
174
179
$ this ->request ->expects ($ this ->once ())->method ('getQuery ' )->will ($ this ->returnValue ($ query ));
175
180
176
- $ this ->select ->expects ($ this ->once ())->method ('columns ' )->will ($ this ->returnValue ($ this ->select ));
181
+ $ this ->select ->expects ($ this ->any ())->method ('columns ' )->will ($ this ->returnValue ($ this ->select ));
177
182
178
183
$ response = $ this ->mapper ->buildQuery ($ this ->request );
179
184
@@ -182,11 +187,13 @@ public function testBuildMatchQuery()
182
187
183
188
public function testBuildFilterQuery ()
184
189
{
185
- $ query = $ this ->createFilterQuery ();
186
- $ query ->expects ($ this ->once ())->method ('getReferenceType ' )->will ($ this ->returnValue (Filter::REFERENCE_FILTER ));
187
- $ query ->expects ($ this ->once ())->method ('getReference ' )->will ($ this ->returnValue ($ this ->filter ));
190
+ $ query = $ this ->createFilterQuery (Filter::REFERENCE_FILTER , $ this ->createFilter ());
191
+
192
+ $ this ->queryContainer ->expects ($ this ->once ())
193
+ ->method ('getDerivedQueries ' )
194
+ ->willReturn ([]);
188
195
189
- $ this ->select ->expects ($ this ->once ())->method ('columns ' )->will ($ this ->returnValue ($ this ->select ));
196
+ $ this ->select ->expects ($ this ->any ())->method ('columns ' )->will ($ this ->returnValue ($ this ->select ));
190
197
191
198
$ this ->request ->expects ($ this ->once ())->method ('getQuery ' )->will ($ this ->returnValue ($ query ));
192
199
@@ -197,64 +204,84 @@ public function testBuildFilterQuery()
197
204
$ this ->assertEquals ($ this ->select , $ response );
198
205
}
199
206
200
- public function testBuildBoolQuery ()
207
+ /**
208
+ * @param $query
209
+ * @throws \Exception
210
+ * @dataProvider buildQueryDataProvider
211
+ */
212
+ public function testBuildQuery ($ query , $ derivedQueries = [])
201
213
{
202
- $ query = $ this ->createBoolQuery ();
203
- $ this ->request ->expects ($ this ->once ())->method ('getQuery ' )->will ($ this ->returnValue ($ query ));
214
+ $ this ->filterBuilder ->expects ($ this ->any ())->method ('build ' )->will ($ this ->returnValue ('(1) ' ));
204
215
205
- $ matchQuery = $ this ->createMatchQuery ();
206
- $ filterMatchQuery = $ this ->createFilterQuery ();
207
- $ filterMatchQuery ->expects ($ this ->once ())->method ('getReferenceType ' )
208
- ->will ($ this ->returnValue (Filter::REFERENCE_QUERY ));
209
- $ filterMatchQuery ->expects ($ this ->once ())->method ('getReference ' )->will ($ this ->returnValue ($ matchQuery ));
216
+ $ this ->queryContainer ->expects ($ this ->any ())
217
+ ->method ('getDerivedQueries ' )
218
+ ->willReturn ($ derivedQueries );
210
219
211
- $ filterQuery = $ this ->createFilterQuery ();
212
- $ filterQuery ->expects ($ this ->once ())->method ('getReferenceType ' )
213
- ->will ($ this ->returnValue (Filter::REFERENCE_FILTER ));
214
- $ filterQuery ->expects ($ this ->once ())->method ('getReference ' )->will ($ this ->returnValue ($ this ->filter ));
220
+ $ this ->select ->expects ($ this ->any ())->method ('columns ' )->will ($ this ->returnValue ($ this ->select ));
215
221
216
222
$ this ->request ->expects ($ this ->once ())->method ('getQuery ' )->will ($ this ->returnValue ($ query ));
217
223
218
- $ this -> filterBuilder -> expects ( $ this ->once ())-> method ( ' build ' )-> will ( $ this ->returnValue ( ' (1) ' ) );
224
+ $ response = $ this ->mapper -> buildQuery ( $ this ->request );
219
225
220
- $ this ->select ->expects ($ this ->once ())->method ('columns ' )->will ($ this ->returnValue ($ this ->select ));
226
+ $ this ->assertEquals ($ this ->select , $ response );
227
+ }
221
228
222
- $ query ->expects ($ this ->once ())
223
- ->method ('getMust ' )
224
- ->will (
225
- $ this ->returnValue (
229
+ public function buildQueryDataProvider ()
230
+ {
231
+ return [
232
+ [
233
+ $ this ->createBoolQuery (
226
234
[
227
235
$ this ->createMatchQuery (),
228
- $ this ->createFilterQuery (),
229
- ]
230
- )
231
- );
232
-
233
- $ query ->expects ($ this ->once ())
234
- ->method ('getShould ' )
235
- ->will (
236
- $ this ->returnValue (
236
+ $ this ->createFilterQuery (Filter::REFERENCE_QUERY , $ this ->createMatchQuery ()),
237
+ ],
237
238
[
238
239
$ this ->createMatchQuery (),
239
- $ filterMatchQuery ,
240
- ]
241
- )
242
- );
243
-
244
- $ query ->expects ($ this ->once ())
245
- ->method ('getMustNot ' )
246
- ->will (
247
- $ this ->returnValue (
240
+ $ this ->createFilterQuery (Filter::REFERENCE_FILTER , $ this ->createFilter ()),
241
+ ],
248
242
[
249
243
$ this ->createMatchQuery (),
250
- $ filterQuery ,
244
+ $ this -> createFilterQuery (Filter:: REFERENCE_FILTER , $ this -> createFilter ()) ,
251
245
]
252
- )
253
- );
254
-
255
- $ response = $ this ->mapper ->buildQuery ($ this ->request );
256
-
257
- $ this ->assertEquals ($ this ->select , $ response );
246
+ ),
247
+ ],
248
+ [
249
+ $ this ->createBoolQuery (
250
+ [
251
+ $ this ->createMatchQuery (),
252
+ $ this ->createMatchQuery (),
253
+ ],
254
+ [],
255
+ []
256
+ ),
257
+ [
258
+ $ this ->createMatchContainer (
259
+ $ this ->createMatchQuery (),
260
+ 'mustNot '
261
+ ),
262
+ ],
263
+ ],
264
+ [
265
+ $ this ->createBoolQuery (
266
+ [
267
+ $ this ->createMatchQuery (),
268
+ $ this ->createMatchQuery (),
269
+ ],
270
+ [],
271
+ []
272
+ ),
273
+ [
274
+ $ this ->createMatchContainer (
275
+ $ this ->createMatchQuery (),
276
+ 'mustNot '
277
+ ),
278
+ $ this ->createMatchContainer (
279
+ $ this ->createMatchQuery (),
280
+ 'must '
281
+ ),
282
+ ],
283
+ ],
284
+ ];
258
285
}
259
286
260
287
/**
@@ -291,9 +318,11 @@ private function createMatchQuery()
291
318
}
292
319
293
320
/**
321
+ * @param string $referenceType
322
+ * @param mixed $reference
294
323
* @return MockObject
295
324
*/
296
- private function createFilterQuery ()
325
+ private function createFilterQuery ($ referenceType , $ reference )
297
326
{
298
327
$ query = $ this ->getMockBuilder ('Magento\Framework\Search\Request\Query\Filter ' )
299
328
->setMethods (['getType ' , 'getReferenceType ' , 'getReference ' ])
@@ -302,13 +331,17 @@ private function createFilterQuery()
302
331
$ query ->expects ($ this ->exactly (1 ))
303
332
->method ('getType ' )
304
333
->will ($ this ->returnValue (QueryInterface::TYPE_FILTER ));
334
+ $ query ->expects ($ this ->once ())->method ('getReferenceType ' )
335
+ ->will ($ this ->returnValue ($ referenceType ));
336
+ $ query ->expects ($ this ->once ())->method ('getReference ' )
337
+ ->will ($ this ->returnValue ($ reference ));
305
338
return $ query ;
306
339
}
307
340
308
341
/**
309
342
* @return MockObject
310
343
*/
311
- private function createBoolQuery ()
344
+ private function createBoolQuery (array $ must , array $ should , array $ mustNot )
312
345
{
313
346
$ query = $ this ->getMockBuilder ('Magento\Framework\Search\Request\Query\Bool ' )
314
347
->setMethods (['getMust ' , 'getShould ' , 'getMustNot ' , 'getType ' ])
@@ -317,6 +350,44 @@ private function createBoolQuery()
317
350
$ query ->expects ($ this ->exactly (1 ))
318
351
->method ('getType ' )
319
352
->will ($ this ->returnValue (QueryInterface::TYPE_BOOL ));
353
+ $ query ->expects ($ this ->once ())
354
+ ->method ('getMust ' )
355
+ ->will ($ this ->returnValue ($ must ));
356
+ $ query ->expects ($ this ->once ())
357
+ ->method ('getShould ' )
358
+ ->will ($ this ->returnValue ($ should ));
359
+ $ query ->expects ($ this ->once ())
360
+ ->method ('getMustNot ' )
361
+ ->will ($ this ->returnValue ($ mustNot ));
320
362
return $ query ;
321
363
}
364
+
365
+ /**
366
+ * @return MockObject
367
+ */
368
+ private function createFilter ()
369
+ {
370
+ return $ this ->getMockBuilder ('Magento\Framework\Search\Request\FilterInterface ' )
371
+ ->disableOriginalConstructor ()
372
+ ->getMockForAbstractClass ();
373
+ }
374
+
375
+ /**
376
+ * @param $request
377
+ * @param $conditionType
378
+ */
379
+ private function createMatchContainer ($ request , $ conditionType )
380
+ {
381
+ $ matchContainer = $ this ->getMockBuilder ('\Magento\Framework\Search\Adapter\Mysql\Query\MatchContainer ' )
382
+ ->setMethods (['getRequest ' , 'getConditionType ' ])
383
+ ->disableOriginalConstructor ()
384
+ ->getMock ();
385
+ $ matchContainer ->expects ($ this ->any ())
386
+ ->method ('getRequest ' )
387
+ ->willReturn ($ request );
388
+ $ matchContainer ->expects ($ this ->any ())
389
+ ->method ('getConditionType ' )
390
+ ->willReturn ($ conditionType );
391
+ return $ matchContainer ;
392
+ }
322
393
}
0 commit comments