PaymentInformationManagementTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Model;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class PaymentInformationManagementTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $billingAddressManagementMock;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $paymentMethodManagementMock;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $cartManagementMock;
  24. /**
  25. * @var \Magento\Checkout\Model\PaymentInformationManagement
  26. */
  27. protected $model;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $loggerMock;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $cartRepositoryMock;
  36. protected function setUp()
  37. {
  38. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  39. $this->billingAddressManagementMock = $this->createMock(
  40. \Magento\Quote\Api\BillingAddressManagementInterface::class
  41. );
  42. $this->paymentMethodManagementMock = $this->createMock(
  43. \Magento\Quote\Api\PaymentMethodManagementInterface::class
  44. );
  45. $this->cartManagementMock = $this->createMock(\Magento\Quote\Api\CartManagementInterface::class);
  46. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  47. $this->cartRepositoryMock = $this->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class)->getMock();
  48. $this->model = $objectManager->getObject(
  49. \Magento\Checkout\Model\PaymentInformationManagement::class,
  50. [
  51. 'billingAddressManagement' => $this->billingAddressManagementMock,
  52. 'paymentMethodManagement' => $this->paymentMethodManagementMock,
  53. 'cartManagement' => $this->cartManagementMock
  54. ]
  55. );
  56. $objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
  57. $objectManager->setBackwardCompatibleProperty($this->model, 'cartRepository', $this->cartRepositoryMock);
  58. }
  59. public function testSavePaymentInformationAndPlaceOrder()
  60. {
  61. $cartId = 100;
  62. $orderId = 200;
  63. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  64. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  65. $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
  66. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  67. $this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
  68. $this->assertEquals(
  69. $orderId,
  70. $this->model->savePaymentInformationAndPlaceOrder($cartId, $paymentMock, $billingAddressMock)
  71. );
  72. }
  73. /**
  74. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  75. */
  76. public function testSavePaymentInformationAndPlaceOrderException()
  77. {
  78. $cartId = 100;
  79. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  80. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  81. $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
  82. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  83. $exception = new \Exception(__('DB exception'));
  84. $this->loggerMock->expects($this->once())->method('critical');
  85. $this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
  86. $this->model->savePaymentInformationAndPlaceOrder($cartId, $paymentMock, $billingAddressMock);
  87. $this->expectExceptionMessage(
  88. 'A server error stopped your order from being placed. Please try to place your order again.'
  89. );
  90. }
  91. public function testSavePaymentInformationAndPlaceOrderIfBillingAddressNotExist()
  92. {
  93. $cartId = 100;
  94. $orderId = 200;
  95. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  96. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  97. $this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
  98. $this->assertEquals(
  99. $orderId,
  100. $this->model->savePaymentInformationAndPlaceOrder($cartId, $paymentMock)
  101. );
  102. }
  103. public function testSavePaymentInformation()
  104. {
  105. $cartId = 100;
  106. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  107. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  108. $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
  109. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  110. $this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock, $billingAddressMock));
  111. }
  112. public function testSavePaymentInformationWithoutBillingAddress()
  113. {
  114. $cartId = 100;
  115. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  116. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  117. $this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock));
  118. }
  119. /**
  120. * @expectedExceptionMessage DB exception
  121. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  122. */
  123. public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
  124. {
  125. $cartId = 100;
  126. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  127. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  128. $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
  129. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  130. $phrase = new \Magento\Framework\Phrase(__('DB exception'));
  131. $exception = new \Magento\Framework\Exception\LocalizedException($phrase);
  132. $this->loggerMock->expects($this->never())->method('critical');
  133. $this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
  134. $this->model->savePaymentInformationAndPlaceOrder($cartId, $paymentMock, $billingAddressMock);
  135. }
  136. /**
  137. * @param int $cartId
  138. * @param \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
  139. */
  140. private function getMockForAssignBillingAddress($cartId, $billingAddressMock)
  141. {
  142. $billingAddressId = 1;
  143. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  144. $quoteBillingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  145. $shippingRate = $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Rate::class, []);
  146. $shippingRate->setCarrier('flatrate');
  147. $quoteShippingAddress = $this->createPartialMock(
  148. \Magento\Quote\Model\Quote\Address::class,
  149. ['setLimitCarrier', 'getShippingMethod', 'getShippingRateByCode']
  150. );
  151. $this->cartRepositoryMock->expects($this->any())->method('getActive')->with($cartId)->willReturn($quoteMock);
  152. $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($quoteBillingAddress);
  153. $quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($quoteShippingAddress);
  154. $quoteBillingAddress->expects($this->once())->method('getId')->willReturn($billingAddressId);
  155. $quoteMock->expects($this->once())->method('removeAddress')->with($billingAddressId);
  156. $quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddressMock);
  157. $quoteMock->expects($this->once())->method('setDataChanges')->willReturnSelf();
  158. $quoteShippingAddress->expects($this->any())->method('getShippingRateByCode')->willReturn($shippingRate);
  159. $quoteShippingAddress->expects($this->any())->method('getShippingMethod')->willReturn('flatrate_flatrate');
  160. $quoteShippingAddress->expects($this->once())->method('setLimitCarrier')->with('flatrate')->willReturnSelf();
  161. }
  162. }