GuestPaymentInformationManagementPluginTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Persistent\Test\Unit\Model\Checkout;
  7. class GuestPaymentInformationManagementPluginTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Persistent\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $persistentHelperMock;
  13. /**
  14. * @var \Magento\Persistent\Helper\Session|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $persistentSessionMock;
  17. /**
  18. * @var \Magento\Checkout\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $checkoutSessionMock;
  21. /**
  22. * @var \Magento\Persistent\Model\QuoteManager|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $quoteManagerMock;
  25. /**
  26. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $customerSessionMock;
  29. /**
  30. * @var \Magento\Quote\Api\CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $cartRepositoryMock;
  33. /**
  34. * @var \Magento\Persistent\Model\Checkout\GuestPaymentInformationManagementPlugin
  35. */
  36. protected $plugin;
  37. /**
  38. * @var \Magento\Checkout\Model\GuestPaymentInformationManagement|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $subjectMock;
  41. protected function setUp()
  42. {
  43. $this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  44. $this->persistentSessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  45. $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
  46. $this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
  47. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  48. $this->cartRepositoryMock = $this->createMock(
  49. \Magento\Quote\Api\CartRepositoryInterface::class
  50. );
  51. $this->subjectMock = $this->createMock(
  52. \Magento\Checkout\Model\GuestPaymentInformationManagement::class
  53. );
  54. $this->plugin = new \Magento\Persistent\Model\Checkout\GuestPaymentInformationManagementPlugin(
  55. $this->persistentHelperMock,
  56. $this->persistentSessionMock,
  57. $this->customerSessionMock,
  58. $this->checkoutSessionMock,
  59. $this->quoteManagerMock,
  60. $this->cartRepositoryMock
  61. );
  62. }
  63. public function testBeforeSavePaymentInformationAndPlaceOrderCartConvertsToGuest()
  64. {
  65. $cartId = '1';
  66. $email = 'guest@example.com';
  67. $walkMethod = 'setEmail';
  68. $walkArgs = ['email' => $email];
  69. /**
  70. * @var \Magento\Quote\Api\Data\PaymentInterface|\PHPUnit_Framework_MockObject_MockObject $paymentInterfaceMock
  71. */
  72. $paymentInterfaceMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  73. $this->persistentHelperMock->expects($this->once())->method('isShoppingCartPersist')->willReturn(true);
  74. $this->persistentSessionMock->expects($this->once())->method('isPersistent')->willReturn(true);
  75. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
  76. $this->quoteManagerMock->expects($this->once())->method('isPersistent')->willReturn(true);
  77. $this->customerSessionMock->expects($this->once())->method('setCustomerId')->with(null);
  78. $this->customerSessionMock->expects($this->once())->method('setCustomerGroupId')->with(null);
  79. $this->quoteManagerMock->expects($this->once())->method('convertCustomerCartToGuest');
  80. /** @var \Magento\Quote\Api\Data\CartInterface|\PHPUnit_Framework_MockObject_MockObject $quoteMock */
  81. $quoteMock = $this->getMockForAbstractClass(
  82. \Magento\Quote\Api\Data\CartInterface::class,
  83. [],
  84. '',
  85. false,
  86. false,
  87. true,
  88. ['setCustomerEmail', 'getAddressesCollection'],
  89. false
  90. );
  91. $this->checkoutSessionMock->method('getQuoteId')->willReturn($cartId);
  92. $this->cartRepositoryMock->expects($this->once())->method('get')->with($cartId)->willReturn($quoteMock);
  93. $quoteMock->expects($this->once())->method('setCustomerEmail')->with($email);
  94. /** @var \Magento\Framework\Data\Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  95. $collectionMock = $this->createMock(\Magento\Framework\Data\Collection::class);
  96. $quoteMock->expects($this->once())->method('getAddressesCollection')->willReturn($collectionMock);
  97. $collectionMock->expects($this->once())->method('walk')->with($walkMethod, $walkArgs);
  98. $this->cartRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
  99. $this->plugin->beforeSavePaymentInformationAndPlaceOrder(
  100. $this->subjectMock,
  101. $cartId,
  102. $email,
  103. $paymentInterfaceMock,
  104. null
  105. );
  106. }
  107. public function testBeforeSavePaymentInformationAndPlaceOrderShoppingCartNotPersistentState()
  108. {
  109. $cartId = '1';
  110. $email = 'guest@example.com';
  111. /**
  112. * @var \Magento\Quote\Api\Data\PaymentInterface|\PHPUnit_Framework_MockObject_MockObject $paymentInterfaceMock
  113. */
  114. $paymentInterfaceMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  115. $this->persistentHelperMock->expects($this->once())->method('isShoppingCartPersist')->willReturn(false);
  116. $this->persistentSessionMock->expects($this->once())->method('isPersistent')->willReturn(true);
  117. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
  118. $this->plugin->beforeSavePaymentInformationAndPlaceOrder(
  119. $this->subjectMock,
  120. $cartId,
  121. $email,
  122. $paymentInterfaceMock,
  123. null
  124. );
  125. }
  126. public function testBeforeSavePaymentInformationAndPlaceOrderPersistentSessionNotPersistentState()
  127. {
  128. $cartId = '1';
  129. $email = 'guest@example.com';
  130. /**
  131. * @var \Magento\Quote\Api\Data\PaymentInterface|\PHPUnit_Framework_MockObject_MockObject $paymentInterfaceMock
  132. */
  133. $paymentInterfaceMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  134. $this->persistentSessionMock->expects($this->once())->method('isPersistent')->willReturn(false);
  135. $this->plugin->beforeSavePaymentInformationAndPlaceOrder(
  136. $this->subjectMock,
  137. $cartId,
  138. $email,
  139. $paymentInterfaceMock,
  140. null
  141. );
  142. }
  143. public function testBeforeSavePaymentInformationAndPlaceOrderCustomerSessionInLoggedInState()
  144. {
  145. $cartId = '1';
  146. $email = 'guest@example.com';
  147. /**
  148. * @var \Magento\Quote\Api\Data\PaymentInterface|\PHPUnit_Framework_MockObject_MockObject $paymentInterfaceMock
  149. */
  150. $paymentInterfaceMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  151. $this->persistentSessionMock->expects($this->once())->method('isPersistent')->willReturn(true);
  152. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true);
  153. $this->plugin->beforeSavePaymentInformationAndPlaceOrder(
  154. $this->subjectMock,
  155. $cartId,
  156. $email,
  157. $paymentInterfaceMock,
  158. null
  159. );
  160. }
  161. public function testBeforeSavePaymentInformationAndPlaceOrderQuoteManagerNotInPersistentState()
  162. {
  163. $cartId = '1';
  164. $email = 'guest@example.com';
  165. /**
  166. * @var \Magento\Quote\Api\Data\PaymentInterface|\PHPUnit_Framework_MockObject_MockObject $paymentInterfaceMock
  167. */
  168. $paymentInterfaceMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  169. $this->persistentHelperMock->expects($this->once())->method('isShoppingCartPersist')->willReturn(true);
  170. $this->persistentSessionMock->expects($this->once())->method('isPersistent')->willReturn(true);
  171. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
  172. $this->quoteManagerMock->expects($this->once())->method('isPersistent')->willReturn(false);
  173. $this->plugin->beforeSavePaymentInformationAndPlaceOrder(
  174. $this->subjectMock,
  175. $cartId,
  176. $email,
  177. $paymentInterfaceMock,
  178. null
  179. );
  180. }
  181. }