ProductLinkManagementTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Bundle\Api;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. class ProductLinkManagementTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  10. {
  11. const SERVICE_NAME = 'bundleProductLinkManagementV1';
  12. const SERVICE_VERSION = 'V1';
  13. const RESOURCE_PATH = '/V1/bundle-products';
  14. /**
  15. * @magentoApiDataFixture Magento/Bundle/_files/product.php
  16. */
  17. public function testGetChildren()
  18. {
  19. $productSku = 'bundle-product';
  20. $expected = [
  21. [
  22. 'sku' => 'simple',
  23. 'position' => 0,
  24. 'qty' => 1,
  25. ],
  26. ];
  27. $result = $this->getChildren($productSku);
  28. $this->assertArrayHasKey(0, $result);
  29. $this->assertArrayHasKey('option_id', $result[0]);
  30. $this->assertArrayHasKey('is_default', $result[0]);
  31. $this->assertArrayHasKey('can_change_quantity', $result[0]);
  32. $this->assertArrayHasKey('price', $result[0]);
  33. $this->assertArrayHasKey('price_type', $result[0]);
  34. $this->assertNotNull($result[0]['id']);
  35. unset($result[0]['option_id'], $result[0]['is_default'], $result[0]['can_change_quantity']);
  36. unset($result[0]['price'], $result[0]['price_type'], $result[0]['id']);
  37. ksort($result[0]);
  38. ksort($expected[0]);
  39. $this->assertEquals($expected, $result);
  40. }
  41. /**
  42. * @magentoApiDataFixture Magento/Bundle/_files/product.php
  43. */
  44. public function testRemoveChild()
  45. {
  46. $productSku = 'bundle-product';
  47. $childSku = 'simple';
  48. $optionIds = $this->getProductOptions(3);
  49. $optionId = array_shift($optionIds);
  50. $this->assertTrue($this->removeChild($productSku, $optionId, $childSku));
  51. }
  52. /**
  53. * @magentoApiDataFixture Magento/Bundle/_files/product.php
  54. * @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
  55. */
  56. public function testAddChild()
  57. {
  58. $productSku = 'bundle-product';
  59. $children = $this->getChildren($productSku);
  60. $optionId = $children[0]['option_id'];
  61. $linkedProduct = [
  62. 'sku' => 'virtual-product',
  63. 'option_id' => $optionId,
  64. 'position' => '1',
  65. 'is_default' => 1,
  66. 'priceType' => 2,
  67. 'price' => 151.34,
  68. 'qty' => 8,
  69. 'can_change_quantity' => 1,
  70. ];
  71. $childId = $this->addChild($productSku, $optionId, $linkedProduct);
  72. $this->assertGreaterThan(0, $childId);
  73. }
  74. /**
  75. * @magentoApiDataFixture Magento/Bundle/_files/product.php
  76. * @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
  77. */
  78. public function testSaveChild()
  79. {
  80. $productSku = 'bundle-product';
  81. $children = $this->getChildren($productSku);
  82. $linkedProduct = $children[0];
  83. //Modify a few fields
  84. $linkedProduct['is_default'] = true;
  85. $linkedProduct['qty'] = 2;
  86. $this->assertTrue($this->saveChild($productSku, $linkedProduct));
  87. $children = $this->getChildren($productSku);
  88. $this->assertEquals($linkedProduct, $children[0]);
  89. }
  90. /**
  91. * @param string $productSku
  92. * @param array $linkedProduct
  93. * @return string
  94. */
  95. private function saveChild($productSku, $linkedProduct)
  96. {
  97. $resourcePath = self::RESOURCE_PATH . '/:sku/links/:id';
  98. $serviceInfo = [
  99. 'rest' => [
  100. 'resourcePath' => str_replace(
  101. [':sku', ':id'],
  102. [$productSku, $linkedProduct['id']],
  103. $resourcePath
  104. ),
  105. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  106. ],
  107. 'soap' => [
  108. 'service' => self::SERVICE_NAME,
  109. 'serviceVersion' => self::SERVICE_VERSION,
  110. 'operation' => self::SERVICE_NAME . 'SaveChild',
  111. ],
  112. ];
  113. return $this->_webApiCall(
  114. $serviceInfo,
  115. ['sku' => $productSku, 'linkedProduct' => $linkedProduct]
  116. );
  117. }
  118. /**
  119. * @param string $productSku
  120. * @param int $optionId
  121. * @param array $linkedProduct
  122. * @return string
  123. */
  124. private function addChild($productSku, $optionId, $linkedProduct)
  125. {
  126. $resourcePath = self::RESOURCE_PATH . '/:sku/links/:optionId';
  127. $serviceInfo = [
  128. 'rest' => [
  129. 'resourcePath' => str_replace(
  130. [':sku', ':optionId'],
  131. [$productSku, $optionId],
  132. $resourcePath
  133. ),
  134. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  135. ],
  136. 'soap' => [
  137. 'service' => self::SERVICE_NAME,
  138. 'serviceVersion' => self::SERVICE_VERSION,
  139. 'operation' => self::SERVICE_NAME . 'AddChildByProductSku',
  140. ],
  141. ];
  142. return $this->_webApiCall(
  143. $serviceInfo,
  144. ['sku' => $productSku, 'optionId' => $optionId, 'linkedProduct' => $linkedProduct]
  145. );
  146. }
  147. protected function getProductOptions($productId)
  148. {
  149. /** @var \Magento\Catalog\Model\Product $product */
  150. $product = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Product::class);
  151. $product->load($productId);
  152. /** @var \Magento\Bundle\Model\Product\Type $type */
  153. $type = Bootstrap::getObjectManager()->get(\Magento\Bundle\Model\Product\Type::class);
  154. return $type->getOptionsIds($product);
  155. }
  156. protected function removeChild($productSku, $optionId, $childSku)
  157. {
  158. $resourcePath = self::RESOURCE_PATH . '/%s/options/%s/children/%s';
  159. $serviceInfo = [
  160. 'rest' => [
  161. 'resourcePath' => sprintf($resourcePath, $productSku, $optionId, $childSku),
  162. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  163. ],
  164. 'soap' => [
  165. 'service' => self::SERVICE_NAME,
  166. 'serviceVersion' => self::SERVICE_VERSION,
  167. 'operation' => self::SERVICE_NAME . 'removeChild',
  168. ],
  169. ];
  170. $requestData = ['sku' => $productSku, 'optionId' => $optionId, 'childSku' => $childSku];
  171. return $this->_webApiCall($serviceInfo, $requestData);
  172. }
  173. /**
  174. * @param string $productSku
  175. * @return string
  176. */
  177. protected function getChildren($productSku)
  178. {
  179. $serviceInfo = [
  180. 'rest' => [
  181. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/children',
  182. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  183. ],
  184. 'soap' => [
  185. 'service' => self::SERVICE_NAME,
  186. 'serviceVersion' => self::SERVICE_VERSION,
  187. 'operation' => self::SERVICE_NAME . 'getChildren',
  188. ],
  189. ];
  190. return $this->_webApiCall($serviceInfo, ['productSku' => $productSku]);
  191. }
  192. }