CheckExpirePersistentQuoteObserverTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 CheckExpirePersistentQuoteObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $sessionMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $checkoutSessionMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $customerSessionMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $persistentHelperMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $observerMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $quoteManagerMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $eventManagerMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\RequestInterface
  44. */
  45. private $requestMock;
  46. /**
  47. * @inheritdoc
  48. */
  49. protected function setUp()
  50. {
  51. $this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  52. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  53. $this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  54. $this->observerMock = $this->createPartialMock(
  55. \Magento\Framework\Event\Observer::class,
  56. ['getControllerAction','__wakeUp']
  57. );
  58. $this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
  59. $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  60. $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
  61. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  62. ->disableOriginalConstructor()
  63. ->setMethods(['getRequestUri', 'getServer'])
  64. ->getMockForAbstractClass();
  65. $this->model = new \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver(
  66. $this->sessionMock,
  67. $this->persistentHelperMock,
  68. $this->quoteManagerMock,
  69. $this->eventManagerMock,
  70. $this->customerSessionMock,
  71. $this->checkoutSessionMock,
  72. $this->requestMock
  73. );
  74. }
  75. public function testExecuteWhenCanNotApplyPersistentData()
  76. {
  77. $this->persistentHelperMock
  78. ->expects($this->once())
  79. ->method('canProcess')
  80. ->with($this->observerMock)
  81. ->willReturn(false);
  82. $this->persistentHelperMock->expects($this->never())->method('isEnabled');
  83. $this->model->execute($this->observerMock);
  84. }
  85. public function testExecuteWhenPersistentIsNotEnabled()
  86. {
  87. $this->persistentHelperMock
  88. ->expects($this->once())
  89. ->method('canProcess')
  90. ->with($this->observerMock)
  91. ->willReturn(true);
  92. $this->persistentHelperMock->expects($this->once())->method('isEnabled')->willReturn(false);
  93. $this->eventManagerMock->expects($this->never())->method('dispatch');
  94. $this->model->execute($this->observerMock);
  95. }
  96. /**
  97. * Test method \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver::execute when persistent is enabled.
  98. *
  99. * @param string $refererUri
  100. * @param string $requestUri
  101. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expireCounter
  102. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $dispatchCounter
  103. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $setCustomerIdCounter
  104. * @return void
  105. * @dataProvider requestDataProvider
  106. */
  107. public function testExecuteWhenPersistentIsEnabled(
  108. string $refererUri,
  109. string $requestUri,
  110. \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expireCounter,
  111. \PHPUnit\Framework\MockObject\Matcher\InvokedCount $dispatchCounter,
  112. \PHPUnit\Framework\MockObject\Matcher\InvokedCount $setCustomerIdCounter
  113. ): void {
  114. $this->persistentHelperMock
  115. ->expects($this->once())
  116. ->method('canProcess')
  117. ->with($this->observerMock)
  118. ->willReturn(true);
  119. $this->persistentHelperMock->expects($this->once())->method('isEnabled')->willReturn(true);
  120. $this->sessionMock->expects($this->once())->method('isPersistent')->willReturn(false);
  121. $this->customerSessionMock
  122. ->expects($this->atLeastOnce())
  123. ->method('isLoggedIn')
  124. ->willReturn(false);
  125. $this->checkoutSessionMock
  126. ->expects($this->atLeastOnce())
  127. ->method('getQuoteId')
  128. ->willReturn(10);
  129. $this->eventManagerMock->expects($dispatchCounter)->method('dispatch');
  130. $this->quoteManagerMock->expects($expireCounter)->method('expire');
  131. $this->customerSessionMock
  132. ->expects($setCustomerIdCounter)
  133. ->method('setCustomerId')
  134. ->with(null)
  135. ->willReturnSelf();
  136. $this->requestMock->expects($this->atLeastOnce())->method('getRequestUri')->willReturn($refererUri);
  137. $this->requestMock
  138. ->expects($this->atLeastOnce())
  139. ->method('getServer')
  140. ->with('HTTP_REFERER')
  141. ->willReturn($requestUri);
  142. $this->model->execute($this->observerMock);
  143. }
  144. /**
  145. * Request Data Provider
  146. *
  147. * @return array
  148. */
  149. public function requestDataProvider()
  150. {
  151. return [
  152. [
  153. 'refererUri' => 'checkout',
  154. 'requestUri' => 'index',
  155. 'expireCounter' => $this->never(),
  156. 'dispatchCounter' => $this->never(),
  157. 'setCustomerIdCounter' => $this->never(),
  158. ],
  159. [
  160. 'refererUri' => 'checkout',
  161. 'requestUri' => 'checkout',
  162. 'expireCounter' => $this->never(),
  163. 'dispatchCounter' => $this->never(),
  164. 'setCustomerIdCounter' => $this->never(),
  165. ],
  166. [
  167. 'refererUri' => 'index',
  168. 'requestUri' => 'checkout',
  169. 'expireCounter' => $this->never(),
  170. 'dispatchCounter' => $this->never(),
  171. 'setCustomerIdCounter' => $this->never(),
  172. ],
  173. [
  174. 'refererUri' => 'index',
  175. 'requestUri' => 'index',
  176. 'expireCounter' => $this->once(),
  177. 'dispatchCounter' => $this->once(),
  178. 'setCustomerIdCounter' => $this->once(),
  179. ],
  180. ];
  181. }
  182. }