Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IntervalTest.php
Go to the documentation of this file.
1 <?php
7 
15 use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
18 
22 class IntervalTest extends \PHPUnit\Framework\TestCase
23 {
27  protected $model;
28 
32  protected $connectionManager;
33 
37  protected $fieldMapper;
38 
42  protected $clientConfig;
43 
47  protected $storeManager;
48 
52  protected $customerSession;
53 
57  protected $clientMock;
58 
62  protected $storeMock;
63 
68 
74  protected function setUp()
75  {
76  $this->connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
77  ->setMethods(['getConnection'])
78  ->disableOriginalConstructor()
79  ->getMock();
80  $this->fieldMapper = $this->getMockBuilder(\Magento\Elasticsearch\Model\Adapter\FieldMapperInterface::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->clientConfig = $this->getMockBuilder(\Magento\Elasticsearch\Model\Config::class)
84  ->setMethods([
85  'getIndexName',
86  'getEntityType',
87  ])
88  ->disableOriginalConstructor()
89  ->getMock();
90  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
91  ->disableOriginalConstructor()
92  ->getMock();
93  $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
94  ->setMethods(['getCustomerGroupId'])
95  ->disableOriginalConstructor()
96  ->getMock();
97  $this->customerSession->expects($this->any())
98  ->method('getCustomerGroupId')
99  ->willReturn(1);
100  $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103  $this->searchIndexNameResolver = $this
104  ->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver::class)
105  ->disableOriginalConstructor()
106  ->getMock();
107  $this->storeMock->expects($this->any())
108  ->method('getWebsiteId')
109  ->willReturn(1);
110  $this->storeMock->expects($this->any())
111  ->method('getId')
112  ->willReturn(1);
113  $this->clientConfig->expects($this->any())
114  ->method('getIndexName')
115  ->willReturn('indexName');
116  $this->clientConfig->expects($this->any())
117  ->method('getEntityType')
118  ->willReturn('product');
119  $this->clientMock = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
120  ->setMethods(['query'])
121  ->disableOriginalConstructor()
122  ->getMock();
123  $this->connectionManager->expects($this->any())
124  ->method('getConnection')
125  ->willReturn($this->clientMock);
126 
127  $objectManagerHelper = new ObjectManagerHelper($this);
128  $this->model = $objectManagerHelper->getObject(
129  \Magento\Elasticsearch\SearchAdapter\Aggregation\Interval::class,
130  [
131  'connectionManager' => $this->connectionManager,
132  'fieldMapper' => $this->fieldMapper,
133  'clientConfig' => $this->clientConfig,
134  'searchIndexNameResolver' => $this->searchIndexNameResolver,
135  'fieldName' => 'price_0_1',
136  'storeId' => 1,
137  'entityIds' => [265, 313, 281]
138  ]
139  );
140  }
141 
150  public function testLoad($limit, $offset, $lower, $upper)
151  {
152  $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
153  ->disableOriginalConstructor()
154  ->getMock();
155  $this->searchIndexNameResolver->expects($this->any())
156  ->method('getIndexName')
157  ->willReturn('magento2_product_1');
158  $this->clientConfig->expects($this->any())
159  ->method('getEntityType')
160  ->willReturn('document');
161 
162  $expectedResult = [25];
163 
164  $this->clientMock->expects($this->once())
165  ->method('query')
166  ->willReturn([
167  'hits' => [
168  'hits' => [
169  [
170  'fields' => [
171 
172  'price_0_1' => [25],
173 
174  ],
175  ],
176  ],
177  ],
178  ]);
179  $this->assertEquals(
180  $expectedResult,
181  $this->model->load($limit, $offset, $lower, $upper)
182  );
183  }
184 
192  public function testLoadPrevArray($data, $index, $lower)
193  {
194  $queryResult = [
195  'hits' => [
196  'total'=> '1',
197  'hits' => [
198  [
199  'fields' => [
200  'price_0_1' => ['25']
201  ]
202  ],
203  ],
204  ],
205  ];
206 
207  $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
208  ->disableOriginalConstructor()
209  ->getMock();
210  $this->searchIndexNameResolver->expects($this->any())
211  ->method('getIndexName')
212  ->willReturn('magento2_product_1');
213  $this->clientConfig->expects($this->any())
214  ->method('getEntityType')
215  ->willReturn('document');
216 
217  $expectedResult = ['25.0'];
218 
219  $this->clientMock->expects($this->any())
220  ->method('query')
221  ->willReturn($queryResult);
222  $this->assertEquals(
223  $expectedResult,
224  $this->model->loadPrevious($data, $index, $lower)
225  );
226  }
227 
235  public function testLoadPrevFalse($data, $index, $lower)
236  {
237  $queryResult = ['hits' => ['total'=> '0']];
238 
239  $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
240  ->disableOriginalConstructor()
241  ->getMock();
242  $this->searchIndexNameResolver->expects($this->any())
243  ->method('getIndexName')
244  ->willReturn('magento2_product_1');
245  $this->clientConfig->expects($this->any())
246  ->method('getEntityType')
247  ->willReturn('document');
248 
249  $this->clientMock->expects($this->any())
250  ->method('query')
251  ->willReturn($queryResult);
252  $this->assertFalse(
253  $this->model->loadPrevious($data, $index, $lower)
254  );
255  }
256 
264  public function testLoadNextArray($data, $rightIndex, $upper)
265  {
266  $queryResult = [
267  'hits' => [
268  'total'=> '1',
269  'hits' => [
270  [
271  'fields' => [
272  'price_0_1' => ['25']
273  ]
274  ],
275  ],
276  ]
277  ];
278 
279  $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
280  ->disableOriginalConstructor()
281  ->getMock();
282  $this->searchIndexNameResolver->expects($this->any())
283  ->method('getIndexName')
284  ->willReturn('magento2_product_1');
285  $this->clientConfig->expects($this->any())
286  ->method('getEntityType')
287  ->willReturn('document');
288 
289  $expectedResult = ['25.0'];
290 
291  $this->clientMock->expects($this->any())
292  ->method('query')
293  ->willReturn($queryResult);
294  $this->assertEquals(
295  $expectedResult,
296  $this->model->loadNext($data, $rightIndex, $upper)
297  );
298  }
299 
307  public function testLoadNextFalse($data, $rightIndex, $upper)
308  {
309  $queryResult = ['hits' => ['total'=> '0']];
310 
311  $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
312  ->disableOriginalConstructor()
313  ->getMock();
314  $this->searchIndexNameResolver->expects($this->any())
315  ->method('getIndexName')
316  ->willReturn('magento2_product_1');
317  $this->clientConfig->expects($this->any())
318  ->method('getEntityType')
319  ->willReturn('document');
320 
321  $this->clientMock->expects($this->any())
322  ->method('query')
323  ->willReturn($queryResult);
324  $this->assertFalse(
325  $this->model->loadNext($data, $rightIndex, $upper)
326  );
327  }
328 
332  public static function loadParamsProvider()
333  {
334  return [
335  ['6', '2', '24', '42'],
336  ];
337  }
338 
342  public static function loadPrevParamsProvider()
343  {
344  return [
345  ['24', '1', '24'],
346  ];
347  }
348 
352  public static function loadNextParamsProvider()
353  {
354  return [
355  ['24', '2', '42'],
356  ];
357  }
358 }
$index
Definition: list.phtml:44