CartTotalRepositoryTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Api;
  8. use Magento\Quote\Model\Cart\Totals;
  9. use Magento\Quote\Model\Cart\Totals\Item as ItemTotals;
  10. use Magento\Framework\Api\FilterBuilder;
  11. use Magento\Framework\Api\SearchCriteriaBuilder;
  12. use Magento\TestFramework\ObjectManager;
  13. use Magento\TestFramework\TestCase\WebapiAbstract;
  14. use Magento\Quote\Model\Quote;
  15. use Magento\Quote\Model\Quote\Address;
  16. class CartTotalRepositoryTest extends WebapiAbstract
  17. {
  18. /**
  19. * @var ObjectManager
  20. */
  21. private $objectManager;
  22. /**
  23. * @var SearchCriteriaBuilder
  24. */
  25. private $searchCriteriaBuilder;
  26. /**
  27. * @var FilterBuilder
  28. */
  29. private $filterBuilder;
  30. protected function setUp()
  31. {
  32. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  33. $this->searchCriteriaBuilder = $this->objectManager->create(
  34. \Magento\Framework\Api\SearchCriteriaBuilder::class
  35. );
  36. $this->filterBuilder = $this->objectManager->create(
  37. \Magento\Framework\Api\FilterBuilder::class
  38. );
  39. }
  40. /**
  41. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
  42. */
  43. public function testGetTotals()
  44. {
  45. /** @var \Magento\Quote\Model\Quote $quote */
  46. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  47. $quote->load('test_order_1', 'reserved_order_id');
  48. $cartId = $quote->getId();
  49. /** @var \Magento\Quote\Model\Quote\Address $shippingAddress */
  50. $shippingAddress = $quote->getShippingAddress();
  51. $data = $this->getData($quote, $shippingAddress);
  52. $data = $this->formatTotalsData($data);
  53. $requestData = ['cartId' => $cartId];
  54. $actual = $this->_webApiCall($this->getServiceInfoForTotalsService($cartId), $requestData);
  55. unset($actual['items'][0]['options']);
  56. unset($actual['weee_tax_applied_amount']);
  57. /** TODO: cover total segments with separate test */
  58. unset($actual['total_segments']);
  59. if (array_key_exists('extension_attributes', $actual)) {
  60. unset($actual['extension_attributes']);
  61. }
  62. $this->assertEquals($data, $actual);
  63. }
  64. /**
  65. * @expectedException \Exception
  66. * @expectedExceptionMessage No such entity
  67. */
  68. public function testGetTotalsWithAbsentQuote()
  69. {
  70. $cartId = 9999999999;
  71. $requestData = ['cartId' => $cartId];
  72. $this->_webApiCall($this->getServiceInfoForTotalsService($cartId), $requestData);
  73. }
  74. /**
  75. * Get service info for totals service
  76. *
  77. * @param string $cartId
  78. * @return array
  79. */
  80. protected function getServiceInfoForTotalsService($cartId)
  81. {
  82. return [
  83. 'soap' => [
  84. 'service' => 'quoteCartTotalRepositoryV1',
  85. 'serviceVersion' => 'V1',
  86. 'operation' => 'quoteCartTotalRepositoryV1get',
  87. ],
  88. 'rest' => [
  89. 'resourcePath' => '/V1/carts/' . $cartId . '/totals',
  90. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  91. ],
  92. ];
  93. }
  94. /**
  95. * Adjust response details for SOAP protocol
  96. *
  97. * @param array $data
  98. * @return array
  99. */
  100. protected function formatTotalsData($data)
  101. {
  102. foreach ($data as $key => $field) {
  103. if (is_numeric($field)) {
  104. $data[$key] = round($field, 1);
  105. if ($data[$key] === null) {
  106. $data[$key] = 0.0;
  107. }
  108. }
  109. }
  110. unset($data[Totals::KEY_BASE_SUBTOTAL_INCL_TAX]);
  111. return $data;
  112. }
  113. /**
  114. * Fetch quote item totals data from quote
  115. *
  116. * @param \Magento\Quote\Model\Quote $quote
  117. * @return array
  118. */
  119. protected function getQuoteItemTotalsData(\Magento\Quote\Model\Quote $quote)
  120. {
  121. $items = $quote->getAllItems();
  122. $item = array_shift($items);
  123. return [
  124. ItemTotals::KEY_ITEM_ID => $item->getItemId(),
  125. ItemTotals::KEY_PRICE => (int)$item->getPrice(),
  126. ItemTotals::KEY_BASE_PRICE => (int)$item->getBasePrice(),
  127. ItemTotals::KEY_QTY => $item->getQty(),
  128. ItemTotals::KEY_ROW_TOTAL => (int)$item->getRowTotal(),
  129. ItemTotals::KEY_BASE_ROW_TOTAL => (int)$item->getBaseRowTotal(),
  130. ItemTotals::KEY_ROW_TOTAL_WITH_DISCOUNT => (int)$item->getRowTotalWithDiscount(),
  131. ItemTotals::KEY_TAX_AMOUNT => (int)$item->getTaxAmount(),
  132. ItemTotals::KEY_BASE_TAX_AMOUNT => (int)$item->getBaseTaxAmount(),
  133. ItemTotals::KEY_TAX_PERCENT => (int)$item->getTaxPercent(),
  134. ItemTotals::KEY_DISCOUNT_AMOUNT => (int)$item->getDiscountAmount(),
  135. ItemTotals::KEY_BASE_DISCOUNT_AMOUNT => (int)$item->getBaseDiscountAmount(),
  136. ItemTotals::KEY_DISCOUNT_PERCENT => (int)$item->getDiscountPercent(),
  137. ItemTotals::KEY_PRICE_INCL_TAX => (int)$item->getPriceInclTax(),
  138. ItemTotals::KEY_BASE_PRICE_INCL_TAX => (int)$item->getBasePriceInclTax(),
  139. ItemTotals::KEY_ROW_TOTAL_INCL_TAX => (int)$item->getRowTotalInclTax(),
  140. ItemTotals::KEY_BASE_ROW_TOTAL_INCL_TAX => (int)$item->getBaseRowTotalInclTax(),
  141. ItemTotals::KEY_WEEE_TAX_APPLIED_AMOUNT => $item->getWeeeTaxAppliedAmount(),
  142. ItemTotals::KEY_WEEE_TAX_APPLIED => $item->getWeeeTaxApplied(),
  143. ItemTotals::KEY_NAME => $item->getName(),
  144. ];
  145. }
  146. /**
  147. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
  148. */
  149. public function testGetMyTotals()
  150. {
  151. $this->_markTestAsRestOnly();
  152. // get customer ID token
  153. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  154. $customerTokenService = $this->objectManager->create(
  155. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  156. );
  157. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  158. /** @var \Magento\Quote\Model\Quote $quote */
  159. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  160. $quote->load('test_order_1', 'reserved_order_id');
  161. $serviceInfo = [
  162. 'rest' => [
  163. 'resourcePath' => '/V1/carts/mine/totals',
  164. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  165. 'token' => $token
  166. ],
  167. ];
  168. /** @var \Magento\Quote\Model\Quote\Address $shippingAddress */
  169. $shippingAddress = $quote->getShippingAddress();
  170. $data = $this->getData($quote, $shippingAddress);
  171. $data = $this->formatTotalsData($data);
  172. $actual = $this->_webApiCall($serviceInfo);
  173. unset($actual['items'][0]['options']);
  174. unset($actual['weee_tax_applied_amount']);
  175. /** TODO: cover total segments with separate test */
  176. unset($actual['total_segments']);
  177. if (array_key_exists('extension_attributes', $actual)) {
  178. unset($actual['extension_attributes']);
  179. }
  180. $this->assertEquals($data, $actual);
  181. }
  182. /**
  183. * Get expected data.
  184. *
  185. * @param Quote $quote
  186. * @param Address $shippingAddress
  187. *
  188. * @return array
  189. */
  190. private function getData(Quote $quote, Address $shippingAddress) : array
  191. {
  192. return [
  193. Totals::KEY_GRAND_TOTAL => $quote->getGrandTotal(),
  194. Totals::KEY_BASE_GRAND_TOTAL => $quote->getBaseGrandTotal(),
  195. Totals::KEY_SUBTOTAL => $quote->getSubtotal(),
  196. Totals::KEY_BASE_SUBTOTAL => $quote->getBaseSubtotal(),
  197. Totals::KEY_DISCOUNT_AMOUNT => $shippingAddress->getDiscountAmount(),
  198. Totals::KEY_BASE_DISCOUNT_AMOUNT => $shippingAddress->getBaseDiscountAmount(),
  199. Totals::KEY_SUBTOTAL_WITH_DISCOUNT => $quote->getSubtotalWithDiscount(),
  200. Totals::KEY_BASE_SUBTOTAL_WITH_DISCOUNT => $quote->getBaseSubtotalWithDiscount(),
  201. Totals::KEY_SHIPPING_AMOUNT => $shippingAddress->getShippingAmount(),
  202. Totals::KEY_BASE_SHIPPING_AMOUNT => $shippingAddress->getBaseShippingAmount(),
  203. Totals::KEY_SHIPPING_DISCOUNT_AMOUNT => $shippingAddress->getShippingDiscountAmount(),
  204. Totals::KEY_BASE_SHIPPING_DISCOUNT_AMOUNT => $shippingAddress->getBaseShippingDiscountAmount(),
  205. Totals::KEY_TAX_AMOUNT => $shippingAddress->getTaxAmount(),
  206. Totals::KEY_BASE_TAX_AMOUNT => $shippingAddress->getBaseTaxAmount(),
  207. Totals::KEY_SHIPPING_TAX_AMOUNT => $shippingAddress->getShippingTaxAmount(),
  208. Totals::KEY_BASE_SHIPPING_TAX_AMOUNT => $shippingAddress->getBaseShippingTaxAmount(),
  209. Totals::KEY_SUBTOTAL_INCL_TAX => $shippingAddress->getSubtotalInclTax(),
  210. Totals::KEY_BASE_SUBTOTAL_INCL_TAX => $shippingAddress->getBaseSubtotalTotalInclTax(),
  211. Totals::KEY_SHIPPING_INCL_TAX => $shippingAddress->getShippingInclTax(),
  212. Totals::KEY_BASE_SHIPPING_INCL_TAX => $shippingAddress->getBaseShippingInclTax(),
  213. Totals::KEY_BASE_CURRENCY_CODE => $quote->getBaseCurrencyCode(),
  214. Totals::KEY_QUOTE_CURRENCY_CODE => $quote->getQuoteCurrencyCode(),
  215. Totals::KEY_ITEMS_QTY => $quote->getItemsQty(),
  216. Totals::KEY_ITEMS => [$this->getQuoteItemTotalsData($quote)],
  217. ];
  218. }
  219. }