Skip to content

Commit 3ba5989

Browse files
committed
MAGETWO-39942: Low quick search performance(MySql)
- Fix unit tests
1 parent bd3fd79 commit 3ba5989

File tree

1 file changed

+132
-61
lines changed
  • lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql

1 file changed

+132
-61
lines changed

lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql/MapperTest.php

Lines changed: 132 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class MapperTest extends \PHPUnit_Framework_TestCase
2121
{
2222
const INDEX_NAME = 'test_index_fulltext';
23+
private $matchBuilder;
2324

2425
/**
2526
* @var \Magento\Framework\Search\RequestInterface|MockObject
@@ -56,11 +57,6 @@ class MapperTest extends \PHPUnit_Framework_TestCase
5657
*/
5758
private $filterBuilder;
5859

59-
/**
60-
* @var \Magento\Framework\Search\Request\FilterInterface|MockObject
61-
*/
62-
private $filter;
63-
6460
/**
6561
* @var Mapper
6662
*/
@@ -71,7 +67,7 @@ protected function setUp()
7167
$helper = new ObjectManager($this);
7268

7369
$this->select = $this->getMockBuilder('Magento\Framework\DB\Select')
74-
->setMethods(['group', 'limit', 'where', 'columns', 'from'])
70+
->setMethods(['group', 'limit', 'where', 'columns', 'from', 'join'])
7571
->disableOriginalConstructor()
7672
->getMock();
7773
$this->select->expects($this->any())
@@ -108,7 +104,7 @@ protected function setUp()
108104
->getMockForAbstractClass();
109105

110106
$this->queryContainer = $this->getMockBuilder('Magento\Framework\Search\Adapter\Mysql\Query\QueryContainer')
111-
->setMethods(['addMatchQuery'])
107+
->setMethods(['addMatchQuery', 'getDerivedQueries'])
112108
->disableOriginalConstructor()
113109
->getMock();
114110
$this->queryContainer->expects($this->any())
@@ -124,14 +120,18 @@ protected function setUp()
124120
->method('create')
125121
->willReturn($this->queryContainer);
126122

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'])
128125
->disableOriginalConstructor()
129-
->getMockForAbstractClass();
126+
->getMock();
130127

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')
132129
->setMethods(['build'])
133130
->disableOriginalConstructor()
134131
->getMock();
132+
$this->matchBuilder->expects($this->any())
133+
->method('build')
134+
->willReturnArgument(1);
135135

136136
/** @var MockObject|\Magento\Framework\Search\Adapter\Mysql\IndexBuilderInterface $indexBuilder */
137137
$indexBuilder = $this->getMockBuilder('\Magento\Framework\Search\Adapter\Mysql\IndexBuilderInterface')
@@ -154,7 +154,8 @@ protected function setUp()
154154
'scoreBuilderFactory' => $this->scoreBuilderFactory,
155155
'queryContainerFactory' => $queryContainerFactory,
156156
'filterBuilder' => $this->filterBuilder,
157-
'indexProviders' => [$index => $indexBuilder]
157+
'matchBuilder' => $this->matchBuilder,
158+
'indexProviders' => [$index => $indexBuilder],
158159
]
159160
);
160161
}
@@ -163,6 +164,10 @@ public function testBuildMatchQuery()
163164
{
164165
$query = $this->createMatchQuery();
165166

167+
$this->queryContainer->expects($this->once())
168+
->method('getDerivedQueries')
169+
->willReturn([]);
170+
166171
$this->queryContainer->expects($this->any())->method('addMatchQuery')
167172
->with(
168173
$this->equalTo($this->select),
@@ -173,7 +178,7 @@ public function testBuildMatchQuery()
173178

174179
$this->request->expects($this->once())->method('getQuery')->will($this->returnValue($query));
175180

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));
177182

178183
$response = $this->mapper->buildQuery($this->request);
179184

@@ -182,11 +187,13 @@ public function testBuildMatchQuery()
182187

183188
public function testBuildFilterQuery()
184189
{
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([]);
188195

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));
190197

191198
$this->request->expects($this->once())->method('getQuery')->will($this->returnValue($query));
192199

@@ -197,64 +204,84 @@ public function testBuildFilterQuery()
197204
$this->assertEquals($this->select, $response);
198205
}
199206

200-
public function testBuildBoolQuery()
207+
/**
208+
* @param $query
209+
* @throws \Exception
210+
* @dataProvider buildQueryDataProvider
211+
*/
212+
public function testBuildQuery($query, $derivedQueries = [])
201213
{
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)'));
204215

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);
210219

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));
215221

216222
$this->request->expects($this->once())->method('getQuery')->will($this->returnValue($query));
217223

218-
$this->filterBuilder->expects($this->once())->method('build')->will($this->returnValue('(1)'));
224+
$response = $this->mapper->buildQuery($this->request);
219225

220-
$this->select->expects($this->once())->method('columns')->will($this->returnValue($this->select));
226+
$this->assertEquals($this->select, $response);
227+
}
221228

222-
$query->expects($this->once())
223-
->method('getMust')
224-
->will(
225-
$this->returnValue(
229+
public function buildQueryDataProvider()
230+
{
231+
return [
232+
[
233+
$this->createBoolQuery(
226234
[
227235
$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+
],
237238
[
238239
$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+
],
248242
[
249243
$this->createMatchQuery(),
250-
$filterQuery,
244+
$this->createFilterQuery(Filter::REFERENCE_FILTER, $this->createFilter()),
251245
]
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+
];
258285
}
259286

260287
/**
@@ -291,9 +318,11 @@ private function createMatchQuery()
291318
}
292319

293320
/**
321+
* @param string $referenceType
322+
* @param mixed $reference
294323
* @return MockObject
295324
*/
296-
private function createFilterQuery()
325+
private function createFilterQuery($referenceType, $reference)
297326
{
298327
$query = $this->getMockBuilder('Magento\Framework\Search\Request\Query\Filter')
299328
->setMethods(['getType', 'getReferenceType', 'getReference'])
@@ -302,13 +331,17 @@ private function createFilterQuery()
302331
$query->expects($this->exactly(1))
303332
->method('getType')
304333
->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));
305338
return $query;
306339
}
307340

308341
/**
309342
* @return MockObject
310343
*/
311-
private function createBoolQuery()
344+
private function createBoolQuery(array $must, array $should, array $mustNot)
312345
{
313346
$query = $this->getMockBuilder('Magento\Framework\Search\Request\Query\Bool')
314347
->setMethods(['getMust', 'getShould', 'getMustNot', 'getType'])
@@ -317,6 +350,44 @@ private function createBoolQuery()
317350
$query->expects($this->exactly(1))
318351
->method('getType')
319352
->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));
320362
return $query;
321363
}
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+
}
322393
}

0 commit comments

Comments
 (0)