MakePersistentQuoteGuestObserverTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 MakePersistentQuoteGuestObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\MakePersistentQuoteGuestObserver
  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 $quoteManagerMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $eventManagerMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $actionMock;
  42. protected function setUp()
  43. {
  44. $this->actionMock = $this->createMock(\Magento\Persistent\Controller\Index::class);
  45. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  46. $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  47. $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  48. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  49. $this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
  50. $this->eventManagerMock =
  51. $this->createPartialMock(\Magento\Framework\Event::class, ['getControllerAction', '__wakeUp']);
  52. $this->observerMock
  53. ->expects($this->once())
  54. ->method('getEvent')
  55. ->will($this->returnValue($this->eventManagerMock));
  56. $this->model = new \Magento\Persistent\Observer\MakePersistentQuoteGuestObserver(
  57. $this->sessionHelperMock,
  58. $this->helperMock,
  59. $this->customerSessionMock,
  60. $this->quoteManagerMock
  61. );
  62. }
  63. public function testExecute()
  64. {
  65. $this->eventManagerMock
  66. ->expects($this->once())
  67. ->method('getControllerAction')
  68. ->will($this->returnValue($this->actionMock));
  69. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  70. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  71. $this->helperMock->expects($this->never())->method('isShoppingCartPersist');
  72. $this->quoteManagerMock->expects($this->once())->method('setGuest')->with(true);
  73. $this->model->execute($this->observerMock);
  74. }
  75. public function testExecuteWhenShoppingCartIsPersist()
  76. {
  77. $this->eventManagerMock
  78. ->expects($this->once())
  79. ->method('getControllerAction')
  80. ->will($this->returnValue($this->actionMock));
  81. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  82. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
  83. $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(true));
  84. $this->quoteManagerMock->expects($this->once())->method('setGuest')->with(true);
  85. $this->model->execute($this->observerMock);
  86. }
  87. public function testExecuteWhenShoppingCartIsNotPersist()
  88. {
  89. $this->eventManagerMock
  90. ->expects($this->once())
  91. ->method('getControllerAction')
  92. ->will($this->returnValue($this->actionMock));
  93. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  94. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
  95. $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(false));
  96. $this->quoteManagerMock->expects($this->never())->method('setGuest');
  97. $this->model->execute($this->observerMock);
  98. }
  99. }