|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Elasticsearch\Test\Unit\Elasticsearch5\SearchAdapter\Query; |
| 10 | + |
| 11 | +use Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Query\Builder; |
| 12 | +use Magento\Elasticsearch\Model\Config; |
| 13 | +use Magento\Elasticsearch\SearchAdapter\Query\Builder\Aggregation as AggregationBuilder; |
| 14 | +use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver; |
| 15 | +use Magento\Framework\App\ScopeInterface; |
| 16 | +use Magento\Framework\App\ScopeResolverInterface; |
| 17 | +use Magento\Framework\Search\Request\Dimension; |
| 18 | +use Magento\Framework\Search\RequestInterface; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 24 | + */ |
| 25 | +class BuilderTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var Builder |
| 29 | + */ |
| 30 | + protected $model; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Config|MockObject |
| 34 | + */ |
| 35 | + protected $clientConfig; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var SearchIndexNameResolver|MockObject |
| 39 | + */ |
| 40 | + protected $searchIndexNameResolver; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var AggregationBuilder|MockObject |
| 44 | + */ |
| 45 | + protected $aggregationBuilder; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var RequestInterface|MockObject |
| 49 | + */ |
| 50 | + protected $request; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var ScopeResolverInterface|MockObject |
| 54 | + */ |
| 55 | + protected $scopeResolver; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var ScopeInterface|MockObject |
| 59 | + */ |
| 60 | + protected $scopeInterface; |
| 61 | + |
| 62 | + /** |
| 63 | + * Setup method |
| 64 | + * |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + protected function setUp(): void |
| 68 | + { |
| 69 | + $this->clientConfig = $this->getMockBuilder(Config::class) |
| 70 | + ->onlyMethods(['getEntityType']) |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->getMock(); |
| 73 | + $this->searchIndexNameResolver = $this |
| 74 | + ->getMockBuilder(SearchIndexNameResolver::class) |
| 75 | + ->onlyMethods(['getIndexName']) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + $this->aggregationBuilder = $this |
| 79 | + ->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\Query\Builder\Aggregation::class) |
| 80 | + ->onlyMethods(['build']) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->getMock(); |
| 83 | + $this->request = $this->getMockBuilder(RequestInterface::class) |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->getMockForAbstractClass(); |
| 86 | + $this->scopeResolver = $this->getMockForAbstractClass( |
| 87 | + ScopeResolverInterface::class, |
| 88 | + [], |
| 89 | + '', |
| 90 | + false |
| 91 | + ); |
| 92 | + $this->scopeInterface = $this->getMockForAbstractClass( |
| 93 | + ScopeInterface::class, |
| 94 | + [], |
| 95 | + '', |
| 96 | + false |
| 97 | + ); |
| 98 | + |
| 99 | + $this->model = new Builder( |
| 100 | + $this->clientConfig, |
| 101 | + $this->searchIndexNameResolver, |
| 102 | + $this->aggregationBuilder, |
| 103 | + $this->scopeResolver, |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Test initQuery() method |
| 109 | + */ |
| 110 | + public function testInitQuery() |
| 111 | + { |
| 112 | + $dimensionValue = 1; |
| 113 | + $dimension = $this->getMockBuilder(Dimension::class) |
| 114 | + ->onlyMethods(['getValue']) |
| 115 | + ->disableOriginalConstructor() |
| 116 | + ->getMock(); |
| 117 | + |
| 118 | + $this->request->expects($this->once()) |
| 119 | + ->method('getDimensions') |
| 120 | + ->willReturn([$dimension]); |
| 121 | + $dimension->expects($this->once()) |
| 122 | + ->method('getValue') |
| 123 | + ->willReturn($dimensionValue); |
| 124 | + $this->scopeResolver->expects($this->once()) |
| 125 | + ->method('getScope') |
| 126 | + ->willReturn($this->scopeInterface); |
| 127 | + $this->scopeInterface->expects($this->once()) |
| 128 | + ->method('getId') |
| 129 | + ->willReturn($dimensionValue); |
| 130 | + $this->request->expects($this->once()) |
| 131 | + ->method('getFrom') |
| 132 | + ->willReturn(0); |
| 133 | + $this->request->expects($this->once()) |
| 134 | + ->method('getSize') |
| 135 | + ->willReturn(10); |
| 136 | + $this->request->expects($this->once()) |
| 137 | + ->method('getIndex') |
| 138 | + ->willReturn('catalogsearch_fulltext'); |
| 139 | + $this->searchIndexNameResolver->expects($this->once()) |
| 140 | + ->method('getIndexName') |
| 141 | + ->willReturn('indexName'); |
| 142 | + $this->clientConfig->expects($this->once()) |
| 143 | + ->method('getEntityType') |
| 144 | + ->willReturn('document'); |
| 145 | + $this->model->initQuery($this->request); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Test initQuery() method with update from value |
| 150 | + */ |
| 151 | + public function testInitQueryLimitFrom() |
| 152 | + { |
| 153 | + $dimensionValue = 1; |
| 154 | + $dimension = $this->getMockBuilder(Dimension::class) |
| 155 | + ->onlyMethods(['getValue']) |
| 156 | + ->disableOriginalConstructor() |
| 157 | + ->getMock(); |
| 158 | + |
| 159 | + $this->request->expects($this->once()) |
| 160 | + ->method('getDimensions') |
| 161 | + ->willReturn([$dimension]); |
| 162 | + $dimension->expects($this->once()) |
| 163 | + ->method('getValue') |
| 164 | + ->willReturn($dimensionValue); |
| 165 | + $this->scopeResolver->expects($this->once()) |
| 166 | + ->method('getScope') |
| 167 | + ->willReturn($this->scopeInterface); |
| 168 | + $this->scopeInterface->expects($this->once()) |
| 169 | + ->method('getId') |
| 170 | + ->willReturn($dimensionValue); |
| 171 | + $this->request->expects($this->once()) |
| 172 | + ->method('getFrom') |
| 173 | + ->willReturn(PHP_INT_MAX); |
| 174 | + $this->request->expects($this->once()) |
| 175 | + ->method('getSize') |
| 176 | + ->willReturn(10); |
| 177 | + $this->request->expects($this->once()) |
| 178 | + ->method('getIndex') |
| 179 | + ->willReturn('catalogsearch_fulltext'); |
| 180 | + $this->searchIndexNameResolver->expects($this->once()) |
| 181 | + ->method('getIndexName') |
| 182 | + ->willReturn('indexName'); |
| 183 | + $this->clientConfig->expects($this->once()) |
| 184 | + ->method('getEntityType') |
| 185 | + ->willReturn('document'); |
| 186 | + $query = $this->model->initQuery($this->request); |
| 187 | + $this->assertLessThanOrEqual(2147483647, $query['body']['from']); |
| 188 | + } |
| 189 | +} |
0 commit comments