ShippingInformationManagementTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. * @SuppressWarnings(PHPMD.TooManyFields)
  10. */
  11. class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  15. */
  16. private $objectManager;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $paymentMethodManagementMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $paymentDetailsFactoryMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $cartTotalsRepositoryMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $quoteRepositoryMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $shippingAddressMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $quoteMock;
  41. /**
  42. * @var \Magento\Checkout\Model\ShippingInformationManagement
  43. */
  44. protected $model;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $shippingAssignmentFactoryMock;
  49. /**
  50. * @var \PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $cartExtensionFactoryMock;
  53. /**
  54. * @var \PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $shippingFactoryMock;
  57. /**
  58. * @var \PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $cartExtensionMock;
  61. /**
  62. * @var \PHPUnit_Framework_MockObject_MockObject
  63. */
  64. private $shippingAssignmentMock;
  65. /**
  66. * @var \PHPUnit_Framework_MockObject_MockObject
  67. */
  68. private $shippingMock;
  69. protected function setUp()
  70. {
  71. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  72. $this->paymentMethodManagementMock = $this->createMock(
  73. \Magento\Quote\Api\PaymentMethodManagementInterface::class
  74. );
  75. $this->paymentDetailsFactoryMock = $this->createPartialMock(
  76. \Magento\Checkout\Model\PaymentDetailsFactory::class,
  77. ['create']
  78. );
  79. $this->cartTotalsRepositoryMock = $this->createMock(\Magento\Quote\Api\CartTotalRepositoryInterface::class);
  80. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  81. $this->shippingAddressMock = $this->createPartialMock(
  82. \Magento\Quote\Model\Quote\Address::class,
  83. [
  84. 'getSaveInAddressBook',
  85. 'getSameAsBilling',
  86. 'getCustomerAddressId',
  87. 'setShippingAddress',
  88. 'getShippingAddress',
  89. 'setSaveInAddressBook',
  90. 'setSameAsBilling',
  91. 'setCollectShippingRates',
  92. 'getCountryId',
  93. 'importCustomerAddressData',
  94. 'save',
  95. 'getShippingRateByCode',
  96. 'getShippingMethod',
  97. 'setLimitCarrier'
  98. ]
  99. );
  100. $this->quoteMock = $this->createPartialMock(
  101. \Magento\Quote\Model\Quote::class,
  102. [
  103. 'isVirtual',
  104. 'getItemsCount',
  105. 'getIsMultiShipping',
  106. 'setIsMultiShipping',
  107. 'validateMinimumAmount',
  108. 'getStoreId',
  109. 'setShippingAddress',
  110. 'getShippingAddress',
  111. 'collectTotals',
  112. 'getExtensionAttributes',
  113. 'setExtensionAttributes',
  114. 'setBillingAddress'
  115. ],
  116. [],
  117. '',
  118. false
  119. );
  120. $this->shippingAssignmentFactoryMock =
  121. $this->createPartialMock(\Magento\Quote\Model\ShippingAssignmentFactory::class, ['create']);
  122. $this->cartExtensionFactoryMock =
  123. $this->createPartialMock(\Magento\Quote\Api\Data\CartExtensionFactory::class, ['create']);
  124. $this->shippingFactoryMock =
  125. $this->createPartialMock(\Magento\Quote\Model\ShippingFactory::class, ['create']);
  126. $this->model = $this->objectManager->getObject(
  127. \Magento\Checkout\Model\ShippingInformationManagement::class,
  128. [
  129. 'paymentMethodManagement' => $this->paymentMethodManagementMock,
  130. 'paymentDetailsFactory' => $this->paymentDetailsFactoryMock,
  131. 'cartTotalsRepository' => $this->cartTotalsRepositoryMock,
  132. 'quoteRepository' => $this->quoteRepositoryMock,
  133. 'shippingAssignmentFactory' => $this->shippingAssignmentFactoryMock,
  134. 'cartExtensionFactory' => $this->cartExtensionFactoryMock,
  135. 'shippingFactory' => $this->shippingFactoryMock
  136. ]
  137. );
  138. }
  139. /**
  140. * @expectedException \Magento\Framework\Exception\InputException
  141. * @expectedExceptionMessage The shipping method can't be set for an empty cart. Add an item to cart and try again.
  142. */
  143. public function testSaveAddressInformationIfCartIsEmpty()
  144. {
  145. $cartId = 100;
  146. $carrierCode = 'carrier_code';
  147. $shippingMethod = 'shipping_method';
  148. $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
  149. $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  150. $addressInformationMock->expects($this->once())
  151. ->method('getShippingAddress')
  152. ->willReturn($this->shippingAddressMock);
  153. $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
  154. $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
  155. $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
  156. $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
  157. $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
  158. $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(0);
  159. $this->quoteRepositoryMock->expects($this->once())
  160. ->method('getActive')
  161. ->with($cartId)
  162. ->willReturn($this->quoteMock);
  163. $this->model->saveAddressInformation($cartId, $addressInformationMock);
  164. }
  165. /**
  166. * @param string $shippingMethod
  167. */
  168. private function setShippingAssignmentsMocks($shippingMethod)
  169. {
  170. $this->quoteMock->expects($this->once())->method('getExtensionAttributes')->willReturn(null);
  171. $this->shippingAddressMock->expects($this->once())->method('setLimitCarrier');
  172. $this->cartExtensionMock = $this->createPartialMock(
  173. \Magento\Quote\Api\Data\CartExtension::class,
  174. ['getShippingAssignments', 'setShippingAssignments']
  175. );
  176. $this->cartExtensionFactoryMock->expects($this->once())
  177. ->method('create')
  178. ->willReturn($this->cartExtensionMock);
  179. $this->cartExtensionMock->expects($this->once())->method('getShippingAssignments')->willReturn(null);
  180. $this->shippingAssignmentMock = $this->createMock(
  181. \Magento\Quote\Model\ShippingAssignment::class
  182. );
  183. $this->shippingAssignmentFactoryMock->expects($this->once())
  184. ->method('create')
  185. ->willReturn($this->shippingAssignmentMock);
  186. $this->shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn(null);
  187. $this->shippingMock = $this->createMock(\Magento\Quote\Model\Shipping::class);
  188. $this->shippingFactoryMock->expects($this->once())->method('create')->willReturn($this->shippingMock);
  189. $this->shippingMock->expects($this->once())
  190. ->method('setAddress')
  191. ->with($this->shippingAddressMock)
  192. ->willReturnSelf();
  193. $this->shippingMock->expects($this->once())->method('setMethod')->with($shippingMethod)->willReturnSelf();
  194. $this->shippingAssignmentMock->expects($this->once())
  195. ->method('setShipping')
  196. ->with($this->shippingMock)
  197. ->willReturnSelf();
  198. $this->cartExtensionMock->expects($this->once())
  199. ->method('setShippingAssignments')
  200. ->with([$this->shippingAssignmentMock])
  201. ->willReturnSelf();
  202. $this->quoteMock->expects($this->once())
  203. ->method('setExtensionAttributes')
  204. ->with($this->cartExtensionMock)
  205. ->willReturnSelf();
  206. }
  207. /**
  208. * @expectedException \Magento\Framework\Exception\StateException
  209. * @expectedExceptionMessage The shipping address is missing. Set the address and try again.
  210. */
  211. public function testSaveAddressInformationIfShippingAddressNotSet()
  212. {
  213. $cartId = 100;
  214. $carrierCode = 'carrier_code';
  215. $shippingMethod = 'shipping_method';
  216. $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
  217. $addressInformationMock->expects($this->once())
  218. ->method('getShippingAddress')
  219. ->willReturn($this->shippingAddressMock);
  220. $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
  221. $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
  222. $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  223. $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
  224. $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(null);
  225. $this->model->saveAddressInformation($cartId, $addressInformationMock);
  226. }
  227. /**
  228. * @expectedException \Magento\Framework\Exception\InputException
  229. * @expectedExceptionMessage The shipping information was unable to be saved. Verify the input data and try again.
  230. */
  231. public function testSaveAddressInformationIfCanNotSaveQuote()
  232. {
  233. $cartId = 100;
  234. $carrierCode = 'carrier_code';
  235. $shippingMethod = 'shipping_method';
  236. $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
  237. $this->quoteRepositoryMock->expects($this->once())
  238. ->method('getActive')
  239. ->with($cartId)
  240. ->willReturn($this->quoteMock);
  241. $addressInformationMock->expects($this->once())
  242. ->method('getShippingAddress')
  243. ->willReturn($this->shippingAddressMock);
  244. $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
  245. $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
  246. $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  247. $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
  248. $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
  249. $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
  250. $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
  251. $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
  252. $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
  253. $this->quoteRepositoryMock->expects($this->once())
  254. ->method('save')
  255. ->with($this->quoteMock)
  256. ->willThrowException(new \Exception());
  257. $this->model->saveAddressInformation($cartId, $addressInformationMock);
  258. }
  259. /**
  260. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  261. * @expectedExceptionMessage Carrier with such method not found: carrier_code, shipping_method
  262. */
  263. public function testSaveAddressInformationIfCarrierCodeIsInvalid()
  264. {
  265. $cartId = 100;
  266. $carrierCode = 'carrier_code';
  267. $shippingMethod = 'shipping_method';
  268. $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
  269. $this->quoteRepositoryMock->expects($this->once())
  270. ->method('getActive')
  271. ->with($cartId)
  272. ->willReturn($this->quoteMock);
  273. $addressInformationMock->expects($this->once())
  274. ->method('getShippingAddress')
  275. ->willReturn($this->shippingAddressMock);
  276. $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
  277. $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
  278. $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  279. $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
  280. $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
  281. $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
  282. $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
  283. $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
  284. $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
  285. $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->shippingAddressMock);
  286. $this->quoteRepositoryMock->expects($this->once())
  287. ->method('save')
  288. ->with($this->quoteMock);
  289. $this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
  290. $this->shippingAddressMock->expects($this->once())
  291. ->method('getShippingRateByCode')
  292. ->with($shippingMethod)
  293. ->willReturn(false);
  294. $this->model->saveAddressInformation($cartId, $addressInformationMock);
  295. }
  296. public function testSaveAddressInformation()
  297. {
  298. $cartId = 100;
  299. $carrierCode = 'carrier_code';
  300. $shippingMethod = 'shipping_method';
  301. $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
  302. $this->quoteRepositoryMock->expects($this->once())
  303. ->method('getActive')
  304. ->with($cartId)
  305. ->willReturn($this->quoteMock);
  306. $addressInformationMock->expects($this->once())
  307. ->method('getShippingAddress')
  308. ->willReturn($this->shippingAddressMock);
  309. $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
  310. $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
  311. $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  312. $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
  313. $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
  314. $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
  315. $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
  316. $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
  317. $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
  318. $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->shippingAddressMock);
  319. $this->quoteRepositoryMock->expects($this->once())
  320. ->method('save')
  321. ->with($this->quoteMock);
  322. $this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
  323. $this->shippingAddressMock->expects($this->once())
  324. ->method('getShippingRateByCode')
  325. ->with($shippingMethod)
  326. ->willReturn('rates');
  327. $paymentDetailsMock = $this->createMock(\Magento\Checkout\Api\Data\PaymentDetailsInterface::class);
  328. $this->paymentDetailsFactoryMock->expects($this->once())->method('create')->willReturn($paymentDetailsMock);
  329. $paymentMethodMock = $this->createMock(\Magento\Quote\Api\Data\PaymentMethodInterface::class);
  330. $this->paymentMethodManagementMock->expects($this->once())
  331. ->method('getList')
  332. ->with($cartId)
  333. ->willReturn([$paymentMethodMock]);
  334. $cartTotalsMock = $this->createMock(\Magento\Quote\Api\Data\TotalsInterface::class);
  335. $this->cartTotalsRepositoryMock->expects($this->once())
  336. ->method('get')
  337. ->with($cartId)
  338. ->willReturn($cartTotalsMock);
  339. $paymentDetailsMock->expects($this->once())
  340. ->method('setPaymentMethods')
  341. ->with([$paymentMethodMock])
  342. ->willReturnSelf();
  343. $paymentDetailsMock->expects($this->once())->method('setTotals')->with()->willReturnSelf($cartTotalsMock);
  344. $this->assertEquals(
  345. $paymentDetailsMock,
  346. $this->model->saveAddressInformation($cartId, $addressInformationMock)
  347. );
  348. }
  349. }