PreventClearCheckoutSessionObserverTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Persistent\Test\Unit\Observer;
  8. class PreventClearCheckoutSessionObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\PreventClearCheckoutSessionObserver
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $customerSessionMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $sessionHelperMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $helperMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $observerMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $eventMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $actionMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $customerMock;
  42. protected function setUp()
  43. {
  44. $eventMethods = ['getControllerAction', 'dispatch', '__wakeUp'];
  45. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  46. $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  47. $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  48. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  49. $this->eventMock = $this->createPartialMock(\Magento\Framework\Event::class, $eventMethods);
  50. $this->actionMock = $this->createMock(\Magento\Persistent\Controller\Index::class);
  51. $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($this->eventMock));
  52. $this->model = new \Magento\Persistent\Observer\PreventClearCheckoutSessionObserver(
  53. $this->sessionHelperMock,
  54. $this->helperMock,
  55. $this->customerSessionMock
  56. );
  57. }
  58. public function testExecuteWhenSessionIsPersist()
  59. {
  60. $this->eventMock
  61. ->expects($this->once())
  62. ->method('getControllerAction')
  63. ->will($this->returnValue($this->actionMock));
  64. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  65. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  66. $this->helperMock->expects($this->never())->method('isShoppingCartPersist');
  67. $this->actionMock->expects($this->once())->method('setClearCheckoutSession')->with(false);
  68. $this->model->execute($this->observerMock);
  69. }
  70. public function testExecuteWhenShoppingCartIsPersist()
  71. {
  72. $this->eventMock
  73. ->expects($this->once())
  74. ->method('getControllerAction')
  75. ->will($this->returnValue($this->actionMock));
  76. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  77. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
  78. $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(false));
  79. $this->actionMock->expects($this->once())->method('setClearCheckoutSession')->with(false);
  80. $this->model->execute($this->observerMock);
  81. }
  82. public function testExecuteWhenActionIsNotPersistent()
  83. {
  84. $this->eventMock
  85. ->expects($this->once())
  86. ->method('getControllerAction');
  87. $this->sessionHelperMock->expects($this->never())->method('isPersistent');
  88. $this->actionMock->expects($this->never())->method('setClearCheckoutSession');
  89. $this->model->execute($this->observerMock);
  90. }
  91. }