CartItemRepositoryTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Api;
  7. use Magento\TestFramework\TestCase\WebapiAbstract;
  8. class CartItemRepositoryTest extends WebapiAbstract
  9. {
  10. const SERVICE_VERSION = 'V1';
  11. const SERVICE_NAME = 'quoteCartItemRepositoryV1';
  12. const RESOURCE_PATH = '/V1/carts/';
  13. /**
  14. * @var \Magento\TestFramework\ObjectManager
  15. */
  16. protected $objectManager;
  17. protected function setUp()
  18. {
  19. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  20. }
  21. /**
  22. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_bundle_and_options.php
  23. */
  24. public function testGetAll()
  25. {
  26. /** @var $quote \Magento\Quote\Model\Quote */
  27. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
  28. 'test_order_bundle',
  29. 'reserved_order_id'
  30. );
  31. $quoteId = $quote->getId();
  32. $serviceInfo = [
  33. 'rest' => [
  34. 'resourcePath' => self::RESOURCE_PATH . $quoteId . '/items',
  35. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  36. ],
  37. 'soap' => [
  38. 'service' => self::SERVICE_NAME,
  39. 'serviceVersion' => self::SERVICE_VERSION,
  40. 'operation' => self::SERVICE_NAME . 'GetList',
  41. ],
  42. ];
  43. $response = $this->_webApiCall($serviceInfo, ['cartId' => $quoteId]);
  44. $this->assertEquals(1, count($response));
  45. $response = $response[0];
  46. $bundleOption = $quote->getItemById($response['item_id'])->getBuyRequest()->getBundleOption();
  47. $bundleOptionQty = $quote->getItemById($response['item_id'])->getBuyRequest()->getBundleOptionQty();
  48. $actualOptions = $response['product_option']['extension_attributes']['bundle_options'];
  49. $this->assertEquals(array_keys($bundleOption), array_column($actualOptions, 'option_id'));
  50. $this->assertEquals($bundleOptionQty, array_column($actualOptions, 'option_qty', 'option_id'));
  51. foreach ($actualOptions as $option) {
  52. $id = $option['option_id'];
  53. $expectedSelections = is_array($bundleOption[$id]) ? $bundleOption[$id] : [$bundleOption[$id]];
  54. $this->assertEquals($expectedSelections, $option['option_selections']);
  55. }
  56. }
  57. /**
  58. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
  59. * @magentoApiDataFixture Magento/Bundle/_files/product.php
  60. */
  61. public function testAddItem()
  62. {
  63. /** @var \Magento\Catalog\Model\Product $product */
  64. $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(3);
  65. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
  66. 'test_order_item_with_items',
  67. 'reserved_order_id'
  68. );
  69. $itemQty = 1;
  70. $bundleProductOptions = $product->getExtensionAttributes()->getBundleProductOptions();
  71. $bundleOptionId = $bundleProductOptions[0]->getId();
  72. $optionSelections = $bundleProductOptions[0]->getProductLinks()[0]->getId();
  73. $buyRequest = [
  74. 'bundle_option' => [$bundleOptionId => [$optionSelections]],
  75. 'bundle_option_qty' => [$bundleOptionId => 1],
  76. 'qty' => $itemQty,
  77. 'original_qty' => $itemQty
  78. ];
  79. $productSku = $product->getSku();
  80. $productId = $product->getId();
  81. /** @var \Magento\Quote\Model\Quote $quote */
  82. $cartId = $quote->getId();
  83. $serviceInfo = [
  84. 'rest' => [
  85. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items',
  86. 'httpMethod' => \Magento\Framework\Webapi\Rest\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" => $itemQty,
  98. "quote_id" => $cartId,
  99. "product_option" => [
  100. "extension_attributes" => [
  101. "bundle_options" => [
  102. [
  103. "option_id" => (int)$bundleOptionId,
  104. "option_qty" => $itemQty,
  105. "option_selections" => [(int)$optionSelections]
  106. ]
  107. ]
  108. ]
  109. ]
  110. ]
  111. ];
  112. $response = $this->_webApiCall($serviceInfo, $requestData);
  113. $this->assertTrue($quote->hasProductId($productId));
  114. $this->assertEquals($buyRequest, $quote->getItemById($response['item_id'])->getBuyRequest()->getData());
  115. }
  116. /**
  117. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_bundle_and_options.php
  118. */
  119. public function testUpdate()
  120. {
  121. /** @var \Magento\Quote\Model\Quote $quote */
  122. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
  123. 'test_order_bundle',
  124. 'reserved_order_id'
  125. );
  126. $cartId = $quote->getId();
  127. $cartItem = $quote->getAllVisibleItems()[0];
  128. $itemSku = $cartItem->getSku();
  129. $itemId = $cartItem->getId();
  130. $product = $cartItem->getProduct();
  131. /** @var $typeInstance \Magento\Bundle\Model\Product\Type */
  132. $typeInstance = $product->getTypeInstance();
  133. $typeInstance->setStoreFilter($product->getStoreId(), $product);
  134. $optionCollection = $typeInstance->getOptionsCollection($product);
  135. $bundleOptions = [];
  136. /** @var $option \Magento\Bundle\Model\Option */
  137. foreach ($optionCollection as $option) {
  138. if (!$option->getRequired()) {
  139. continue;
  140. }
  141. $selectionsCollection = $typeInstance->getSelectionsCollection([$option->getId()], $product);
  142. $option = ['option_id' => $option->getId(), 'option_qty' => 1];
  143. $option['option_selections'] = [$selectionsCollection->getLastItem()->getSelectionId()];
  144. $bundleOptions[] = $option;
  145. }
  146. $serviceInfo = [
  147. 'rest' => [
  148. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
  149. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  150. ],
  151. 'soap' => [
  152. 'service' => self::SERVICE_NAME,
  153. 'serviceVersion' => self::SERVICE_VERSION,
  154. 'operation' => self::SERVICE_NAME . 'Save',
  155. ],
  156. ];
  157. $requestData = [
  158. "cartItem" => [
  159. "sku" => $itemSku,
  160. "qty" => 2,
  161. "quote_id" => $cartId,
  162. "item_id" => $itemId,
  163. "product_option" => [
  164. "extension_attributes" => [
  165. "bundle_options" => $bundleOptions
  166. ]
  167. ]
  168. ]
  169. ];
  170. $this->_webApiCall($serviceInfo, $requestData);
  171. $quoteUpdated = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
  172. 'test_order_bundle',
  173. 'reserved_order_id'
  174. );
  175. $cartItems = $quoteUpdated->getAllVisibleItems();
  176. $buyRequest = $cartItems[0]->getBuyRequest()->toArray();
  177. $this->assertEquals(1, count($cartItems));
  178. $this->assertEquals(count($buyRequest['bundle_option']), count($bundleOptions));
  179. foreach ($bundleOptions as $option) {
  180. $optionId = $option['option_id'];
  181. $optionQty = $option['option_qty'];
  182. $optionSelections = $option['option_selections'];
  183. $this->assertArrayHasKey($optionId, $buyRequest['bundle_option']);
  184. $this->assertEquals($optionQty, $buyRequest['bundle_option_qty'][$optionId]);
  185. $this->assertEquals($optionSelections, $buyRequest['bundle_option'][$optionId]);
  186. }
  187. }
  188. }