PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/CartItemRepositoryTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 360 lines | 257 code | 48 blank | 55 comment | 11 complexity | 79bf756f3f00855616ca375abc9073fd MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Api;
  7. use Magento\TestFramework\TestCase\WebapiAbstract;
  8. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  9. class CartItemRepositoryTest extends WebapiAbstract
  10. {
  11. const SERVICE_NAME = 'quoteCartItemRepositoryV1';
  12. const SERVICE_VERSION = 'V1';
  13. const CONFIGURABLE_PRODUCT_SKU = 'configurable';
  14. /**
  15. * @var \Magento\TestFramework\ObjectManager
  16. */
  17. protected $objectManager;
  18. protected function setUp()
  19. {
  20. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  21. }
  22. /**
  23. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  24. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  25. */
  26. public function testAddProduct()
  27. {
  28. /** @var \Magento\Quote\Model\Quote $quote */
  29. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  30. $quote->load('test_order_1', 'reserved_order_id');
  31. $cartId = $quote->getId();
  32. $serviceInfo = [
  33. 'rest' => [
  34. 'resourcePath' => '/V1/carts/' . $cartId . '/items',
  35. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  36. ],
  37. 'soap' => [
  38. 'service' => self::SERVICE_NAME,
  39. 'serviceVersion' => self::SERVICE_VERSION,
  40. 'operation' => self::SERVICE_NAME . 'Save',
  41. ],
  42. ];
  43. $response = $this->_webApiCall($serviceInfo, $this->getRequestData($cartId));
  44. $this->assertNotNull($response['item_id']);
  45. $this->assertEquals(Configurable::TYPE_CODE, $response['product_type']);
  46. $quote->load('test_order_1', 'reserved_order_id');
  47. $items = $quote->getAllItems();
  48. $this->assertGreaterThan(0, count($items));
  49. /** @var \Magento\Quote\Model\ResourceModel\Quote\Item|null $item */
  50. $item = null;
  51. /** @var \Magento\Quote\Model\ResourceModel\Quote\Item $quoteItem */
  52. foreach ($items as $quoteItem) {
  53. if ($quoteItem->getProductType() == Configurable::TYPE_CODE && !$quoteItem->getParentItemId()) {
  54. $item = $quoteItem;
  55. break;
  56. }
  57. }
  58. $this->assertNotNull($item);
  59. }
  60. /**
  61. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  62. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  63. * @expectedException \Exception
  64. * @expectedExceptionMessage You need to choose options for your item.
  65. */
  66. public function testAddProductWithIncorrectOptions()
  67. {
  68. /** @var \Magento\Quote\Model\Quote $quote */
  69. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  70. $quote->load('test_order_1', 'reserved_order_id');
  71. $cartId = $quote->getId();
  72. $serviceInfo = [
  73. 'rest' => [
  74. 'resourcePath' => '/V1/carts/' . $cartId . '/items',
  75. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  76. ],
  77. 'soap' => [
  78. 'service' => self::SERVICE_NAME,
  79. 'serviceVersion' => self::SERVICE_VERSION,
  80. 'operation' => self::SERVICE_NAME . 'Save',
  81. ],
  82. ];
  83. $requestData = $this->getRequestData($cartId);
  84. $requestData['cartItem']['product_option']['extension_attributes']
  85. ['configurable_item_options'][0]['option_id'] = 1000;
  86. $requestData['cartItem']['product_option']['extension_attributes']
  87. ['configurable_item_options'][0]['option_value'] = 2000;
  88. $this->_webApiCall($serviceInfo, $requestData);
  89. }
  90. /**
  91. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
  92. * @expectedException \Exception
  93. * @expectedExceptionMessage Cart %1 does not contain item %2
  94. */
  95. public function testUpdateIncorrectItem()
  96. {
  97. $qty = 1;
  98. /** @var \Magento\Quote\Model\Quote $quote */
  99. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  100. $quote->load('test_cart_with_configurable', 'reserved_order_id');
  101. $cartId = $quote->getId();
  102. $requestData = $this->getRequestData($cartId, 1);
  103. $requestData['cartItem']['qty'] = $qty;
  104. $requestData['cartItem']['item_id'] = 1000;
  105. $serviceInfo = [
  106. 'rest' => [
  107. 'resourcePath' => '/V1/carts/' . $cartId . '/items/1000',
  108. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT
  109. ],
  110. 'soap' => [
  111. 'service' => self::SERVICE_NAME,
  112. 'serviceVersion' => self::SERVICE_VERSION,
  113. 'operation' => self::SERVICE_NAME . 'Save',
  114. ],
  115. ];
  116. $this->_webApiCall($serviceInfo, $requestData);
  117. }
  118. /**
  119. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
  120. */
  121. public function testUpdate()
  122. {
  123. $qty = 4;
  124. $this->updateStockForItem(10, 100);
  125. $this->updateStockForItem(20, 100);
  126. /** @var \Magento\Quote\Model\Quote $quote */
  127. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  128. $quote->load('test_cart_with_configurable', 'reserved_order_id');
  129. $cartId = $quote->getId();
  130. $items = $quote->getAllItems();
  131. $this->assertGreaterThan(0, count($items));
  132. /** @var \Magento\Quote\Model\ResourceModel\Quote\Item|null $item */
  133. $item = null;
  134. /** @var \Magento\Quote\Model\ResourceModel\Quote\Item $quoteItem */
  135. foreach ($items as $quoteItem) {
  136. if ($quoteItem->getProductType() == Configurable::TYPE_CODE) {
  137. $item = $quoteItem;
  138. break;
  139. }
  140. }
  141. $this->assertNotNull($item);
  142. $this->assertNotNull($item->getId());
  143. $this->assertEquals(Configurable::TYPE_CODE, $item->getProductType());
  144. $requestData = $this->getRequestData($cartId, 1);
  145. $requestData['cartItem']['qty'] = $qty;
  146. $requestData['cartItem']['item_id'] = $item->getId();
  147. $serviceInfo = [
  148. 'rest' => [
  149. 'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $item->getId(),
  150. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT
  151. ],
  152. 'soap' => [
  153. 'service' => self::SERVICE_NAME,
  154. 'serviceVersion' => self::SERVICE_VERSION,
  155. 'operation' => self::SERVICE_NAME . 'Save',
  156. ],
  157. ];
  158. $response = $this->_webApiCall($serviceInfo, $requestData);
  159. $this->assertNotNull($response['item_id']);
  160. $this->assertEquals(Configurable::TYPE_CODE, $response['product_type']);
  161. $this->assertEquals($cartId, $response['quote_id']);
  162. $this->assertEquals($qty, $response['qty']);
  163. $this->assertEquals(
  164. $response['product_option']['extension_attributes']['configurable_item_options'][0],
  165. $requestData['cartItem']['product_option']['extension_attributes']['configurable_item_options'][0]
  166. );
  167. }
  168. /**
  169. * @param int $itemId
  170. * @param int $qty
  171. */
  172. protected function updateStockForItem($itemId, $qty)
  173. {
  174. /** @var \Magento\CatalogInventory\Model\Stock\Status $stockStatus */
  175. $stockStatus = $this->objectManager->create(\Magento\CatalogInventory\Model\Stock\Status::class);
  176. $stockStatus->load($itemId, 'product_id');
  177. if (!$stockStatus->getProductId()) {
  178. $stockStatus->setProductId($itemId);
  179. }
  180. $stockStatus->setQty($qty);
  181. $stockStatus->setStockStatus(1);
  182. $stockStatus->save();
  183. /** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
  184. $stockItem = $this->objectManager->create(\Magento\CatalogInventory\Model\Stock\Item::class);
  185. $stockItem->load($itemId, 'product_id');
  186. if (!$stockItem->getProductId()) {
  187. $stockItem->setProductId($itemId);
  188. }
  189. $stockItem->setUseConfigManageStock(1);
  190. $stockItem->setQty($qty);
  191. $stockItem->setIsQtyDecimal(0);
  192. $stockItem->setIsInStock(1);
  193. $stockItem->save();
  194. }
  195. /**
  196. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
  197. */
  198. public function testUpdateQty()
  199. {
  200. $qty = 1;
  201. /** @var \Magento\Quote\Model\Quote $quote */
  202. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  203. $quote->load('test_cart_with_configurable', 'reserved_order_id');
  204. $cartId = $quote->getId();
  205. $items = $quote->getAllItems();
  206. $this->assertGreaterThan(0, count($items));
  207. /** @var \Magento\Quote\Model\ResourceModel\Quote\Item|null $item */
  208. $item = null;
  209. /** @var \Magento\Quote\Model\ResourceModel\Quote\Item $quoteItem */
  210. foreach ($items as $quoteItem) {
  211. if ($quoteItem->getProductType() == Configurable::TYPE_CODE) {
  212. $item = $quoteItem;
  213. break;
  214. }
  215. }
  216. $serviceInfo = [
  217. 'rest' => [
  218. 'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $item->getId(),
  219. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT
  220. ],
  221. 'soap' => [
  222. 'service' => self::SERVICE_NAME,
  223. 'serviceVersion' => self::SERVICE_VERSION,
  224. 'operation' => self::SERVICE_NAME . 'Save',
  225. ],
  226. ];
  227. $this->assertNotNull($item);
  228. $this->assertNotNull($item->getId());
  229. $this->assertEquals(Configurable::TYPE_CODE, $item->getProductType());
  230. $requestData = $this->getRequestData($cartId);
  231. $requestData['cartItem']['qty'] = $qty;
  232. $requestData['cartItem']['item_id'] = $item->getId();
  233. $requestData['cartItem']['product_option'] = null;
  234. $response = $this->_webApiCall($serviceInfo, $requestData);
  235. $this->assertNotNull($response['item_id']);
  236. $this->assertEquals($item->getId(), $response['item_id']);
  237. $this->assertEquals(Configurable::TYPE_CODE, $response['product_type']);
  238. $this->assertEquals($cartId, $response['quote_id']);
  239. $this->assertEquals($qty, $response['qty']);
  240. }
  241. /**
  242. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
  243. */
  244. public function testGetList()
  245. {
  246. /** @var \Magento\Quote\Model\Quote $quote */
  247. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  248. $quote->load('test_cart_with_configurable', 'reserved_order_id');
  249. $cartId = $quote->getId();
  250. $serviceInfo = [
  251. 'rest' => [
  252. 'resourcePath' => '/V1/carts/' . $cartId . '/items',
  253. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET
  254. ],
  255. 'soap' => [
  256. 'service' => self::SERVICE_NAME,
  257. 'serviceVersion' => self::SERVICE_VERSION,
  258. 'operation' => self::SERVICE_NAME . 'GetList',
  259. ],
  260. ];
  261. $response = $this->_webApiCall($serviceInfo, ['cartId' => $cartId]);
  262. $this->assertGreaterThan(0, count($response));
  263. $item = $response[0];
  264. $this->assertNotNull($item['item_id']);
  265. $this->assertEquals(Configurable::TYPE_CODE, $item['product_type']);
  266. $this->assertArrayHasKey('product_option', $item);
  267. $this->assertArrayHasKey('extension_attributes', $item['product_option']);
  268. $this->assertArrayHasKey('configurable_item_options', $item['product_option']['extension_attributes']);
  269. $options = $item['product_option']['extension_attributes']['configurable_item_options'];
  270. $this->assertGreaterThan(0, count($options));
  271. $this->assertArrayHasKey('option_id', $options[0]);
  272. $this->assertArrayHasKey('option_value', $options[0]);
  273. $this->assertNotNull($options[0]['option_id']);
  274. $this->assertNotNull($options[0]['option_value']);
  275. }
  276. /**
  277. * @param $cartId
  278. * @param null $selectedOption
  279. * @return array
  280. */
  281. protected function getRequestData($cartId, $selectedOption = null)
  282. {
  283. /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
  284. $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
  285. $product = $productRepository->get(self::CONFIGURABLE_PRODUCT_SKU);
  286. $configurableProductOptions = $product->getExtensionAttributes()->getConfigurableProductOptions();
  287. $optionKey = 0;
  288. if ($selectedOption && isset($options[$selectedOption])) {
  289. $optionKey = $selectedOption;
  290. }
  291. $attributeId = $configurableProductOptions[0]->getAttributeId();
  292. $options = $configurableProductOptions[0]->getOptions();
  293. $optionId = $options[$optionKey]['value_index'];
  294. return [
  295. 'cartItem' => [
  296. 'sku' => self::CONFIGURABLE_PRODUCT_SKU,
  297. 'qty' => 1,
  298. 'quote_id' => $cartId,
  299. 'product_option' => [
  300. 'extension_attributes' => [
  301. 'configurable_item_options' => [
  302. [
  303. 'option_id' => $attributeId,
  304. 'option_value' => $optionId
  305. ]
  306. ]
  307. ]
  308. ]
  309. ]
  310. ];
  311. }
  312. }