Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductLinkManagementTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Bundle\Api;
8 
10 
12 {
13  const SERVICE_NAME = 'bundleProductLinkManagementV1';
14  const SERVICE_VERSION = 'V1';
15  const RESOURCE_PATH = '/V1/bundle-products';
16 
20  public function testGetChildren()
21  {
22  $productSku = 'bundle-product';
23  $expected = [
24  [
25  'sku' => 'simple',
26  'position' => 0,
27  'qty' => 1,
28  ],
29  ];
30 
31  $result = $this->getChildren($productSku);
32 
33  $this->assertArrayHasKey(0, $result);
34  $this->assertArrayHasKey('option_id', $result[0]);
35  $this->assertArrayHasKey('is_default', $result[0]);
36  $this->assertArrayHasKey('can_change_quantity', $result[0]);
37  $this->assertArrayHasKey('price', $result[0]);
38  $this->assertArrayHasKey('price_type', $result[0]);
39  $this->assertNotNull($result[0]['id']);
40 
41  unset($result[0]['option_id'], $result[0]['is_default'], $result[0]['can_change_quantity']);
42  unset($result[0]['price'], $result[0]['price_type'], $result[0]['id']);
43 
44  ksort($result[0]);
45  ksort($expected[0]);
46  $this->assertEquals($expected, $result);
47  }
48 
52  public function testRemoveChild()
53  {
54  $productSku = 'bundle-product';
55  $childSku = 'simple';
56  $optionIds = $this->getProductOptions(3);
57  $optionId = array_shift($optionIds);
58  $this->assertTrue($this->removeChild($productSku, $optionId, $childSku));
59  }
60 
65  public function testAddChild()
66  {
67  $productSku = 'bundle-product';
68  $children = $this->getChildren($productSku);
69 
70  $optionId = $children[0]['option_id'];
71 
72  $linkedProduct = [
73  'sku' => 'virtual-product',
74  'option_id' => $optionId,
75  'position' => '1',
76  'is_default' => 1,
77  'priceType' => 2,
78  'price' => 151.34,
79  'qty' => 8,
80  'can_change_quantity' => 1,
81  ];
82 
83  $childId = $this->addChild($productSku, $optionId, $linkedProduct);
84  $this->assertGreaterThan(0, $childId);
85  }
86 
91  public function testSaveChild()
92  {
93  $productSku = 'bundle-product';
94  $children = $this->getChildren($productSku);
95 
97 
98  //Modify a few fields
99  $linkedProduct['is_default'] = true;
100  $linkedProduct['qty'] = 2;
101 
102  $this->assertTrue($this->saveChild($productSku, $linkedProduct));
103  $children = $this->getChildren($productSku);
104  $this->assertEquals($linkedProduct, $children[0]);
105  }
106 
112  private function saveChild($productSku, $linkedProduct)
113  {
114  $resourcePath = self::RESOURCE_PATH . '/:sku/links/:id';
115  $serviceInfo = [
116  'rest' => [
117  'resourcePath' => str_replace(
118  [':sku', ':id'],
119  [$productSku, $linkedProduct['id']],
120  $resourcePath
121  ),
123  ],
124  'soap' => [
125  'service' => self::SERVICE_NAME,
126  'serviceVersion' => self::SERVICE_VERSION,
127  'operation' => self::SERVICE_NAME . 'SaveChild',
128  ],
129  ];
130  return $this->_webApiCall(
131  $serviceInfo,
132  ['sku' => $productSku, 'linkedProduct' => $linkedProduct]
133  );
134  }
135 
142  private function addChild($productSku, $optionId, $linkedProduct)
143  {
144  $resourcePath = self::RESOURCE_PATH . '/:sku/links/:optionId';
145  $serviceInfo = [
146  'rest' => [
147  'resourcePath' => str_replace(
148  [':sku', ':optionId'],
149  [$productSku, $optionId],
150  $resourcePath
151  ),
153  ],
154  'soap' => [
155  'service' => self::SERVICE_NAME,
156  'serviceVersion' => self::SERVICE_VERSION,
157  'operation' => self::SERVICE_NAME . 'AddChildByProductSku',
158  ],
159  ];
160  return $this->_webApiCall(
161  $serviceInfo,
162  ['sku' => $productSku, 'optionId' => $optionId, 'linkedProduct' => $linkedProduct]
163  );
164  }
165 
166  protected function getProductOptions($productId)
167  {
169  $product = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Product::class);
170  $product->load($productId);
172  $type = Bootstrap::getObjectManager()->get(\Magento\Bundle\Model\Product\Type::class);
173  return $type->getOptionsIds($product);
174  }
175 
176  protected function removeChild($productSku, $optionId, $childSku)
177  {
178  $resourcePath = self::RESOURCE_PATH . '/%s/options/%s/children/%s';
179  $serviceInfo = [
180  'rest' => [
181  'resourcePath' => sprintf($resourcePath, $productSku, $optionId, $childSku),
183  ],
184  'soap' => [
185  'service' => self::SERVICE_NAME,
186  'serviceVersion' => self::SERVICE_VERSION,
187  'operation' => self::SERVICE_NAME . 'removeChild',
188  ],
189  ];
190  $requestData = ['sku' => $productSku, 'optionId' => $optionId, 'childSku' => $childSku];
191  return $this->_webApiCall($serviceInfo, $requestData);
192  }
193 
198  protected function getChildren($productSku)
199  {
200  $serviceInfo = [
201  'rest' => [
202  'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/children',
204  ],
205  'soap' => [
206  'service' => self::SERVICE_NAME,
207  'serviceVersion' => self::SERVICE_VERSION,
208  'operation' => self::SERVICE_NAME . 'getChildren',
209  ],
210  ];
211  return $this->_webApiCall($serviceInfo, ['productSku' => $productSku]);
212  }
213 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$type
Definition: item.phtml:13
$linkedProduct
$children
Definition: actions.phtml:11