Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductOptionRepositoryTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Bundle\Api;
8 
10 {
11  const SERVICE_NAME = 'bundleProductOptionRepositoryV1';
12  const SERVICE_VERSION = 'V1';
13  const RESOURCE_PATH = '/V1/bundle-products/:sku/options';
14 
18  public function testGet()
19  {
20  $productSku = 'bundle-product';
21  $expected = [
22  'required' => true,
23  'position' => 0,
24  'type' => 'select',
25  'title' => 'Bundle Product Items',
26  'sku' => $productSku,
27  'product_links' => [
28  [
29  'sku' => 'simple',
30  'qty' => 1,
31  'position' => 0,
32  'can_change_quantity' => 1,
33  'is_default' => false,
34  'price' => 2.75,
35  'price_type' => 0,
36  ],
37  ],
38  ];
39  $optionId = $this->getList($productSku)[0]['option_id'];
40  $result = $this->get($productSku, $optionId);
41 
42  $this->assertArrayHasKey('option_id', $result);
43  $expected['product_links'][0]['option_id'] = $result['option_id'];
44  unset($result['option_id']);
45  $this->assertNotNull($result['product_links'][0]['id']);
46  unset($result['product_links'][0]['id']);
47 
48  ksort($expected);
49  ksort($result);
50  ksort($expected['product_links'][0]);
51  ksort($result['product_links'][0]);
52  $this->assertEquals($expected, $result);
53  }
54 
58  public function testGetList()
59  {
60  $productSku = 'bundle-product';
61  $expected = [
62  [
63  'required' => true,
64  'position' => 0,
65  'type' => 'select',
66  'title' => 'Bundle Product Items',
67  'sku' => $productSku,
68  'product_links' => [
69  [
70  'sku' => 'simple',
71  'qty' => 1,
72  'position' => 0,
73  'can_change_quantity' => 1,
74  'is_default' => false,
75  'price' => 2.75,
76  'price_type' => 0,
77  ],
78  ],
79  ],
80  ];
81  $result = $this->getList($productSku);
82 
83  $this->assertArrayHasKey(0, $result);
84  $this->assertArrayHasKey('option_id', $result[0]);
85  $expected[0]['product_links'][0]['option_id'] = $result[0]['option_id'];
86  unset($result[0]['option_id']);
87  $this->assertNotNull($result[0]['product_links'][0]['id']);
88  unset($result[0]['product_links'][0]['id']);
89 
90  ksort($expected[0]);
91  ksort($result[0]);
92  ksort($expected[0]['product_links'][0]);
93  ksort($result[0]['product_links'][0]);
94  $this->assertEquals($expected, $result);
95  }
96 
101  public function testRemove()
102  {
103  $productSku = 'bundle-product';
104 
105  $optionId = $this->getList($productSku)[0]['option_id'];
106  $result = $this->remove($productSku, $optionId);
107 
108  $this->assertTrue($result);
109 
110  try {
111  $this->get($productSku, $optionId);
112  } catch (\Exception $e) {
113  throw new \Magento\Framework\Exception\NoSuchEntityException();
114  }
115  }
116 
120  public function testAdd()
121  {
122  $productSku = 'bundle-product';
123  $request = [
124  'required' => true,
125  'position' => 0,
126  'type' => 'select',
127  'title' => 'test product',
128  'product_links' => [],
129  'sku' => $productSku,
130  ];
131 
132  $optionId = $this->add($request);
133  $this->assertGreaterThan(0, $optionId);
134  $result = $this->get($productSku, $optionId);
135 
136  $this->assertArrayHasKey('option_id', $result);
137  $this->assertArrayHasKey('sku', $result);
138  unset($result['option_id']);
139 
140  ksort($result);
141  ksort($request);
142  $this->assertEquals($request, $result);
143  }
144 
148  public function testUpdate()
149  {
150  $productSku = 'bundle-product';
151  $request = [
152  'title' => 'someTitle',
153  'type' => 'select',
154  'required' => 1,
155  'sku' => $productSku,
156  ];
157 
158  $options = $this->getList($productSku);
159 
160  $this->assertGreaterThan(0, count($options));
161  $this->assertArrayHasKey('option_id', $options[0]);
162 
163  $optionId = $options[0]['option_id'];
164  $this->update($optionId, $request);
165 
166  $result = $this->get($productSku, $optionId);
167 
168  $this->assertCount(7, $result);
169  $this->assertArrayHasKey('title', $result);
170  $this->assertEquals($request['title'], $result['title']);
171  }
172 
178  protected function update($optionId, $option)
179  {
180  $serviceInfo = [
181  'rest' => [
182  'resourcePath' => '/V1/bundle-products/options/' . $optionId,
184  ],
185  'soap' => [
186  'service' => 'bundleProductOptionManagementV1',
187  'serviceVersion' => self::SERVICE_VERSION,
188  'operation' => 'bundleProductOptionManagementV1Save',
189  ],
190  ];
191 
192  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
193  $option['optionId'] = $optionId;
194  }
195  return $this->_webApiCall($serviceInfo, ['option' => $option]);
196  }
197 
202  protected function add($option)
203  {
204  $serviceInfo = [
205  'rest' => [
206  'resourcePath' => '/V1/bundle-products/options/add',
208  ],
209  'soap' => [
210  'service' => 'bundleProductOptionManagementV1',
211  'serviceVersion' => self::SERVICE_VERSION,
212  'operation' => 'bundleProductOptionManagementV1Save',
213  ],
214  ];
215  return $this->_webApiCall($serviceInfo, ['option' => $option]);
216  }
217 
223  protected function remove($productSku, $optionId)
224  {
225  $serviceInfo = [
226  'rest' => [
227  'resourcePath' => str_replace(':sku', $productSku, self::RESOURCE_PATH) . '/' . $optionId,
229  ],
230  'soap' => [
231  'service' => self::SERVICE_NAME,
232  'serviceVersion' => self::SERVICE_VERSION,
233  'operation' => self::SERVICE_NAME . 'DeleteById',
234  ],
235  ];
236  return $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'optionId' => $optionId]);
237  }
238 
243  protected function getList($productSku)
244  {
245  $serviceInfo = [
246  'rest' => [
247  'resourcePath' => str_replace(':sku', $productSku, self::RESOURCE_PATH) . '/all',
249  ],
250  'soap' => [
251  'service' => self::SERVICE_NAME,
252  'serviceVersion' => self::SERVICE_VERSION,
253  'operation' => self::SERVICE_NAME . 'GetList',
254  ],
255  ];
256  return $this->_webApiCall($serviceInfo, ['sku' => $productSku]);
257  }
258 
264  protected function get($productSku, $optionId)
265  {
266  $serviceInfo = [
267  'rest' => [
268  'resourcePath' => str_replace(':sku', $productSku, self::RESOURCE_PATH) . '/' . $optionId,
270  ],
271  'soap' => [
272  'service' => self::SERVICE_NAME,
273  'serviceVersion' => self::SERVICE_VERSION,
274  'operation' => self::SERVICE_NAME . 'Get',
275  ],
276  ];
277  return $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'optionId' => $optionId]);
278  }
279 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)