Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductCustomOptionRepositoryTest.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Catalog\Api;
9 
13 
15 {
19  protected $objectManager;
20 
21  const SERVICE_NAME = 'catalogProductCustomOptionRepositoryV1';
22 
26  protected $productFactory;
27 
28  protected function setUp()
29  {
31  $this->productFactory = $this->objectManager->get(\Magento\Catalog\Model\ProductFactory::class);
32  }
33 
38  public function testRemove()
39  {
40  $sku = 'simple';
42  $productRepository = $this->objectManager->create(
43  \Magento\Catalog\Model\ProductRepository::class
44  );
46  $product = $productRepository->get($sku, false, null, true);
47  $customOptions = $product->getOptions();
48  $optionId = array_pop($customOptions)->getId();
49  $serviceInfo = [
50  'rest' => [
51  'resourcePath' => "/V1/products/$sku/options/$optionId",
53  ],
54  'soap' => [
55  'service' => self::SERVICE_NAME,
56  'serviceVersion' => 'V1',
57  'operation' => self::SERVICE_NAME . 'DeleteByIdentifier',
58  ],
59  ];
60  $this->assertTrue($this->_webApiCall($serviceInfo, ['sku' => $sku, 'optionId' => $optionId]));
62  $product = $productRepository->get($sku, false, null, true);
63  $this->assertNull($product->getOptionById($optionId));
64  $this->assertEquals(9, count($product->getOptions()));
65  }
66 
71  public function testGet()
72  {
73  $productSku = 'simple';
76  ->get(\Magento\Catalog\Api\ProductCustomOptionRepositoryInterface::class);
77  $options = $service->getList('simple');
78  $option = current($options);
79  $optionId = $option->getOptionId();
80  $serviceInfo = [
81  'rest' => [
82  'resourcePath' => '/V1/products/' . $productSku . "/options/" . $optionId,
84  ],
85  'soap' => [
86  'service' => self::SERVICE_NAME,
87  'serviceVersion' => 'V1',
88  'operation' => self::SERVICE_NAME . 'Get',
89  ],
90  ];
91  $option = $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'optionId' => $optionId]);
92  unset($option['product_sku']);
93  unset($option['option_id']);
94  $excepted = include '_files/product_options.php';
95  $this->assertEquals($excepted[0], $option);
96  }
97 
103  public function testGetList()
104  {
105  $this->_markTestAsRestOnly('Fix inconsistencies in WSDL and Data interfaces');
106  $productSku = 'simple';
107  $serviceInfo = [
108  'rest' => [
109  'resourcePath' => '/V1/products/' . $productSku . "/options",
111  ],
112  'soap' => [
113  'service' => self::SERVICE_NAME,
114  'serviceVersion' => 'V1',
115  'operation' => self::SERVICE_NAME . 'GetList',
116  ],
117  ];
118  $options = $this->_webApiCall($serviceInfo, ['sku' => $productSku]);
119 
121  foreach ($options as $key => $value) {
122  unset($options[$key]['product_sku']);
123  unset($options[$key]['option_id']);
124  if (!empty($options[$key]['values'])) {
125  foreach ($options[$key]['values'] as $newKey => $valueData) {
126  unset($options[$key]['values'][$newKey]['option_type_id']);
127  }
128  }
129  }
130 
131  $excepted = include '_files/product_options.php';
132  $this->assertEquals(count($excepted), count($options));
133 
134  //in order to make assertion result readable we need to check each element separately
135  foreach ($excepted as $index => $value) {
136  $this->assertEquals($value, $options[$index]);
137  }
138  }
139 
146  public function testSave($optionData)
147  {
148  $productSku = 'simple';
149 
150  $optionDataPost = $optionData;
151  $optionDataPost['product_sku'] = $productSku;
152  $serviceInfo = [
153  'rest' => [
154  'resourcePath' => '/V1/products/options',
156  ],
157  'soap' => [
158  'service' => self::SERVICE_NAME,
159  'serviceVersion' => 'V1',
160  'operation' => self::SERVICE_NAME . 'Save',
161  ],
162  ];
163 
164  $result = $this->_webApiCall($serviceInfo, ['option' => $optionDataPost]);
165  unset($result['product_sku']);
166  unset($result['option_id']);
167  if (!empty($result['values'])) {
168  foreach (array_keys($result['values']) as $key) {
169  unset($result['values'][$key]['option_type_id']);
170  }
171  }
172  $this->assertEquals($optionData, $result);
173  }
174 
175  public function optionDataProvider()
176  {
177  $fixtureOptions = [];
178  $fixture = include '_files/product_options.php';
179  foreach ($fixture as $item) {
180  $fixtureOptions[$item['type']] = [
181  'optionData' => $item,
182  ];
183  };
184 
185  return $fixtureOptions;
186  }
187 
193  public function testAddNegative($optionData)
194  {
195  $productSku = 'simple';
196  $optionDataPost = $optionData;
197  $optionDataPost['product_sku'] = $productSku;
198  $serviceInfo = [
199  'rest' => [
200  'resourcePath' => "/V1/products/options",
202  ],
203  'soap' => [
204  'service' => self::SERVICE_NAME,
205  'serviceVersion' => 'V1',
206  'operation' => self::SERVICE_NAME . 'Save',
207  ],
208  ];
209 
210  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
211  if ($optionDataPost['title'] === null || $optionDataPost['title'] === '') {
212  $this->expectException('SoapFault');
213  $this->expectExceptionMessage('Missed values for option required fields');
214  } else {
215  $this->expectException('SoapFault');
216  $this->expectExceptionMessage('Invalid option');
217  }
218  } else {
219  $this->expectException('Exception');
220  $this->expectExceptionCode(400);
221  }
222  $this->_webApiCall($serviceInfo, ['option' => $optionDataPost]);
223  }
224 
225  public function optionNegativeDataProvider()
226  {
227  $fixtureOptions = [];
228  $fixture = include '_files/product_options_negative.php';
229  foreach ($fixture as $key => $item) {
230  $fixtureOptions[$key] = [
231  'optionData' => $item,
232  ];
233  };
234 
235  return $fixtureOptions;
236  }
237 
242  public function testUpdate()
243  {
244  $productSku = 'simple';
246  $productRepository = $this->objectManager->create(
247  \Magento\Catalog\Model\ProductRepository::class
248  );
249 
250  $options = $productRepository->get($productSku, true)->getOptions();
251  $option = array_shift($options);
252  $optionId = $option->getOptionId();
253  $optionDataPost = [
254  'product_sku' => $productSku,
255  'title' => $option->getTitle() . "_updated",
256  'type' => $option->getType(),
257  'sort_order' => (int)$option->getSortOrder(),
258  'is_require' => (bool)$option->getIsRequire(),
259  'price' => $option->getPrice(),
260  'price_type' => $option->getPriceType(),
261  'sku' => $option->getSku(),
262  'max_characters' => 500,
263  ];
264 
265  $serviceInfo = [
266  'rest' => [
267  'resourcePath' => '/V1/products/options/' . $optionId,
269  ],
270  'soap' => [
271  'service' => self::SERVICE_NAME,
272  'serviceVersion' => 'V1',
273  'operation' => self::SERVICE_NAME . 'Save',
274  ],
275  ];
276  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
277  $optionDataPost['option_id'] = $optionId;
278  $updatedOption = $this->_webApiCall(
279  $serviceInfo,
280  ['id' => $optionId, 'option' => $optionDataPost]
281  );
282  unset($optionDataPost['option_id']);//update change option id now
283  } else {
284  $updatedOption = $this->_webApiCall($serviceInfo, ['option' => $optionDataPost]);
285  }
286 
287  unset($updatedOption['values']);
288  unset($updatedOption['option_id']);//update change option id now
289  $this->assertEquals($optionDataPost, $updatedOption);
290  }
291 
300  public function testUpdateOptionAddingNewValue($optionType)
301  {
302  $fixtureOption = null;
303  $valueData = [
304  'price' => 100500,
305  'price_type' => 'fixed',
306  'sku' => 'new option sku ' . $optionType,
307  'title' => 'New Option Title',
308  'sort_order' => 100,
309  ];
310 
312  $productRepository = $this->objectManager->create(
313  \Magento\Catalog\Model\ProductRepository::class
314  );
316  $product = $productRepository->get('simple', false, null, true);
317 
319  foreach ($product->getOptions() as $option) {
320  if ($option->getType() == $optionType) {
321  $fixtureOption = $option;
322  break;
323  }
324  }
325 
326  $values = [];
327  foreach ($option->getValues() as $key => $value) {
328  $values[] =
329  [
330  'price' => $value->getPrice(),
331  'price_type' => $value->getPriceType(),
332  'sku' => $value->getSku(),
333  'title' => $value->getTitle(),
334  'sort_order' => $value->getSortOrder(),
335  ];
336  }
337  $values[] = $valueData;
338  $data = [
339  'product_sku' => $option->getProductSku(),
340  'title' => $option->getTitle(),
341  'type' => $option->getType(),
342  'is_require' => $option->getIsRequire(),
343  'sort_order' => $option->getSortOrder(),
344  'values' => $values,
345  ];
346 
347  $serviceInfo = [
348  'rest' => [
349  'resourcePath' => '/V1/products/options/' . $fixtureOption->getId(),
351  ],
352  'soap' => [
353  'service' => self::SERVICE_NAME,
354  'serviceVersion' => 'V1',
355  'operation' => self::SERVICE_NAME . 'Save',
356  ],
357  ];
358  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
359  $data['option_id'] = $fixtureOption->getId();
360  $valueObject = $this->_webApiCall(
361  $serviceInfo,
362  [ 'option_id' => $fixtureOption->getId(), 'option' => $data]
363  );
364  } else {
365  $valueObject = $this->_webApiCall($serviceInfo, ['option' => $data]);
366  }
367 
368  $values = end($valueObject['values']);
369  $this->assertEquals($valueData['price'], $values['price']);
370  $this->assertEquals($valueData['price_type'], $values['price_type']);
371  $this->assertEquals($valueData['sku'], $values['sku']);
372  $this->assertEquals('New Option Title', $values['title']);
373  $this->assertEquals(100, $values['sort_order']);
374  }
375 
376  public function validOptionDataProvider()
377  {
378  return [
379  'drop_down' => ['drop_down'],
380  'checkbox' => ['checkbox'],
381  'radio' => ['radio'],
382  'multiple' => ['multiple']
383  ];
384  }
385 
394  public function testUpdateNegative($optionData, $message, $exceptionCode)
395  {
396  $this->_markTestAsRestOnly();
397  $productSku = 'simple';
399  $productRepository = $this->objectManager->create(ProductRepository::class);
400  $options = $productRepository->get($productSku, true)->getOptions();
401  $option = array_shift($options);
402  $optionId = $option->getOptionId();
403 
404  $serviceInfo = [
405  'rest' => [
406  'resourcePath' => '/V1/products/options/' . $optionId,
408  ],
409  ];
410 
411  $this->expectException('Exception');
412  $this->expectExceptionMessage($message);
413  $this->expectExceptionCode($exceptionCode);
414  $this->_webApiCall($serviceInfo, ['option' => $optionData]);
415  }
416 
421  {
422  return include '_files/product_options_update_negative.php';
423  }
424 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$optionData
$values
Definition: options.phtml:88
$message
$value
Definition: gender.phtml:16
$index
Definition: list.phtml:44