Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ViewTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Mview\View;
9 
10 class ViewTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
20  protected $configMock;
21 
25  protected $actionFactoryMock;
26 
30  protected $stateMock;
31 
35  protected $changelogMock;
36 
41 
42  protected function setUp()
43  {
44  $this->configMock = $this->getMockForAbstractClass(
45  \Magento\Framework\Mview\ConfigInterface::class,
46  [],
47  '',
48  false,
49  false,
50  true,
51  ['getView']
52  );
53  $this->actionFactoryMock = $this->createPartialMock(\Magento\Framework\Mview\ActionFactory::class, ['get']);
54  $this->stateMock = $this->createPartialMock(\Magento\Indexer\Model\Mview\View\State::class, ['getViewId',
55  'loadByView',
56  'getVersionId',
57  'setVersionId',
58  'getUpdated',
59  'getStatus',
60  'setStatus',
61  'getMode',
62  'setMode',
63  'save',
64  '__wakeup',
65  ]);
66  $this->changelogMock = $this->createPartialMock(
67  \Magento\Framework\Mview\View\Changelog::class,
68  ['getViewId', 'setViewId', 'create', 'drop', 'getVersion', 'getList', 'clear']
69  );
70  $this->subscriptionFactoryMock = $this->createPartialMock(
71  \Magento\Framework\Mview\View\SubscriptionFactory::class,
72  ['create']
73  );
74  $this->model = new View(
75  $this->configMock,
76  $this->actionFactoryMock,
77  $this->stateMock,
78  $this->changelogMock,
79  $this->subscriptionFactoryMock
80  );
81  }
82 
83  public function testGetActionClass()
84  {
85  $this->model->setData('action_class', 'actionClass');
86  $this->assertEquals('actionClass', $this->model->getActionClass());
87  }
88 
89  public function testGetGroup()
90  {
91  $this->model->setData('group', 'some_group');
92  $this->assertEquals('some_group', $this->model->getGroup());
93  }
94 
95  public function testGetSubscriptions()
96  {
97  $this->model->setData('subscriptions', ['subscription']);
98  $this->assertEquals(['subscription'], $this->model->getSubscriptions());
99  }
100 
101  public function testLoad()
102  {
103  $viewId = 'view_test';
104  $this->configMock->expects(
105  $this->once()
106  )->method(
107  'getView'
108  )->with(
109  $viewId
110  )->will(
111  $this->returnValue($this->getViewData())
112  );
113  $this->assertInstanceOf(\Magento\Framework\Mview\View::class, $this->model->load($viewId));
114  }
115 
120  public function testLoadWithException()
121  {
122  $viewId = 'view_id';
123  $this->configMock->expects(
124  $this->once()
125  )->method(
126  'getView'
127  )->with(
128  $viewId
129  )->will(
130  $this->returnValue($this->getViewData())
131  );
132  $this->model->load($viewId);
133  }
134 
135  public function testSubscribe()
136  {
137  $this->stateMock->expects($this->once())
138  ->method('getMode')
139  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
140  $this->stateMock->expects($this->once())
141  ->method('setMode')
142  ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
143  ->will($this->returnSelf());
144  $this->changelogMock->expects($this->once())
145  ->method('create');
146  $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['create']);
147  $subscriptionMock->expects($this->exactly(1))->method('create');
148  $this->subscriptionFactoryMock->expects(
149  $this->exactly(1)
150  )->method(
151  'create'
152  )->will(
153  $this->returnValue($subscriptionMock)
154  );
155  $this->loadView();
156  $this->model->subscribe();
157  }
158 
159  public function testSubscribeEnabled()
160  {
161  $this->stateMock->expects($this->once())
162  ->method('getMode')
163  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
164  $this->stateMock->expects($this->never())
165  ->method('setMode');
166  $this->changelogMock->expects($this->never())
167  ->method('create');
168  $this->subscriptionFactoryMock->expects($this->never())
169  ->method('create');
170  $this->loadView();
171  $this->model->subscribe();
172  }
173 
177  public function testSubscribeWithException()
178  {
179  $this->stateMock->expects($this->once())
180  ->method('getMode')
181  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
182 
183  $this->changelogMock->expects($this->once())
184  ->method('create')
185  ->will($this->returnCallback(
186  function () {
187  throw new \Exception();
188  }
189  ));
190 
191  $this->loadView();
192  $this->model->subscribe();
193  }
194 
195  public function testUnsubscribe()
196  {
197  $this->stateMock->expects($this->once())
198  ->method('getMode')
199  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
200  $this->stateMock->expects($this->once())
201  ->method('setMode')
202  ->with(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED)
203  ->will($this->returnSelf());
204  $this->changelogMock->expects($this->never())
205  ->method('drop');
206  $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['remove']);
207  $subscriptionMock->expects($this->exactly(1))->method('remove');
208  $this->subscriptionFactoryMock->expects(
209  $this->exactly(1)
210  )->method(
211  'create'
212  )->will(
213  $this->returnValue($subscriptionMock)
214  );
215  $this->loadView();
216  $this->model->unsubscribe();
217  }
218 
219  public function testUnsubscribeDisabled()
220  {
221  $this->stateMock->expects($this->once())
222  ->method('getMode')
223  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
224  $this->stateMock->expects($this->never())
225  ->method('setVersionId');
226  $this->stateMock->expects($this->never())
227  ->method('setMode');
228  $this->changelogMock->expects($this->never())
229  ->method('drop');
230  $this->subscriptionFactoryMock->expects($this->never())
231  ->method('create');
232  $this->loadView();
233  $this->model->unsubscribe();
234  }
235 
240  {
241  $this->stateMock->expects($this->once())
242  ->method('getMode')
243  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
244 
245  $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['remove']);
246  $subscriptionMock->expects($this->exactly(1))
247  ->method('remove')
248  ->will($this->returnCallback(
249  function () {
250  throw new \Exception();
251  }
252  ));
253  $this->subscriptionFactoryMock->expects($this->exactly(1))
254  ->method('create')
255  ->will($this->returnValue($subscriptionMock));
256 
257  $this->loadView();
258  $this->model->unsubscribe();
259  }
260 
261  public function testUpdate()
262  {
263  $currentVersionId = 3;
264  $lastVersionId = 1;
265  $listId = [2, 3];
266 
267  $this->stateMock->expects($this->any())
268  ->method('getViewId')
269  ->will($this->returnValue(1));
270  $this->stateMock->expects($this->once())
271  ->method('getVersionId')
272  ->will($this->returnValue($lastVersionId));
273  $this->stateMock->expects($this->once())
274  ->method('setVersionId')
275  ->will($this->returnSelf());
276  $this->stateMock->expects($this->exactly(2))
277  ->method('getStatus')
278  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE));
279  $this->stateMock->expects($this->exactly(2))
280  ->method('setStatus')
281  ->will($this->returnSelf());
282  $this->stateMock->expects($this->exactly(2))
283  ->method('save')
284  ->will($this->returnSelf());
285 
286  $this->changelogMock->expects(
287  $this->once()
288  )->method(
289  'getVersion'
290  )->will(
291  $this->returnValue($currentVersionId)
292  );
293  $this->changelogMock->expects(
294  $this->once()
295  )->method(
296  'getList'
297  )->with(
298  $lastVersionId,
299  $currentVersionId
300  )->will(
301  $this->returnValue($listId)
302  );
303 
304  $actionMock = $this->createMock(\Magento\Framework\Mview\ActionInterface::class);
305  $actionMock->expects($this->once())->method('execute')->with($listId)->will($this->returnSelf());
306  $this->actionFactoryMock->expects(
307  $this->once()
308  )->method(
309  'get'
310  )->with(
311  'Some\Class\Name'
312  )->will(
313  $this->returnValue($actionMock)
314  );
315 
316  $this->loadView();
317  $this->model->update();
318  }
319 
324  public function testUpdateWithException()
325  {
326  $currentVersionId = 3;
327  $lastVersionId = 1;
328  $listId = [2, 3];
329 
330  $this->stateMock->expects($this->any())
331  ->method('getViewId')
332  ->will($this->returnValue(1));
333  $this->stateMock->expects($this->once())
334  ->method('getVersionId')
335  ->will($this->returnValue($lastVersionId));
336  $this->stateMock->expects($this->never())
337  ->method('setVersionId');
338  $this->stateMock->expects($this->exactly(2))
339  ->method('getStatus')
340  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE));
341  $this->stateMock->expects($this->exactly(2))
342  ->method('setStatus')
343  ->will($this->returnSelf());
344  $this->stateMock->expects($this->exactly(2))
345  ->method('save')
346  ->will($this->returnSelf());
347 
348  $this->changelogMock->expects(
349  $this->once()
350  )->method(
351  'getVersion'
352  )->will(
353  $this->returnValue($currentVersionId)
354  );
355  $this->changelogMock->expects(
356  $this->once()
357  )->method(
358  'getList'
359  )->with(
360  $lastVersionId,
361  $currentVersionId
362  )->will(
363  $this->returnValue($listId)
364  );
365 
366  $actionMock = $this->createPartialMock(\Magento\Framework\Mview\ActionInterface::class, ['execute']);
367  $actionMock->expects($this->once())->method('execute')->with($listId)->will(
368  $this->returnCallback(
369  function () {
370  throw new \Exception('Test exception');
371  }
372  )
373  );
374  $this->actionFactoryMock->expects(
375  $this->once()
376  )->method(
377  'get'
378  )->with(
379  'Some\Class\Name'
380  )->will(
381  $this->returnValue($actionMock)
382  );
383 
384  $this->loadView();
385  $this->model->update();
386  }
387 
388  public function testSuspend()
389  {
390  $this->stateMock->expects($this->once())
391  ->method('getMode')
392  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
393  $this->stateMock->expects($this->once())
394  ->method('setVersionId')
395  ->with(11)
396  ->will($this->returnSelf());
397  $this->stateMock->expects($this->once())
398  ->method('setStatus')
399  ->with(\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED)
400  ->will($this->returnSelf());
401  $this->stateMock->expects($this->once())
402  ->method('save')
403  ->will($this->returnSelf());
404 
405  $this->changelogMock->expects($this->once())
406  ->method('getVersion')
407  ->will($this->returnValue(11));
408 
409  $this->loadView();
410  $this->model->suspend();
411  }
412 
413  public function testSuspendDisabled()
414  {
415  $this->stateMock->expects($this->once())
416  ->method('getMode')
417  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
418  $this->stateMock->expects($this->never())
419  ->method('setVersionId');
420  $this->stateMock->expects($this->never())
421  ->method('setStatus');
422  $this->stateMock->expects($this->never())
423  ->method('save');
424 
425  $this->changelogMock->expects($this->never())
426  ->method('getVersion');
427 
428  $this->loadView();
429  $this->model->suspend();
430  }
431 
432  public function testResume()
433  {
434  $this->stateMock->expects($this->once())
435  ->method('getStatus')
436  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED));
437  $this->stateMock->expects($this->once())
438  ->method('setStatus')
439  ->with(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE)
440  ->will($this->returnSelf());
441  $this->stateMock->expects($this->once())
442  ->method('save')
443  ->will($this->returnSelf());
444 
445  $this->loadView();
446  $this->model->resume();
447  }
448 
454  {
455  $this->stateMock->expects($this->once())
456  ->method('getStatus')
457  ->will($this->returnValue($status));
458  $this->stateMock->expects($this->never())
459  ->method('setStatus');
460  $this->stateMock->expects($this->never())
461  ->method('save');
462 
463  $this->loadView();
464  $this->model->resume();
465  }
466 
471  {
472  return [
473  [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE],
474  [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING],
475  ];
476  }
477 
478  public function testClearChangelog()
479  {
480  $this->stateMock->expects($this->once())
481  ->method('getMode')
482  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
483  $this->stateMock->expects($this->once())
484  ->method('getVersionId')
485  ->will($this->returnValue(11));
486  $this->changelogMock->expects($this->once())
487  ->method('clear')
488  ->with(11)
489  ->will($this->returnValue(true));
490  $this->loadView();
491  $this->model->clearChangelog();
492  }
493 
494  public function testClearChangelogDisabled()
495  {
496  $this->stateMock->expects($this->once())
497  ->method('getMode')
498  ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
499  $this->stateMock->expects($this->never())
500  ->method('getVersionId');
501  $this->changelogMock->expects($this->never())
502  ->method('clear');
503  $this->loadView();
504  $this->model->clearChangelog();
505  }
506 
507  public function testSetState()
508  {
509  $this->model->setState($this->stateMock);
510  $this->assertEquals($this->stateMock, $this->model->getState());
511  }
512 
518  public function testIsEnabled($mode, $result)
519  {
520  $this->stateMock->expects($this->once())
521  ->method('getMode')
522  ->will($this->returnValue($mode));
523  $this->assertEquals($result, $this->model->isEnabled());
524  }
525 
529  public function dataProviderIsEnabled()
530  {
531  return [
532  [\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED, true],
533  [\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED, false],
534  ];
535  }
536 
542  public function testIsIdle($status, $result)
543  {
544  $this->stateMock->expects($this->once())
545  ->method('getStatus')
546  ->will($this->returnValue($status));
547  $this->assertEquals($result, $this->model->isIdle());
548  }
549 
553  public function dataProviderIsIdle()
554  {
555  return [
556  [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, true],
557  [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, false],
558  [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, false],
559  ];
560  }
561 
567  public function testIsWorking($status, $result)
568  {
569  $this->stateMock->expects($this->once())
570  ->method('getStatus')
571  ->will($this->returnValue($status));
572  $this->assertEquals($result, $this->model->isWorking());
573  }
574 
578  public function dataProviderIsWorking()
579  {
580  return [
581  [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, false],
582  [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, true],
583  [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, false],
584  ];
585  }
586 
592  public function testIsSuspended($status, $result)
593  {
594  $this->stateMock->expects($this->once())
595  ->method('getStatus')
596  ->will($this->returnValue($status));
597  $this->assertEquals($result, $this->model->isSuspended());
598  }
599 
603  public function dataProviderIsSuspended()
604  {
605  return [
606  [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, false],
607  [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, false],
608  [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, true],
609  ];
610  }
611 
612  public function testGetUpdated()
613  {
614  $this->stateMock->expects($this->once())
615  ->method('getUpdated')
616  ->will($this->returnValue('some datetime'));
617  $this->assertEquals('some datetime', $this->model->getUpdated());
618  }
619 
620  protected function loadView()
621  {
622  $viewId = 'view_test';
623  $this->configMock->expects(
624  $this->once()
625  )->method(
626  'getView'
627  )->with(
628  $viewId
629  )->will(
630  $this->returnValue($this->getViewData())
631  );
632  $this->model->load($viewId);
633  }
634 
638  protected function getViewData()
639  {
640  return [
641  'view_id' => 'view_test',
642  'action_class' => 'Some\Class\Name',
643  'group' => 'some_group',
644  'subscriptions' => ['some_entity' => ['name' => 'some_entity', 'column' => 'entity_id']]
645  ];
646  }
647 }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$status
Definition: order_status.php:8