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
6 declare(strict_types=1);
7 
9 
20 
26 class IntervalTest extends \PHPUnit\Framework\TestCase
27 {
31  private $model;
32 
36  private $connectionManager;
37 
41  private $fieldMapper;
42 
46  private $clientConfig;
47 
51  private $storeManager;
52 
56  private $customerSession;
57 
61  private $clientMock;
62 
66  private $storeMock;
67 
71  private $searchIndexNameResolver;
72 
76  protected function setUp()
77  {
78  $this->connectionManager = $this->getMockBuilder(ConnectionManager::class)
79  ->setMethods(['getConnection'])
80  ->disableOriginalConstructor()
81  ->getMock();
82  $this->fieldMapper = $this->getMockBuilder(FieldMapperInterface::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85  $this->clientConfig = $this->getMockBuilder(Config::class)
86  ->setMethods([
87  'getIndexName',
88  'getEntityType',
89  ])
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $this->customerSession = $this->getMockBuilder(CustomerSession::class)
96  ->setMethods(['getCustomerGroupId'])
97  ->disableOriginalConstructor()
98  ->getMock();
99  $this->customerSession->expects($this->any())
100  ->method('getCustomerGroupId')
101  ->willReturn(1);
102  $this->storeMock = $this->getMockBuilder(StoreInterface::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105  $this->searchIndexNameResolver = $this
106  ->getMockBuilder(SearchIndexNameResolver::class)
107  ->disableOriginalConstructor()
108  ->getMock();
109  $this->storeMock->expects($this->any())
110  ->method('getWebsiteId')
111  ->willReturn(1);
112  $this->storeMock->expects($this->any())
113  ->method('getId')
114  ->willReturn(1);
115  $this->clientConfig->expects($this->any())
116  ->method('getIndexName')
117  ->willReturn('indexName');
118  $this->clientConfig->expects($this->any())
119  ->method('getEntityType')
120  ->willReturn('product');
121  $this->clientMock = $this->getMockBuilder(ElasticsearchClient::class)
122  ->setMethods(['query'])
123  ->disableOriginalConstructor()
124  ->getMock();
125  $this->connectionManager->expects($this->any())
126  ->method('getConnection')
127  ->willReturn($this->clientMock);
128 
129  $objectManagerHelper = new ObjectManagerHelper($this);
130  $this->model = $objectManagerHelper->getObject(
131  Interval::class,
132  [
133  'connectionManager' => $this->connectionManager,
134  'fieldMapper' => $this->fieldMapper,
135  'clientConfig' => $this->clientConfig,
136  'searchIndexNameResolver' => $this->searchIndexNameResolver,
137  'fieldName' => 'price_0_1',
138  'storeId' => 1,
139  'entityIds' => [265, 313, 281],
140  ]
141  );
142  }
143 
154  public function testLoad(
155  string $limit,
156  string $offset,
157  string $lower,
158  string $upper,
159  array $queryResult,
160  array $expected
161  ): void {
162  $this->processQuery($queryResult);
163 
164  $this->assertEquals(
165  $expected,
166  $this->model->load($limit, $offset, $lower, $upper)
167  );
168  }
169 
179  public function testLoadPrev(string $data, string $index, string $lower, array $queryResult, $expected): void
180  {
181  $this->processQuery($queryResult);
182 
183  $this->assertEquals(
184  $expected,
185  $this->model->loadPrevious($data, $index, $lower)
186  );
187  }
188 
198  public function testLoadNext(string $data, string $rightIndex, string $upper, array $queryResult, $expected): void
199  {
200  $this->processQuery($queryResult);
201 
202  $this->assertEquals(
203  $expected,
204  $this->model->loadNext($data, $rightIndex, $upper)
205  );
206  }
207 
212  private function processQuery(array $queryResult): void
213  {
214  $this->searchIndexNameResolver->expects($this->any())
215  ->method('getIndexName')
216  ->willReturn('magento2_product_1');
217  $this->clientConfig->expects($this->any())
218  ->method('getEntityType')
219  ->willReturn('document');
220  $this->clientMock->expects($this->any())
221  ->method('query')
222  ->willReturn($queryResult);
223  }
224 
228  public function loadParamsProvider(): array
229  {
230  return [
231  [
232  'limit' => '6',
233  'offset' => '2',
234  'lower' => '24',
235  'upper' => '42',
236  'queryResult' => [
237  'hits' => [
238  'hits' => [
239  [
240  'fields' => [
241  'price_0_1' => [25],
242  ],
243  ],
244  ],
245  ],
246  ],
247  'expected' => [25],
248  ],
249  ];
250  }
251 
255  public function loadPrevParamsProvider(): array
256  {
257  return [
258  [
259  'data' => '24',
260  'rightIndex' => '1',
261  'upper' => '24',
262  'queryResult' => [
263  'hits' => [
264  'total'=> '1',
265  'hits' => [
266  [
267  'fields' => [
268  'price_0_1' => ['25'],
269  ],
270  ],
271  ],
272  ],
273  ],
274  'expected' => ['25.0'],
275  ],
276  [
277  'data' => '24',
278  'rightIndex' => '1',
279  'upper' => '24',
280  'queryResult' => [
281  'hits' => ['total'=> '0'],
282  ],
283  'expected' => false,
284  ],
285  ];
286  }
287 
291  public function loadNextParamsProvider(): array
292  {
293  return [
294  [
295  'data' => '24',
296  'rightIndex' => '2',
297  'upper' => '42',
298  'queryResult' => [
299  'hits' => [
300  'total'=> '1',
301  'hits' => [
302  [
303  'fields' => [
304  'price_0_1' => ['25'],
305  ],
306  ],
307  ],
308  ],
309  ],
310  'expected' => ['25.0'],
311  ],
312  [
313  'data' => '24',
314  'rightIndex' => '2',
315  'upper' => '42',
316  'queryResult' => [
317  'hits' => ['total'=> '0'],
318  ],
319  'expected' => false,
320  ],
321  ];
322  }
323 }
testLoadPrev(string $data, string $index, string $lower, array $queryResult, $expected)
testLoadNext(string $data, string $rightIndex, string $upper, array $queryResult, $expected)
testLoad(string $limit, string $offset, string $lower, string $upper, array $queryResult, array $expected)
$index
Definition: list.phtml:44