|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Elasticsearch\Test\Unit\SearchAdapter; |
| 9 | + |
| 10 | +use Magento\Elasticsearch\SearchAdapter\Mapper; |
| 11 | +use Magento\Elasticsearch\SearchAdapter\Query\Builder as QueryBuilder; |
| 12 | +use Magento\Elasticsearch\SearchAdapter\Query\Builder\Match as MatchQueryBuilder; |
| 13 | +use Magento\Elasticsearch\SearchAdapter\Filter\Builder as FilterBuilder; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 15 | + |
| 16 | +/** |
| 17 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 18 | + */ |
| 19 | +class MapperTest extends \PHPUnit\Framework\TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Mapper |
| 23 | + */ |
| 24 | + protected $model; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var QueryBuilder|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + protected $queryBuilder; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var MatchQueryBuilder|\PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + protected $matchQueryBuilder; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var FilterBuilder|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + protected $filterBuilder; |
| 40 | + |
| 41 | + /** |
| 42 | + * Setup method |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + protected function setUp() |
| 46 | + { |
| 47 | + $this->queryBuilder = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\Query\Builder::class) |
| 48 | + ->setMethods([ |
| 49 | + 'initQuery', |
| 50 | + 'initAggregations', |
| 51 | + ]) |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->getMock(); |
| 54 | + $this->matchQueryBuilder = $this->getMockBuilder( |
| 55 | + \Magento\Elasticsearch\SearchAdapter\Query\Builder\Match::class |
| 56 | + ) |
| 57 | + ->setMethods(['build']) |
| 58 | + ->disableOriginalConstructor() |
| 59 | + ->getMock(); |
| 60 | + $this->filterBuilder = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\Filter\Builder::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + $this->queryBuilder->expects($this->any()) |
| 64 | + ->method('initQuery') |
| 65 | + ->willReturn([ |
| 66 | + 'body' => [ |
| 67 | + 'query' => [], |
| 68 | + ], |
| 69 | + ]); |
| 70 | + $this->queryBuilder->expects($this->any()) |
| 71 | + ->method('initAggregations') |
| 72 | + ->willReturn([ |
| 73 | + 'body' => [ |
| 74 | + 'query' => [], |
| 75 | + ], |
| 76 | + ]); |
| 77 | + $this->matchQueryBuilder->expects($this->any()) |
| 78 | + ->method('build') |
| 79 | + ->willReturn([]); |
| 80 | + |
| 81 | + $objectManagerHelper = new ObjectManagerHelper($this); |
| 82 | + $this->model = $objectManagerHelper->getObject( |
| 83 | + \Magento\Elasticsearch\SearchAdapter\Mapper::class, |
| 84 | + [ |
| 85 | + 'queryBuilder' => $this->queryBuilder, |
| 86 | + 'matchQueryBuilder' => $this->matchQueryBuilder, |
| 87 | + 'filterBuilder' => $this->filterBuilder |
| 88 | + ] |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test buildQuery() method with exception |
| 94 | + * @expectedException \InvalidArgumentException |
| 95 | + */ |
| 96 | + public function testBuildQueryFailure() |
| 97 | + { |
| 98 | + $request = $this->getMockBuilder(\Magento\Framework\Search\RequestInterface::class) |
| 99 | + ->disableOriginalConstructor() |
| 100 | + ->getMock(); |
| 101 | + $query = $this->getMockBuilder(\Magento\Framework\Search\Request\QueryInterface::class) |
| 102 | + ->disableOriginalConstructor() |
| 103 | + ->getMock(); |
| 104 | + $request->expects($this->once()) |
| 105 | + ->method('getQuery') |
| 106 | + ->willReturn($query); |
| 107 | + $query->expects($this->atLeastOnce()) |
| 108 | + ->method('getType') |
| 109 | + ->willReturn('unknown'); |
| 110 | + |
| 111 | + $this->model->buildQuery($request); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test buildQuery() method |
| 116 | + * |
| 117 | + * @param string $queryType |
| 118 | + * @param string $queryMock |
| 119 | + * @param string $referenceType |
| 120 | + * @param string $filterMock |
| 121 | + * @dataProvider buildQueryDataProvider |
| 122 | + */ |
| 123 | + public function testBuildQuery($queryType, $queryMock, $referenceType, $filterMock) |
| 124 | + { |
| 125 | + $request = $this->getMockBuilder(\Magento\Framework\Search\RequestInterface::class) |
| 126 | + ->disableOriginalConstructor() |
| 127 | + ->getMock(); |
| 128 | + $query = $this->getMockBuilder($queryMock) |
| 129 | + ->setMethods(['getMust', 'getMustNot', 'getType', 'getShould', 'getReferenceType', 'getReference']) |
| 130 | + ->disableOriginalConstructor() |
| 131 | + ->getMock(); |
| 132 | + $matchQuery = $this->getMockBuilder(\Magento\Framework\Search\Request\Query\Match::class) |
| 133 | + ->disableOriginalConstructor() |
| 134 | + ->getMock(); |
| 135 | + $filterQuery = $this->getMockBuilder($filterMock) |
| 136 | + ->disableOriginalConstructor() |
| 137 | + ->getMock(); |
| 138 | + $request->expects($this->once()) |
| 139 | + ->method('getQuery') |
| 140 | + ->willReturn($query); |
| 141 | + |
| 142 | + $query->expects($this->atLeastOnce()) |
| 143 | + ->method('getType') |
| 144 | + ->willReturn($queryType); |
| 145 | + $query->expects($this->any()) |
| 146 | + ->method('getMust') |
| 147 | + ->willReturn([$matchQuery]); |
| 148 | + $query->expects($this->any()) |
| 149 | + ->method('getShould') |
| 150 | + ->willReturn([]); |
| 151 | + $query->expects($this->any()) |
| 152 | + ->method('getMustNot') |
| 153 | + ->willReturn([]); |
| 154 | + $query->expects($this->any()) |
| 155 | + ->method('getReferenceType') |
| 156 | + ->willReturn($referenceType); |
| 157 | + $query->expects($this->any()) |
| 158 | + ->method('getReference') |
| 159 | + ->willReturn($filterQuery); |
| 160 | + $matchQuery->expects($this->any()) |
| 161 | + ->method('getType') |
| 162 | + ->willReturn('matchQuery'); |
| 163 | + $filterQuery->expects($this->any()) |
| 164 | + ->method('getType') |
| 165 | + ->willReturn('matchQuery'); |
| 166 | + $filterQuery->expects($this->any()) |
| 167 | + ->method('getType') |
| 168 | + ->willReturn('matchQuery'); |
| 169 | + $this->filterBuilder->expects(($this->any())) |
| 170 | + ->method('build') |
| 171 | + ->willReturn([ |
| 172 | + 'bool' => [ |
| 173 | + 'must' => [], |
| 174 | + ], |
| 175 | + ]); |
| 176 | + |
| 177 | + $this->model->buildQuery($request); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @return array |
| 182 | + */ |
| 183 | + public function buildQueryDataProvider() |
| 184 | + { |
| 185 | + return [ |
| 186 | + [ |
| 187 | + 'matchQuery', \Magento\Framework\Search\Request\Query\Match::class, |
| 188 | + 'query', \Magento\Framework\Search\Request\QueryInterface::class, |
| 189 | + ], |
| 190 | + [ |
| 191 | + 'boolQuery', \Magento\Framework\Search\Request\Query\BoolExpression::class, |
| 192 | + 'query', \Magento\Framework\Search\Request\QueryInterface::class, |
| 193 | + ], |
| 194 | + [ |
| 195 | + 'filteredQuery', \Magento\Framework\Search\Request\Query\Filter::class, |
| 196 | + 'query', \Magento\Framework\Search\Request\QueryInterface::class, |
| 197 | + ], |
| 198 | + [ |
| 199 | + 'filteredQuery', \Magento\Framework\Search\Request\Query\Filter::class, |
| 200 | + 'filter', \Magento\Framework\Search\Request\FilterInterface::class, |
| 201 | + ], |
| 202 | + ]; |
| 203 | + } |
| 204 | +} |
0 commit comments