QuoteManagerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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\Model;
  8. use Magento\Persistent\Model\QuoteManager;
  9. class QuoteManagerTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var QuoteManager
  13. */
  14. protected $model;
  15. /**
  16. * @var \Magento\Persistent\Helper\Session|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $persistentSessionMock;
  19. /**
  20. * @var \Magento\Persistent\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $persistentDataMock;
  23. /**
  24. * @var \Magento\Checkout\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $checkoutSessionMock;
  27. /**
  28. * @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $quoteMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $sessionMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $abstractCollectionMock;
  39. /**
  40. * @var \Magento\Quote\Api\CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $quoteRepositoryMock;
  43. protected function setUp()
  44. {
  45. $this->persistentSessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  46. $this->sessionMock =
  47. $this->createPartialMock(\Magento\Persistent\Model\Session::class, [
  48. 'setLoadInactive',
  49. 'setCustomerData',
  50. 'clearQuote',
  51. 'clearStorage',
  52. 'getQuote',
  53. 'removePersistentCookie',
  54. '__wakeup',
  55. ]);
  56. $this->persistentDataMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  57. $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
  58. $this->abstractCollectionMock =
  59. $this->createMock(\Magento\Eav\Model\Entity\Collection\AbstractCollection::class);
  60. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  61. $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
  62. 'getId',
  63. 'getIsPersistent',
  64. 'getPaymentsCollection',
  65. 'getAddressesCollection',
  66. 'setIsActive',
  67. 'setCustomerId',
  68. 'setCustomerEmail',
  69. 'setCustomerFirstname',
  70. 'setCustomerLastname',
  71. 'setCustomerGroupId',
  72. 'setIsPersistent',
  73. 'getShippingAddress',
  74. 'getBillingAddress',
  75. 'collectTotals',
  76. 'removeAllAddresses',
  77. 'getIsActive',
  78. 'getCustomerId',
  79. '__wakeup'
  80. ]);
  81. $this->model = new QuoteManager(
  82. $this->persistentSessionMock,
  83. $this->persistentDataMock,
  84. $this->checkoutSessionMock,
  85. $this->quoteRepositoryMock
  86. );
  87. }
  88. public function testSetGuestWithEmptyQuote()
  89. {
  90. $this->checkoutSessionMock->expects($this->once())
  91. ->method('getQuote')->will($this->returnValue(null));
  92. $this->quoteMock->expects($this->never())->method('getId');
  93. $this->persistentSessionMock->expects($this->once())
  94. ->method('getSession')->will($this->returnValue($this->sessionMock));
  95. $this->sessionMock->expects($this->once())
  96. ->method('removePersistentCookie')->will($this->returnValue($this->sessionMock));
  97. $this->model->setGuest(false);
  98. }
  99. public function testSetGuestWithEmptyQuoteId()
  100. {
  101. $this->checkoutSessionMock->expects($this->once())
  102. ->method('getQuote')->will($this->returnValue($this->quoteMock));
  103. $this->quoteMock->expects($this->once())->method('getId')->will($this->returnValue(null));
  104. $this->persistentDataMock->expects($this->never())->method('isShoppingCartPersist');
  105. $this->persistentSessionMock->expects($this->once())
  106. ->method('getSession')->will($this->returnValue($this->sessionMock));
  107. $this->sessionMock->expects($this->once())
  108. ->method('removePersistentCookie')->will($this->returnValue($this->sessionMock));
  109. $this->model->setGuest(false);
  110. }
  111. public function testSetGuestWhenShoppingCartAndQuoteAreNotPersistent()
  112. {
  113. $this->checkoutSessionMock->expects($this->once())
  114. ->method('getQuote')->will($this->returnValue($this->quoteMock));
  115. $this->quoteMock->expects($this->once())->method('getId')->will($this->returnValue(11));
  116. $this->persistentDataMock->expects($this->once())
  117. ->method('isShoppingCartPersist')->will($this->returnValue(false));
  118. $this->quoteMock->expects($this->once())->method('getIsPersistent')->will($this->returnValue(false));
  119. $this->checkoutSessionMock->expects($this->once())
  120. ->method('clearQuote')->will($this->returnValue($this->checkoutSessionMock));
  121. $this->checkoutSessionMock->expects($this->once())->method('clearStorage');
  122. $this->quoteMock->expects($this->never())->method('getPaymentsCollection');
  123. $this->model->setGuest(true);
  124. }
  125. public function testSetGuest()
  126. {
  127. $this->checkoutSessionMock->expects($this->once())
  128. ->method('getQuote')->will($this->returnValue($this->quoteMock));
  129. $this->quoteMock->expects($this->once())->method('getId')->will($this->returnValue(11));
  130. $this->persistentDataMock->expects($this->never())->method('isShoppingCartPersist');
  131. $this->quoteMock->expects($this->once())
  132. ->method('getPaymentsCollection')->will($this->returnValue($this->abstractCollectionMock));
  133. $this->quoteMock->expects($this->once())
  134. ->method('getAddressesCollection')->will($this->returnValue($this->abstractCollectionMock));
  135. $this->abstractCollectionMock->expects($this->exactly(2))->method('walk')->with('delete');
  136. $this->quoteMock->expects($this->once())
  137. ->method('setIsActive')->with(true)->will($this->returnValue($this->quoteMock));
  138. $this->quoteMock->expects($this->once())
  139. ->method('setCustomerId')->with(null)->will($this->returnValue($this->quoteMock));
  140. $this->quoteMock->expects($this->once())
  141. ->method('setCustomerEmail')->with(null)->will($this->returnValue($this->quoteMock));
  142. $this->quoteMock->expects($this->once())
  143. ->method('setCustomerFirstname')->with(null)->will($this->returnValue($this->quoteMock));
  144. $this->quoteMock->expects($this->once())
  145. ->method('setCustomerLastname')->with(null)->will($this->returnValue($this->quoteMock));
  146. $this->quoteMock->expects($this->once())->method('setCustomerGroupId')
  147. ->with(\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID)
  148. ->will($this->returnValue($this->quoteMock));
  149. $this->quoteMock->expects($this->once())
  150. ->method('setIsPersistent')->with(false)->will($this->returnValue($this->quoteMock));
  151. $this->quoteMock->expects($this->once())
  152. ->method('removeAllAddresses')->will($this->returnValue($this->quoteMock));
  153. $quoteAddressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  154. $this->quoteMock->expects($this->once())
  155. ->method('getShippingAddress')->will($this->returnValue($quoteAddressMock));
  156. $this->quoteMock->expects($this->once())
  157. ->method('getBillingAddress')->will($this->returnValue($quoteAddressMock));
  158. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  159. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
  160. $this->persistentSessionMock->expects($this->once())
  161. ->method('getSession')->will($this->returnValue($this->sessionMock));
  162. $this->sessionMock->expects($this->once())
  163. ->method('removePersistentCookie')->will($this->returnValue($this->sessionMock));
  164. $this->model->setGuest(false);
  165. }
  166. public function testExpireWithActiveQuoteAndCustomerId()
  167. {
  168. $this->checkoutSessionMock->expects($this->once())
  169. ->method('setLoadInactive')->will($this->returnValue($this->sessionMock));
  170. $this->sessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($this->quoteMock));
  171. $this->quoteMock->expects($this->once())->method('getIsActive')->will($this->returnValue(11));
  172. $this->quoteMock->expects($this->once())->method('getCustomerId')->will($this->returnValue(22));
  173. $this->checkoutSessionMock->expects($this->once())
  174. ->method('setCustomerData')->with(null)->will($this->returnValue($this->sessionMock));
  175. $this->sessionMock->expects($this->once())
  176. ->method('clearQuote')->will($this->returnValue($this->sessionMock));
  177. $this->sessionMock->expects($this->once())
  178. ->method('clearStorage')->will($this->returnValue($this->sessionMock));
  179. $this->quoteMock->expects($this->never())->method('setIsActive');
  180. $this->model->expire();
  181. }
  182. public function testExpire()
  183. {
  184. $this->checkoutSessionMock->expects($this->once())
  185. ->method('setLoadInactive')->will($this->returnValue($this->sessionMock));
  186. $this->sessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($this->quoteMock));
  187. $this->quoteMock->expects($this->once())->method('getIsActive')->will($this->returnValue(0));
  188. $this->checkoutSessionMock->expects($this->never())->method('setCustomerData');
  189. $this->quoteMock->expects($this->once())
  190. ->method('setIsActive')
  191. ->with(true)
  192. ->will($this->returnValue($this->quoteMock));
  193. $this->quoteMock->expects($this->once())
  194. ->method('setIsPersistent')
  195. ->with(false)
  196. ->will($this->returnValue($this->quoteMock));
  197. $this->quoteMock->expects($this->once())
  198. ->method('setCustomerId')
  199. ->with(null)
  200. ->will($this->returnValue($this->quoteMock));
  201. $this->quoteMock->expects($this->once())
  202. ->method('setCustomerGroupId')
  203. ->with(\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID)
  204. ->will($this->returnValue($this->quoteMock));
  205. $this->model->expire();
  206. }
  207. public function testConvertCustomerCartToGuest()
  208. {
  209. $quoteId = 1;
  210. $addressArgs = ['customerAddressId' => null];
  211. $customerIdArgs = ['customerId' => null];
  212. $emailArgs = ['email' => null];
  213. $this->checkoutSessionMock->expects($this->once())
  214. ->method('getQuoteId')->willReturn($quoteId);
  215. $this->quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
  216. $this->quoteRepositoryMock->expects($this->once())->method('get')->with($quoteId)->willReturn($this->quoteMock);
  217. $this->quoteMock->expects($this->once())
  218. ->method('setIsActive')->with(true)->willReturn($this->quoteMock);
  219. $this->quoteMock->expects($this->once())
  220. ->method('setCustomerId')->with(null)->willReturn($this->quoteMock);
  221. $this->quoteMock->expects($this->once())
  222. ->method('setCustomerEmail')->with(null)->willReturn($this->quoteMock);
  223. $this->quoteMock->expects($this->once())
  224. ->method('setCustomerFirstname')->with(null)->willReturn($this->quoteMock);
  225. $this->quoteMock->expects($this->once())
  226. ->method('setCustomerLastname')->with(null)->willReturn($this->quoteMock);
  227. $this->quoteMock->expects($this->once())->method('setCustomerGroupId')
  228. ->with(\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID)
  229. ->willReturn($this->quoteMock);
  230. $this->quoteMock->expects($this->once())
  231. ->method('setIsPersistent')->with(false)->willReturn($this->quoteMock);
  232. $this->quoteMock->expects($this->exactly(3))
  233. ->method('getAddressesCollection')->willReturn($this->abstractCollectionMock);
  234. $this->abstractCollectionMock->expects($this->exactly(3))->method('walk')->with($this->logicalOr(
  235. $this->equalTo('setCustomerAddressId'),
  236. $this->equalTo($addressArgs),
  237. $this->equalTo('setCustomerId'),
  238. $this->equalTo($customerIdArgs),
  239. $this->equalTo('setEmail'),
  240. $this->equalTo($emailArgs)
  241. ));
  242. $this->quoteMock->expects($this->once())->method('collectTotals')->willReturn($this->quoteMock);
  243. $this->persistentSessionMock->expects($this->once())
  244. ->method('getSession')->willReturn($this->sessionMock);
  245. $this->sessionMock->expects($this->once())
  246. ->method('removePersistentCookie')->willReturn($this->sessionMock);
  247. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
  248. $this->model->convertCustomerCartToGuest();
  249. }
  250. public function testConvertCustomerCartToGuestWithEmptyQuote()
  251. {
  252. $this->checkoutSessionMock->expects($this->once())
  253. ->method('getQuoteId')->willReturn(null);
  254. $this->quoteRepositoryMock->expects($this->once())->method('get')->with(null)->willReturn(null);
  255. $this->model->convertCustomerCartToGuest();
  256. }
  257. public function testConvertCustomerCartToGuestWithEmptyQuoteId()
  258. {
  259. $this->checkoutSessionMock->expects($this->once())
  260. ->method('getQuoteId')->willReturn(1);
  261. $quoteWithNoId = $this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  262. $quoteWithNoId->expects($this->once())->method('getId')->willReturn(null);
  263. $this->quoteRepositoryMock->expects($this->once())->method('get')->with(1)->willReturn($quoteWithNoId);
  264. $this->quoteMock->expects($this->once())->method('getId')->willReturn(1);
  265. $this->model->convertCustomerCartToGuest();
  266. }
  267. }