CartItemRepositoryTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\Catalog\Model\CustomOptions\CustomOptionProcessor;
  9. use Magento\Framework\Webapi\Rest\Request;
  10. use Magento\Quote\Model\Quote;
  11. use Magento\TestFramework\TestCase\WebapiAbstract;
  12. class CartItemRepositoryTest extends WebapiAbstract
  13. {
  14. const SERVICE_VERSION = 'V1';
  15. const SERVICE_NAME = 'quoteCartItemRepositoryV1';
  16. const RESOURCE_PATH = '/V1/carts/';
  17. /**
  18. * @var \Magento\TestFramework\ObjectManager
  19. */
  20. protected $objectManager;
  21. protected function setUp()
  22. {
  23. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  24. }
  25. /**
  26. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_and_custom_options_saved.php
  27. */
  28. public function testGetList()
  29. {
  30. /** @var Quote $quote */
  31. $quote = $this->objectManager->create(Quote::class);
  32. $quote->load('test_order_item_with_items_and_custom_options', 'reserved_order_id');
  33. $cartId = $quote->getId();
  34. $output = [];
  35. $customOptionProcessor = $this->objectManager->get(CustomOptionProcessor::class);
  36. /** @var \Magento\Quote\Api\Data\CartItemInterface $item */
  37. foreach ($quote->getAllItems() as $item) {
  38. $customOptionProcessor->processOptions($item);
  39. $data = [
  40. 'item_id' => (int)$item->getItemId(),
  41. 'sku' => $item->getSku(),
  42. 'name' => $item->getName(),
  43. 'price' => (float)$item->getPrice(),
  44. 'qty' => (float)$item->getQty(),
  45. 'product_type' => $item->getProductType(),
  46. 'quote_id' => $item->getQuoteId(),
  47. ];
  48. if ($item->getProductOption() !== null) {
  49. $customOptions = $item->getProductOption()->getExtensionAttributes()->getCustomOptions();
  50. foreach ($customOptions as $option) {
  51. $data['product_option']['extension_attributes']['custom_options'][] = $option->getData();
  52. }
  53. }
  54. $output[] = $data;
  55. }
  56. $serviceInfo = [
  57. 'rest' => [
  58. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items',
  59. 'httpMethod' => Request::HTTP_METHOD_GET,
  60. ],
  61. 'soap' => [
  62. 'service' => self::SERVICE_NAME,
  63. 'serviceVersion' => self::SERVICE_VERSION,
  64. 'operation' => self::SERVICE_NAME . 'GetList',
  65. ],
  66. ];
  67. $requestData = ["cartId" => $cartId];
  68. $this->assertEquals($output, $this->_webApiCall($serviceInfo, $requestData));
  69. }
  70. /**
  71. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  72. * @magentoApiDataFixture Magento/Catalog/_files/product_without_options.php
  73. */
  74. public function testAddItem()
  75. {
  76. /** @var \Magento\Catalog\Model\Product $product */
  77. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(2);
  78. $productSku = $product->getSku();
  79. /** @var Quote $quote */
  80. $quote = $this->objectManager->create(Quote::class);
  81. $quote->load('test_order_1', 'reserved_order_id');
  82. $cartId = $quote->getId();
  83. $serviceInfo = [
  84. 'rest' => [
  85. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items',
  86. 'httpMethod' => Request::HTTP_METHOD_POST,
  87. ],
  88. 'soap' => [
  89. 'service' => self::SERVICE_NAME,
  90. 'serviceVersion' => self::SERVICE_VERSION,
  91. 'operation' => self::SERVICE_NAME . 'Save',
  92. ],
  93. ];
  94. $requestData = [
  95. "cartItem" => [
  96. "sku" => $productSku,
  97. "qty" => 7,
  98. "quote_id" => $cartId,
  99. ],
  100. ];
  101. $this->_webApiCall($serviceInfo, $requestData);
  102. $this->assertTrue($quote->hasProductId(2));
  103. $this->assertEquals(7, $quote->getItemByProduct($product)->getQty());
  104. }
  105. /**
  106. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
  107. */
  108. public function testRemoveItem()
  109. {
  110. /** @var Quote $quote */
  111. $quote = $this->objectManager->create(Quote::class);
  112. $quote->load('test_order_item_with_items', 'reserved_order_id');
  113. $cartId = $quote->getId();
  114. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  115. $productId = $product->getIdBySku('simple_one');
  116. $product->load($productId);
  117. $itemId = $quote->getItemByProduct($product)->getId();
  118. $serviceInfo = [
  119. 'rest' => [
  120. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
  121. 'httpMethod' => Request::HTTP_METHOD_DELETE,
  122. ],
  123. 'soap' => [
  124. 'service' => self::SERVICE_NAME,
  125. 'serviceVersion' => self::SERVICE_VERSION,
  126. 'operation' => self::SERVICE_NAME . 'DeleteById',
  127. ],
  128. ];
  129. $requestData = [
  130. "cartId" => $cartId,
  131. "itemId" => $itemId,
  132. ];
  133. $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
  134. $quote = $this->objectManager->create(Quote::class);
  135. $quote->load('test_order_item_with_items', 'reserved_order_id');
  136. $this->assertFalse($quote->hasProductId($productId));
  137. }
  138. /**
  139. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
  140. */
  141. public function testUpdateItem()
  142. {
  143. /** @var Quote $quote */
  144. $quote = $this->objectManager->create(Quote::class);
  145. $quote->load('test_order_item_with_items', 'reserved_order_id');
  146. $cartId = $quote->getId();
  147. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
  148. $productId = $product->getIdBySku('simple_one');
  149. $product->load($productId);
  150. $itemId = $quote->getItemByProduct($product)->getId();
  151. $serviceInfo = [
  152. 'rest' => [
  153. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
  154. 'httpMethod' => Request::HTTP_METHOD_PUT,
  155. ],
  156. 'soap' => [
  157. 'service' => self::SERVICE_NAME,
  158. 'serviceVersion' => self::SERVICE_VERSION,
  159. 'operation' => self::SERVICE_NAME . 'Save',
  160. ],
  161. ];
  162. if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
  163. $requestData = [
  164. "cartItem" => [
  165. "qty" => 5,
  166. "quote_id" => $cartId,
  167. "itemId" => $itemId,
  168. ],
  169. ];
  170. } else {
  171. $requestData = [
  172. "cartItem" => [
  173. "qty" => 5,
  174. "quote_id" => $cartId,
  175. ],
  176. ];
  177. }
  178. $this->_webApiCall($serviceInfo, $requestData);
  179. $quote = $this->objectManager->create(Quote::class);
  180. $quote->load('test_order_item_with_items', 'reserved_order_id');
  181. $this->assertTrue($quote->hasProductId(1));
  182. $item = $quote->getItemByProduct($product);
  183. $this->assertEquals(5, $item->getQty());
  184. $this->assertEquals($itemId, $item->getItemId());
  185. }
  186. }