EmulateQuoteObserverTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class EmulateQuoteObserverTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Persistent\Observer\EmulateQuoteObserver
  15. */
  16. protected $model;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $customerRepository;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $customerSessionMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $sessionHelperMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $helperMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $observerMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $checkoutSessionMock;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $eventMock;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $requestMock;
  49. /**
  50. * @var \PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $customerMock;
  53. /**
  54. * @var \PHPUnit_Framework_MockObject_MockObject
  55. */
  56. protected $sessionMock;
  57. protected function setUp()
  58. {
  59. $eventMethods = ['getRequest', 'dispatch', '__wakeUp'];
  60. $this->customerRepository = $this->getMockForAbstractClass(
  61. \Magento\Customer\Api\CustomerRepositoryInterface::class,
  62. [],
  63. '',
  64. false
  65. );
  66. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  67. $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  68. $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  69. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  70. $this->checkoutSessionMock = $this->createPartialMock(
  71. \Magento\Checkout\Model\Session::class,
  72. ['isLoggedIn', 'setCustomerData', 'hasQuote', 'getQuote']
  73. );
  74. $this->eventMock = $this->createPartialMock(\Magento\Framework\Event::class, $eventMethods);
  75. $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  76. $this->customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  77. $this->sessionMock =
  78. $this->createPartialMock(\Magento\Persistent\Model\Session::class, ['getCustomerId', '__wakeUp']);
  79. $this->model = new \Magento\Persistent\Observer\EmulateQuoteObserver(
  80. $this->sessionHelperMock,
  81. $this->helperMock,
  82. $this->checkoutSessionMock,
  83. $this->customerSessionMock,
  84. $this->customerRepository
  85. );
  86. }
  87. public function testExecuteWhenCannotProcess()
  88. {
  89. $this->helperMock
  90. ->expects($this->once())
  91. ->method('canProcess')
  92. ->with($this->observerMock)
  93. ->will($this->returnValue(false));
  94. $this->sessionHelperMock->expects($this->never())->method('isPersistent');
  95. $this->observerMock->expects($this->never())->method('getEvent');
  96. $this->model->execute($this->observerMock);
  97. }
  98. public function testExecuteWhenSessionIsNotPersistent()
  99. {
  100. $this->helperMock
  101. ->expects($this->once())
  102. ->method('canProcess')
  103. ->with($this->observerMock)
  104. ->will($this->returnValue(true));
  105. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
  106. $this->checkoutSessionMock->expects($this->never())->method('isLoggedIn');
  107. $this->observerMock->expects($this->never())->method('getEvent');
  108. $this->model->execute($this->observerMock);
  109. }
  110. public function testExecuteWhenCustomerLoggedIn()
  111. {
  112. $this->helperMock
  113. ->expects($this->once())
  114. ->method('canProcess')
  115. ->with($this->observerMock)
  116. ->will($this->returnValue(true));
  117. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  118. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
  119. $this->observerMock->expects($this->never())->method('getEvent');
  120. $this->model->execute($this->observerMock);
  121. }
  122. public function testExecuteWhenActionIsStop()
  123. {
  124. $this->helperMock
  125. ->expects($this->once())
  126. ->method('canProcess')
  127. ->with($this->observerMock)
  128. ->will($this->returnValue(true));
  129. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  130. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  131. $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($this->eventMock));
  132. $this->eventMock->expects($this->once())->method('getRequest')->will($this->returnValue($this->requestMock));
  133. $this->requestMock
  134. ->expects($this->once())
  135. ->method('getFullActionName')
  136. ->will($this->returnValue('persistent_index_saveMethod'));
  137. $this->helperMock->expects($this->never())->method('isShoppingCartPersist');
  138. $this->model->execute($this->observerMock);
  139. }
  140. public function testExecuteWhenShoppingCartIsPersistent()
  141. {
  142. $customerId = 1;
  143. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  144. $this->helperMock
  145. ->expects($this->once())
  146. ->method('canProcess')
  147. ->with($this->observerMock)
  148. ->will($this->returnValue(true));
  149. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  150. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  151. $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($this->eventMock));
  152. $this->eventMock->expects($this->once())->method('getRequest')->will($this->returnValue($this->requestMock));
  153. $this->requestMock
  154. ->expects($this->once())
  155. ->method('getFullActionName')
  156. ->will($this->returnValue('method_name'));
  157. $this->helperMock
  158. ->expects($this->once())
  159. ->method('isShoppingCartPersist')
  160. ->will($this->returnValue(true));
  161. $this->sessionHelperMock
  162. ->expects($this->once())
  163. ->method('getSession')
  164. ->will($this->returnValue($this->sessionMock));
  165. $this->sessionMock->expects($this->once())->method('getCustomerId')->will($this->returnValue($customerId));
  166. $this->customerRepository
  167. ->expects($this->once())
  168. ->method('getById')
  169. ->with($customerId)
  170. ->will($this->returnValue($this->customerMock));
  171. $this->checkoutSessionMock->expects($this->once())->method('setCustomerData')->with($this->customerMock);
  172. $this->checkoutSessionMock->expects($this->once())->method('hasQuote')->will($this->returnValue(false));
  173. $this->checkoutSessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
  174. $this->model->execute($this->observerMock);
  175. }
  176. public function testExecuteWhenShoppingCartIsNotPersistent()
  177. {
  178. $this->helperMock
  179. ->expects($this->once())
  180. ->method('canProcess')
  181. ->with($this->observerMock)
  182. ->will($this->returnValue(true));
  183. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  184. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  185. $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($this->eventMock));
  186. $this->eventMock->expects($this->once())->method('getRequest')->will($this->returnValue($this->requestMock));
  187. $this->requestMock
  188. ->expects($this->once())
  189. ->method('getFullActionName')
  190. ->will($this->returnValue('method_name'));
  191. $this->helperMock
  192. ->expects($this->once())
  193. ->method('isShoppingCartPersist')
  194. ->will($this->returnValue(false));
  195. $this->checkoutSessionMock->expects($this->never())->method('setCustomerData');
  196. $this->model->execute($this->observerMock);
  197. }
  198. public function testExecuteWhenShoppingCartIsPersistentAndQuoteExist()
  199. {
  200. $customerId = 1;
  201. $this->helperMock
  202. ->expects($this->once())
  203. ->method('canProcess')
  204. ->with($this->observerMock)
  205. ->will($this->returnValue(true));
  206. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  207. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  208. $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($this->eventMock));
  209. $this->eventMock->expects($this->once())->method('getRequest')->will($this->returnValue($this->requestMock));
  210. $this->requestMock
  211. ->expects($this->once())
  212. ->method('getFullActionName')
  213. ->will($this->returnValue('method_name'));
  214. $this->helperMock
  215. ->expects($this->once())
  216. ->method('isShoppingCartPersist')
  217. ->will($this->returnValue(true));
  218. $this->sessionHelperMock
  219. ->expects($this->once())
  220. ->method('getSession')
  221. ->will($this->returnValue($this->sessionMock));
  222. $this->sessionMock->expects($this->once())->method('getCustomerId')->will($this->returnValue($customerId));
  223. $this->customerRepository
  224. ->expects($this->once())
  225. ->method('getById')
  226. ->with($customerId)
  227. ->will($this->returnValue($this->customerMock));
  228. $this->checkoutSessionMock->expects($this->once())->method('hasQuote')->will($this->returnValue(true));
  229. $this->checkoutSessionMock->expects($this->once())->method('setCustomerData')->with($this->customerMock);
  230. $this->model->execute($this->observerMock);
  231. }
  232. }