GuestPaymentInformationManagementTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Checkout\Test\Unit\Model;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Adapter\AdapterInterface;
  10. use Magento\Quote\Model\Quote;
  11. use Magento\Quote\Model\Quote\Address;
  12. use Magento\Quote\Model\QuoteIdMask;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class GuestPaymentInformationManagementTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $billingAddressManagementMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $paymentMethodManagementMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $cartManagementMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $cartRepositoryMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $quoteIdMaskFactoryMock;
  38. /**
  39. * @var \Magento\Checkout\Model\GuestPaymentInformationManagement
  40. */
  41. protected $model;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $loggerMock;
  46. /**
  47. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $resourceConnectionMock;
  50. protected function setUp()
  51. {
  52. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  53. $this->billingAddressManagementMock = $this->createMock(
  54. \Magento\Quote\Api\GuestBillingAddressManagementInterface::class
  55. );
  56. $this->paymentMethodManagementMock = $this->createMock(
  57. \Magento\Quote\Api\GuestPaymentMethodManagementInterface::class
  58. );
  59. $this->cartManagementMock = $this->createMock(\Magento\Quote\Api\GuestCartManagementInterface::class);
  60. $this->cartRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  61. $this->quoteIdMaskFactoryMock = $this->createPartialMock(
  62. \Magento\Quote\Model\QuoteIdMaskFactory::class,
  63. ['create']
  64. );
  65. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  66. $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->model = $objectManager->getObject(
  70. \Magento\Checkout\Model\GuestPaymentInformationManagement::class,
  71. [
  72. 'billingAddressManagement' => $this->billingAddressManagementMock,
  73. 'paymentMethodManagement' => $this->paymentMethodManagementMock,
  74. 'cartManagement' => $this->cartManagementMock,
  75. 'cartRepository' => $this->cartRepositoryMock,
  76. 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
  77. 'connectionPool' => $this->resourceConnectionMock,
  78. ]
  79. );
  80. $objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
  81. }
  82. public function testSavePaymentInformationAndPlaceOrder()
  83. {
  84. $cartId = 100;
  85. $orderId = 200;
  86. $email = 'email@magento.com';
  87. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  88. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  89. $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
  90. $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
  91. $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
  92. ->disableOriginalConstructor()
  93. ->getMockForAbstractClass();
  94. $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
  95. ->disableOriginalConstructor()
  96. ->getMockForAbstractClass();
  97. $this->resourceConnectionMock->expects($this->at(0))
  98. ->method('getConnection')
  99. ->with('sales')
  100. ->willReturn($adapterMockForSales);
  101. $adapterMockForSales->expects($this->once())->method('beginTransaction');
  102. $adapterMockForSales->expects($this->once())->method('commit');
  103. $this->resourceConnectionMock->expects($this->at(1))
  104. ->method('getConnection')
  105. ->with('checkout')
  106. ->willReturn($adapterMockForCheckout);
  107. $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
  108. $adapterMockForCheckout->expects($this->once())->method('commit');
  109. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  110. $this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
  111. $this->assertEquals(
  112. $orderId,
  113. $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock)
  114. );
  115. }
  116. /**
  117. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  118. */
  119. public function testSavePaymentInformationAndPlaceOrderException()
  120. {
  121. $cartId = 100;
  122. $email = 'email@magento.com';
  123. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  124. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  125. $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
  126. $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
  127. $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
  128. ->disableOriginalConstructor()
  129. ->getMockForAbstractClass();
  130. $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
  131. ->disableOriginalConstructor()
  132. ->getMockForAbstractClass();
  133. $this->resourceConnectionMock->expects($this->at(0))
  134. ->method('getConnection')
  135. ->with('sales')
  136. ->willReturn($adapterMockForSales);
  137. $adapterMockForSales->expects($this->once())->method('beginTransaction');
  138. $adapterMockForSales->expects($this->once())->method('rollback');
  139. $this->resourceConnectionMock->expects($this->at(1))
  140. ->method('getConnection')
  141. ->with('checkout')
  142. ->willReturn($adapterMockForCheckout);
  143. $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
  144. $adapterMockForCheckout->expects($this->once())->method('rollback');
  145. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  146. $exception = new \Magento\Framework\Exception\CouldNotSaveException(__('DB exception'));
  147. $this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
  148. $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock);
  149. $this->expectExceptionMessage(
  150. 'A server error stopped your order from being placed. Please try to place your order again.'
  151. );
  152. }
  153. public function testSavePaymentInformation()
  154. {
  155. $cartId = 100;
  156. $email = 'email@magento.com';
  157. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  158. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  159. $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
  160. $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
  161. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  162. $this->assertTrue($this->model->savePaymentInformation($cartId, $email, $paymentMock, $billingAddressMock));
  163. }
  164. public function testSavePaymentInformationWithoutBillingAddress()
  165. {
  166. $cartId = 100;
  167. $email = 'email@magento.com';
  168. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  169. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  170. $quoteMock = $this->createMock(Quote::class);
  171. $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
  172. $this->billingAddressManagementMock->expects($this->never())->method('assign');
  173. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  174. $quoteIdMaskMock = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
  175. $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($quoteIdMaskMock);
  176. $quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf();
  177. $quoteIdMaskMock->expects($this->once())->method('getQuoteId')->willReturn($cartId);
  178. $this->cartRepositoryMock->expects($this->once())->method('getActive')->with($cartId)->willReturn($quoteMock);
  179. $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock);
  180. $billingAddressMock->expects($this->once())->method('setEmail')->with($email);
  181. $this->assertTrue($this->model->savePaymentInformation($cartId, $email, $paymentMock));
  182. }
  183. /**
  184. * @expectedExceptionMessage DB exception
  185. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  186. */
  187. public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
  188. {
  189. $cartId = 100;
  190. $email = 'email@magento.com';
  191. $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  192. $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  193. $quoteMock = $this->createMock(Quote::class);
  194. $quoteMock->method('getBillingAddress')->willReturn($billingAddressMock);
  195. $this->cartRepositoryMock->method('getActive')->with($cartId)->willReturn($quoteMock);
  196. $quoteIdMask = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
  197. $this->quoteIdMaskFactoryMock->method('create')->willReturn($quoteIdMask);
  198. $quoteIdMask->method('load')->with($cartId, 'masked_id')->willReturnSelf();
  199. $quoteIdMask->method('getQuoteId')->willReturn($cartId);
  200. $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
  201. $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
  202. ->disableOriginalConstructor()
  203. ->getMockForAbstractClass();
  204. $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
  205. ->disableOriginalConstructor()
  206. ->getMockForAbstractClass();
  207. $this->resourceConnectionMock->expects($this->at(0))
  208. ->method('getConnection')
  209. ->with('sales')
  210. ->willReturn($adapterMockForSales);
  211. $adapterMockForSales->expects($this->once())->method('beginTransaction');
  212. $adapterMockForSales->expects($this->once())->method('rollback');
  213. $this->resourceConnectionMock->expects($this->at(1))
  214. ->method('getConnection')
  215. ->with('checkout')
  216. ->willReturn($adapterMockForCheckout);
  217. $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
  218. $adapterMockForCheckout->expects($this->once())->method('rollback');
  219. $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
  220. $phrase = new \Magento\Framework\Phrase(__('DB exception'));
  221. $exception = new \Magento\Framework\Exception\LocalizedException($phrase);
  222. $this->loggerMock->expects($this->never())->method('critical');
  223. $this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
  224. $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock);
  225. }
  226. /**
  227. * @param int $cartId
  228. * @param \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
  229. * @return void
  230. */
  231. private function getMockForAssignBillingAddress(
  232. int $cartId,
  233. \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
  234. ) : void {
  235. $quoteIdMask = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
  236. $this->quoteIdMaskFactoryMock->method('create')
  237. ->willReturn($quoteIdMask);
  238. $quoteIdMask->method('load')
  239. ->with($cartId, 'masked_id')
  240. ->willReturnSelf();
  241. $quoteIdMask->method('getQuoteId')
  242. ->willReturn($cartId);
  243. $billingAddressId = 1;
  244. $quote = $this->createMock(Quote::class);
  245. $quoteBillingAddress = $this->createMock(Address::class);
  246. $quoteShippingAddress = $this->createPartialMock(
  247. Address::class,
  248. ['setLimitCarrier', 'getShippingMethod']
  249. );
  250. $this->cartRepositoryMock->method('getActive')
  251. ->with($cartId)
  252. ->willReturn($quote);
  253. $quote->expects($this->once())
  254. ->method('getBillingAddress')
  255. ->willReturn($quoteBillingAddress);
  256. $quote->expects($this->once())
  257. ->method('getShippingAddress')
  258. ->willReturn($quoteShippingAddress);
  259. $quoteBillingAddress->expects($this->once())
  260. ->method('getId')
  261. ->willReturn($billingAddressId);
  262. $quote->expects($this->once())
  263. ->method('removeAddress')
  264. ->with($billingAddressId);
  265. $quote->expects($this->once())
  266. ->method('setBillingAddress')
  267. ->with($billingAddressMock);
  268. $quote->expects($this->once())
  269. ->method('setDataChanges')
  270. ->willReturnSelf();
  271. $quoteShippingAddress->method('getShippingMethod')
  272. ->willReturn('flatrate_flatrate');
  273. $quoteShippingAddress->expects($this->once())
  274. ->method('setLimitCarrier')
  275. ->with('flatrate');
  276. }
  277. }