ShippingMethodManagementTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Api;
  7. use Magento\TestFramework\ObjectManager;
  8. use Magento\TestFramework\TestCase\WebapiAbstract;
  9. use Magento\Quote\Api\Data\ShippingMethodInterface;
  10. class ShippingMethodManagementTest extends WebapiAbstract
  11. {
  12. const SERVICE_VERSION = 'V1';
  13. const SERVICE_NAME = 'quoteShippingMethodManagementV1';
  14. const RESOURCE_PATH = '/V1/carts/';
  15. /**
  16. * @var ObjectManager
  17. */
  18. private $objectManager;
  19. /**
  20. * @var \Magento\Quote\Model\Quote
  21. */
  22. protected $quote;
  23. /**
  24. * @var \Magento\Quote\Model\Quote\TotalsCollector
  25. */
  26. protected $totalsCollector;
  27. protected function setUp()
  28. {
  29. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  30. $this->quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  31. $this->totalsCollector = $this->objectManager->create(\Magento\Quote\Model\Quote\TotalsCollector::class);
  32. }
  33. /**
  34. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
  35. *
  36. */
  37. public function testGetListForVirtualCart()
  38. {
  39. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  40. $cartId = $quote->load('test_order_with_virtual_product', 'reserved_order_id')->getId();
  41. $this->assertEquals([], $this->_webApiCall($this->getListServiceInfo($cartId), ["cartId" => $cartId]));
  42. }
  43. /**
  44. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  45. */
  46. public function testGetList()
  47. {
  48. /** @var \Magento\Quote\Model\Quote $quote */
  49. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  50. $quote->load('test_order_1', 'reserved_order_id');
  51. $cartId = $quote->getId();
  52. if (!$cartId) {
  53. $this->fail('quote fixture failed');
  54. }
  55. $quote->getShippingAddress()->collectShippingRates();
  56. $expectedRates = $quote->getShippingAddress()->getGroupedAllShippingRates();
  57. $expectedData = $this->convertRates($expectedRates, $quote->getQuoteCurrencyCode());
  58. $requestData = ["cartId" => $cartId];
  59. $returnedRates = $this->_webApiCall($this->getListServiceInfo($cartId), $requestData);
  60. $this->assertEquals($expectedData, $returnedRates);
  61. }
  62. /**
  63. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  64. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  65. */
  66. public function testGetListForMyCart()
  67. {
  68. $this->_markTestAsRestOnly();
  69. $this->quote->load('test_order_1', 'reserved_order_id');
  70. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  71. $customerTokenService = $this->objectManager->create(
  72. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  73. );
  74. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  75. /** @var \Magento\Quote\Model\ShippingMethodManagementInterface $shippingMethodManagementService */
  76. $shippingMethodManagementService = $this->objectManager->create(
  77. \Magento\Quote\Model\ShippingMethodManagementInterface::class
  78. );
  79. $shippingMethodManagementService->set($this->quote->getId(), 'flatrate', 'flatrate');
  80. $serviceInfo = [
  81. 'rest' => [
  82. 'resourcePath' => '/V1/carts/mine/shipping-methods',
  83. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  84. 'token' => $token
  85. ]
  86. ];
  87. $result = $this->_webApiCall($serviceInfo, []);
  88. $this->assertNotEmpty($result);
  89. $this->assertCount(1, $result);
  90. $shippingMethod = $shippingMethodManagementService->get($this->quote->getId());
  91. $expectedData = [
  92. ShippingMethodInterface::KEY_CARRIER_CODE => $shippingMethod->getCarrierCode(),
  93. ShippingMethodInterface::KEY_METHOD_CODE => $shippingMethod->getMethodCode(),
  94. ShippingMethodInterface::KEY_CARRIER_TITLE => $shippingMethod->getCarrierTitle(),
  95. ShippingMethodInterface::KEY_METHOD_TITLE => $shippingMethod->getMethodTitle(),
  96. ShippingMethodInterface::KEY_SHIPPING_AMOUNT => $shippingMethod->getAmount(),
  97. ShippingMethodInterface::KEY_BASE_SHIPPING_AMOUNT => $shippingMethod->getBaseAmount(),
  98. ShippingMethodInterface::KEY_AVAILABLE => $shippingMethod->getAvailable(),
  99. ShippingMethodInterface::KEY_ERROR_MESSAGE => null,
  100. ShippingMethodInterface::KEY_PRICE_EXCL_TAX => $shippingMethod->getPriceExclTax(),
  101. ShippingMethodInterface::KEY_PRICE_INCL_TAX => $shippingMethod->getPriceInclTax(),
  102. ];
  103. $this->assertEquals($expectedData, $result[0]);
  104. }
  105. /**
  106. * Service info
  107. *
  108. * @param int $cartId
  109. * @return array
  110. */
  111. protected function getListServiceInfo($cartId)
  112. {
  113. return [
  114. 'rest' => [
  115. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/shipping-methods',
  116. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  117. ],
  118. 'soap' => [
  119. 'service' => self::SERVICE_NAME,
  120. 'serviceVersion' => self::SERVICE_VERSION,
  121. 'operation' => self::SERVICE_NAME . 'GetList',
  122. ],
  123. ];
  124. }
  125. /**
  126. * Convert rate models array to data array
  127. *
  128. * @param string $currencyCode
  129. * @param \Magento\Quote\Model\Quote\Address\Rate[] $groupedRates
  130. * @return array
  131. */
  132. protected function convertRates($groupedRates, $currencyCode)
  133. {
  134. $result = [];
  135. /** @var \Magento\Quote\Model\Cart\ShippingMethodConverter $converter */
  136. $converter = $this->objectManager->create(\Magento\Quote\Model\Cart\ShippingMethodConverter::class);
  137. foreach ($groupedRates as $carrierRates) {
  138. foreach ($carrierRates as $rate) {
  139. $result[] = $converter->modelToDataObject($rate, $currencyCode)->__toArray();
  140. }
  141. }
  142. return $result;
  143. }
  144. }