Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class IndexerTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
20  protected $configMock;
21 
25  protected $actionFactoryMock;
26 
30  protected $viewMock;
31 
35  protected $stateFactoryMock;
36 
40  protected $indexFactoryMock;
41 
42  protected function setUp()
43  {
44  $this->configMock = $this->getMockForAbstractClass(
45  \Magento\Framework\Indexer\ConfigInterface::class,
46  [],
47  '',
48  false,
49  false,
50  true,
51  ['getIndexer']
52  );
53  $this->actionFactoryMock = $this->createPartialMock(
54  \Magento\Framework\Indexer\ActionFactory::class,
55  ['create']
56  );
57  $this->viewMock = $this->getMockForAbstractClass(
58  \Magento\Framework\Mview\ViewInterface::class,
59  [],
60  '',
61  false,
62  false,
63  true,
64  ['load', 'isEnabled', 'getUpdated', 'getStatus', '__wakeup', 'getId', 'suspend', 'resume']
65  );
66  $this->stateFactoryMock = $this->createPartialMock(
67  \Magento\Indexer\Model\Indexer\StateFactory::class,
68  ['create']
69  );
70  $this->indexFactoryMock = $this->createPartialMock(
71  \Magento\Indexer\Model\Indexer\CollectionFactory::class,
72  ['create']
73  );
74  $structureFactory = $this->getMockBuilder(\Magento\Framework\Indexer\StructureFactory::class)
75  ->disableOriginalConstructor()
76  ->setMethods(['create'])
77  ->getMock();
78 
80  $this->model = new \Magento\Indexer\Model\Indexer(
81  $this->configMock,
82  $this->actionFactoryMock,
83  $structureFactory,
84  $this->viewMock,
85  $this->stateFactoryMock,
86  $this->indexFactoryMock
87  );
88  }
89 
94  public function testLoadWithException()
95  {
96  $indexId = 'indexer_id';
97  $this->configMock->expects(
98  $this->once()
99  )->method(
100  'getIndexer'
101  )->with(
102  $indexId
103  )->will(
104  $this->returnValue($this->getIndexerData())
105  );
106  $this->model->load($indexId);
107  }
108 
109  public function testGetView()
110  {
111  $indexId = 'indexer_internal_name';
112  $this->viewMock->expects($this->once())->method('load')->with('view_test')->will($this->returnSelf());
113  $this->loadIndexer($indexId);
114 
115  $this->assertEquals($this->viewMock, $this->model->getView());
116  }
117 
118  public function testGetState()
119  {
120  $indexId = 'indexer_internal_name';
121  $stateMock = $this->createPartialMock(
122  \Magento\Indexer\Model\Indexer\State::class,
123  ['loadByIndexer', 'getId', '__wakeup']
124  );
125  $stateMock->expects($this->once())->method('loadByIndexer')->with($indexId)->will($this->returnSelf());
126  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
127 
128  $this->loadIndexer($indexId);
129 
130  $this->assertInstanceOf(\Magento\Indexer\Model\Indexer\State::class, $this->model->getState());
131  }
132 
139  public function testGetLatestUpdated($getViewIsEnabled, $getViewGetUpdated, $getStateGetUpdated)
140  {
141  $indexId = 'indexer_internal_name';
142  $this->loadIndexer($indexId);
143 
144  $this->viewMock->expects($this->any())->method('getId')->will($this->returnValue(1));
145  $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue($getViewIsEnabled));
146  $this->viewMock->expects($this->any())->method('getUpdated')->will($this->returnValue($getViewGetUpdated));
147 
148  $stateMock = $this->createPartialMock(
149  \Magento\Indexer\Model\Indexer\State::class,
150  ['load', 'getId', 'setIndexerId', '__wakeup', 'getUpdated']
151  );
152 
153  $stateMock->expects($this->any())->method('getUpdated')->will($this->returnValue($getStateGetUpdated));
154  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
155 
156  if ($getViewIsEnabled && $getViewGetUpdated) {
157  if (!$getStateGetUpdated) {
158  $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
159  } else {
160  if ($getViewGetUpdated == $getStateGetUpdated) {
161  $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
162  } else {
163  $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
164  }
165  }
166  } else {
167  $this->assertEquals($getStateGetUpdated, $this->model->getLatestUpdated());
168  }
169  }
170 
175  {
176  return [
177  [false, '06-Jan-1944', '06-Jan-1944'],
178  [false, '', '06-Jan-1944'],
179  [false, '06-Jan-1944', ''],
180  [false, '', ''],
181  [true, '06-Jan-1944', '06-Jan-1944'],
182  [true, '', '06-Jan-1944'],
183  [true, '06-Jan-1944', ''],
184  [true, '', ''],
185  [true, '06-Jan-1944', '05-Jan-1944']
186  ];
187  }
188 
189  public function testReindexAll()
190  {
191  $indexId = 'indexer_internal_name';
192  $this->loadIndexer($indexId);
193 
194  $stateMock = $this->createPartialMock(
195  \Magento\Indexer\Model\Indexer\State::class,
196  ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
197  );
198  $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
199  $stateMock->expects($this->never())->method('setIndexerId');
200  $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
201  $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
202  $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
203  $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
204  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
205 
206  $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
207  $this->viewMock->expects($this->once())->method('suspend');
208  $this->viewMock->expects($this->once())->method('resume');
209 
210  $actionMock = $this->createPartialMock(
211  \Magento\Framework\Indexer\ActionInterface::class,
212  ['executeFull', 'executeList', 'executeRow']
213  );
214  $this->actionFactoryMock->expects(
215  $this->once()
216  )->method(
217  'create'
218  )->with(
219  'Some\Class\Name'
220  )->will(
221  $this->returnValue($actionMock)
222  );
223 
224  $this->model->reindexAll();
225  }
226 
231  public function testReindexAllWithException()
232  {
233  $indexId = 'indexer_internal_name';
234  $this->loadIndexer($indexId);
235 
236  $stateMock = $this->createPartialMock(
237  \Magento\Indexer\Model\Indexer\State::class,
238  ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
239  );
240  $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
241  $stateMock->expects($this->never())->method('setIndexerId');
242  $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
243  $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
244  $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
245  $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
246  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
247 
248  $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
249  $this->viewMock->expects($this->never())->method('suspend');
250  $this->viewMock->expects($this->once())->method('resume');
251 
252  $actionMock = $this->createPartialMock(
253  \Magento\Framework\Indexer\ActionInterface::class,
254  ['executeFull', 'executeList', 'executeRow']
255  );
256  $actionMock->expects($this->once())->method('executeFull')->will(
257  $this->returnCallback(
258  function () {
259  throw new \Exception('Test exception');
260  }
261  )
262  );
263  $this->actionFactoryMock->expects(
264  $this->once()
265  )->method(
266  'create'
267  )->with(
268  'Some\Class\Name'
269  )->will(
270  $this->returnValue($actionMock)
271  );
272 
273  $this->model->reindexAll();
274  }
275 
280  public function testReindexAllWithError()
281  {
282 
283  $indexId = 'indexer_internal_name';
284  $this->loadIndexer($indexId);
285 
286  $stateMock = $this->createPartialMock(
287  \Magento\Indexer\Model\Indexer\State::class,
288  ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
289  );
290  $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
291  $stateMock->expects($this->never())->method('setIndexerId');
292  $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
293  $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
294  $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
295  $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
296  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
297 
298  $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
299  $this->viewMock->expects($this->never())->method('suspend');
300  $this->viewMock->expects($this->once())->method('resume');
301 
302  $actionMock = $this->createPartialMock(
303  \Magento\Framework\Indexer\ActionInterface::class,
304  ['executeFull', 'executeList', 'executeRow']
305  );
306  $actionMock->expects($this->once())->method('executeFull')->will(
307  $this->returnCallback(
308  function () {
309  throw new \Error('Test Engine Error');
310  }
311  )
312  );
313  $this->actionFactoryMock->expects(
314  $this->once()
315  )->method(
316  'create'
317  )->with(
318  'Some\Class\Name'
319  )->will(
320  $this->returnValue($actionMock)
321  );
322 
323  $this->model->reindexAll();
324  }
325 
329  protected function getIndexerData()
330  {
331  return [
332  'indexer_id' => 'indexer_internal_name',
333  'view_id' => 'view_test',
334  'action_class' => 'Some\Class\Name',
335  'title' => 'Indexer public name',
336  'description' => 'Indexer public description'
337  ];
338  }
339 
343  protected function loadIndexer($indexId)
344  {
345  $this->configMock->expects(
346  $this->once()
347  )->method(
348  'getIndexer'
349  )->with(
350  $indexId
351  )->will(
352  $this->returnValue($this->getIndexerData())
353  );
354  $this->model->load($indexId);
355  }
356 
357  public function testGetTitle()
358  {
359  $result = 'Test Result';
360  $this->model->setTitle($result);
361  $this->assertEquals($result, $this->model->getTitle());
362  }
363 
364  public function testGetDescription()
365  {
366  $result = 'Test Result';
367  $this->model->setDescription($result);
368  $this->assertEquals($result, $this->model->getDescription());
369  }
370 
371  public function testSetState()
372  {
373  $stateMock = $this->createPartialMock(
374  \Magento\Indexer\Model\Indexer\State::class,
375  ['loadByIndexer', 'getId', '__wakeup']
376  );
377 
378  $this->model->setState($stateMock);
379  $this->assertInstanceOf(\Magento\Indexer\Model\Indexer\State::class, $this->model->getState());
380  }
381 
382  public function testIsScheduled()
383  {
384  $result = true;
385  $this->viewMock->expects($this->once())->method('load')->will($this->returnSelf());
386  $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue($result));
387  $this->assertEquals($result, $this->model->isScheduled());
388  }
389 
395  public function testSetScheduled($scheduled, $method)
396  {
397  $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
398 
399  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
400  $this->viewMock->expects($this->once())->method('load')->will($this->returnSelf());
401  $this->viewMock->expects($this->once())->method($method)->will($this->returnValue(true));
402  $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
403  $this->model->setScheduled($scheduled);
404  }
405 
409  public function setScheduledDataProvider()
410  {
411  return [
412  [true, 'subscribe'],
413  [false, 'unsubscribe']
414  ];
415  }
416 
417  public function testGetStatus()
418  {
420  $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'getStatus']);
421 
422  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
423  $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue($status));
424  $this->assertEquals($status, $this->model->getStatus());
425  }
426 
432  public function testStatus($method, $status)
433  {
434  $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'getStatus']);
435 
436  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
437  $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue($status));
438  $this->assertEquals(true, $this->model->$method());
439  }
440 
444  public function statusDataProvider()
445  {
446  return [
447  ['isValid', StateInterface::STATUS_VALID],
448  ['isInvalid', StateInterface::STATUS_INVALID],
449  ['isWorking', StateInterface::STATUS_WORKING]
450  ];
451  }
452 
453  public function testInvalidate()
454  {
455  $stateMock = $this->createPartialMock(
456  \Magento\Indexer\Model\Indexer\State::class,
457  ['load', 'setStatus', 'save']
458  );
459 
460  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
461  $stateMock->expects($this->once())->method('setStatus')->with(StateInterface::STATUS_INVALID)->will(
462  $this->returnSelf()
463  );
464  $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
465  $this->model->invalidate();
466  }
467 
468  public function testReindexRow()
469  {
470  $id = 1;
471 
472  $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
473  $actionMock = $this->createPartialMock(
474  \Magento\Framework\Indexer\ActionInterface::class,
475  ['executeFull', 'executeList', 'executeRow']
476  );
477 
478  $this->actionFactoryMock->expects(
479  $this->once()
480  )->method(
481  'create'
482  )->will(
483  $this->returnValue($actionMock)
484  );
485 
486  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
487  $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
488  $actionMock->expects($this->once())->method('executeRow')->with($id)->will($this->returnSelf());
489  $this->model->reindexRow($id);
490  }
491 
492  public function testReindexList()
493  {
494  $ids = [1];
495 
496  $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
497  $actionMock = $this->createPartialMock(
498  \Magento\Framework\Indexer\ActionInterface::class,
499  ['executeFull', 'executeList', 'executeRow']
500  );
501 
502  $this->actionFactoryMock->expects(
503  $this->once()
504  )->method(
505  'create'
506  )->will(
507  $this->returnValue($actionMock)
508  );
509 
510  $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
511  $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
512  $actionMock->expects($this->once())->method('executeList')->with($ids)->will($this->returnSelf());
513  $this->model->reindexList($ids);
514  }
515 }
testGetLatestUpdated($getViewIsEnabled, $getViewGetUpdated, $getStateGetUpdated)
$id
Definition: fieldset.phtml:14
$status
Definition: order_status.php:8
$method
Definition: info.phtml:13