44 * Copyright © Magento, Inc. All rights reserved.
55 * See COPYING.txt for license details.
66 */
7+ declare (strict_types=1 );
8+
79namespace Magento \ConfigurableProduct \Api ;
810
911use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
@@ -43,8 +45,10 @@ protected function setUp(): void
4345
4446 /**
4547 * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
48+ *
49+ * @return void
4650 */
47- public function testGetChildren ()
51+ public function testGetChildren (): void
4852 {
4953 $ productSku = 'configurable ' ;
5054
@@ -72,8 +76,10 @@ public function testGetChildren()
7276 * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
7377 * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_simple_77.php
7478 * @magentoApiDataFixture Magento/ConfigurableProduct/_files/delete_association.php
79+ *
80+ * @return void
7581 */
76- public function testAddChild ()
82+ public function testAddChild (): void
7783 {
7884 $ productSku = 'configurable ' ;
7985 $ childSku = 'simple_77 ' ;
@@ -85,8 +91,10 @@ public function testAddChild()
8591 * Test the full flow of creating a configurable product and adding a child via REST
8692 *
8793 * @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
94+ *
95+ * @return void
8896 */
89- public function testAddChildFullRestCreation ()
97+ public function testAddChildFullRestCreation (): void
9098 {
9199 $ productSku = 'configurable-product-sku ' ;
92100 $ childSku = 'simple-product-sku ' ;
@@ -96,7 +104,7 @@ public function testAddChildFullRestCreation()
96104
97105 $ this ->addOptionToConfigurableProduct (
98106 $ productSku ,
99- $ attribute ->getAttributeId (),
107+ ( int ) $ attribute ->getAttributeId (),
100108 [
101109 [
102110 'value_index ' => $ attribute ->getOptions ()[1 ]->getValue ()
@@ -139,8 +147,10 @@ public function testAddChildFullRestCreation()
139147 * configurable product.
140148 *
141149 * @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attributes_for_position_test.php
150+ *
151+ * @return void
142152 */
143- public function testConfigurableOptionPositionPreservation ()
153+ public function testConfigurableOptionPositionPreservation (): void
144154 {
145155 $ productSku = 'configurable-product-sku ' ;
146156 $ childProductSkus = [
@@ -160,19 +170,19 @@ public function testConfigurableOptionPositionPreservation()
160170 /** @var Attribute $attribute */
161171 $ attribute = $ this ->attributeRepository ->get ('catalog_product ' , $ attributeToAdd );
162172
163- /** @var Option $options[] */
173+ /** @var Option $options [] */
164174 $ options = $ attribute ->getOptions ();
165175 array_shift ($ options );
166176
167177 $ attributeOptions [$ attributeToAdd ] = $ options ;
168178
169179 $ valueIndexesData = [];
170180 foreach ($ options as $ option ) {
171- $ valueIndexesData []['value_index ' ]= $ option ->getValue ();
181+ $ valueIndexesData []['value_index ' ] = $ option ->getValue ();
172182 }
173183 $ this ->addOptionToConfigurableProduct (
174184 $ productSku ,
175- $ attribute ->getAttributeId (),
185+ ( int ) $ attribute ->getAttributeId (),
176186 $ valueIndexesData ,
177187 $ position
178188 );
@@ -284,7 +294,14 @@ protected function getConfigurableAttribute(string $productSku): array
284294 return $ this ->_webApiCall ($ serviceInfo , ['sku ' => $ productSku ]);
285295 }
286296
287- private function addChild ($ productSku , $ childSku )
297+ /**
298+ * Perform add child product Api call
299+ *
300+ * @param string $productSku
301+ * @param string $childSku
302+ * @return array|int|string|float|bool
303+ */
304+ private function addChild (string $ productSku , string $ childSku )
288305 {
289306 $ serviceInfo = [
290307 'rest ' => [
@@ -300,7 +317,13 @@ private function addChild($productSku, $childSku)
300317 return $ this ->_webApiCall ($ serviceInfo , ['sku ' => $ productSku , 'childSku ' => $ childSku ]);
301318 }
302319
303- protected function createConfigurableProduct ($ productSku )
320+ /**
321+ * Perform create configurable product api call
322+ *
323+ * @param string $productSku
324+ * @return array|bool|float|int|string
325+ */
326+ protected function createConfigurableProduct (string $ productSku )
304327 {
305328 $ requestData = [
306329 'product ' => [
@@ -325,8 +348,21 @@ protected function createConfigurableProduct($productSku)
325348 return $ this ->_webApiCall ($ serviceInfo , $ requestData );
326349 }
327350
328- protected function addOptionToConfigurableProduct ($ productSku , $ attributeId , $ attributeValues , $ position = 0 )
329- {
351+ /**
352+ * Add option to configurable product
353+ *
354+ * @param string $productSku
355+ * @param int $attributeId
356+ * @param array $attributeValues
357+ * @param int $position
358+ * @return array|bool|float|int|string
359+ */
360+ protected function addOptionToConfigurableProduct (
361+ string $ productSku ,
362+ int $ attributeId ,
363+ array $ attributeValues ,
364+ int $ position = 0
365+ ) {
330366 $ requestData = [
331367 'sku ' => $ productSku ,
332368 'option ' => [
@@ -339,7 +375,7 @@ protected function addOptionToConfigurableProduct($productSku, $attributeId, $at
339375 ];
340376 $ serviceInfo = [
341377 'rest ' => [
342- 'resourcePath ' => '/V1/configurable-products/ ' . $ productSku .'/options ' ,
378+ 'resourcePath ' => '/V1/configurable-products/ ' . $ productSku . '/options ' ,
343379 'httpMethod ' => Request::HTTP_METHOD_POST ,
344380 ],
345381 'soap ' => [
@@ -381,15 +417,67 @@ protected function createSimpleProduct($sku, $customAttributes)
381417
382418 /**
383419 * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
420+ *
421+ * @return void
384422 */
385- public function testRemoveChild ()
423+ public function testRemoveChild (): void
386424 {
387425 $ productSku = 'configurable ' ;
388426 $ childSku = 'simple_10 ' ;
389427 $ this ->assertTrue ($ this ->removeChild ($ productSku , $ childSku ));
390428 }
391429
392- protected function removeChild ($ productSku , $ childSku )
430+ /**
431+ * @dataProvider errorsDataProvider
432+ *
433+ * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
434+ * @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
435+ *
436+ * @param string $parentSku
437+ * @param string $childSku
438+ * @param string $errorMessage
439+ * @return void
440+ */
441+ public function testAddChildWithError (string $ parentSku , string $ childSku , string $ errorMessage ): void
442+ {
443+ $ this ->expectException (\Exception::class);
444+ $ this ->expectExceptionMessage ($ errorMessage );
445+ $ this ->addChild ($ parentSku , $ childSku );
446+ }
447+
448+ /**
449+ * @return array
450+ */
451+ public function errorsDataProvider (): array
452+ {
453+ return [
454+ 'simple_instead_of_configurable ' => [
455+ 'parent_sku ' => 'simple2 ' ,
456+ 'child_sku ' => 'configurable ' ,
457+ 'error_message ' => (string )__ ("The parent product doesn't have configurable product options. " ),
458+ ],
459+ 'simple_with_empty_configurable_attribute_value ' => [
460+ 'parent_sku ' => 'configurable ' ,
461+ 'child_sku ' => 'simple2 ' ,
462+ 'error_message ' => TESTS_WEB_API_ADAPTER === self ::ADAPTER_SOAP
463+ ? (string )__ (
464+ 'The child product doesn \'t have the "%1" attribute value. Verify the value and try again. '
465+ )
466+ : (string )__ (
467+ 'The child product doesn \'t have the \\"%1 \\" attribute value. Verify the value and try again. '
468+ ),
469+ ],
470+ ];
471+ }
472+
473+ /**
474+ * Remove child product
475+ *
476+ * @param string $productSku
477+ * @param string $childSku
478+ * @return array|bool|float|int|string
479+ */
480+ protected function removeChild (string $ productSku , string $ childSku )
393481 {
394482 $ resourcePath = self ::RESOURCE_PATH . '/%s/children/%s ' ;
395483 $ serviceInfo = [
@@ -408,14 +496,16 @@ protected function removeChild($productSku, $childSku)
408496 }
409497
410498 /**
499+ * Get child products
500+ *
411501 * @param string $productSku
412502 * @return string[]
413503 */
414- protected function getChildren ($ productSku )
504+ protected function getChildren (string $ productSku )
415505 {
416506 $ serviceInfo = [
417507 'rest ' => [
418- 'resourcePath ' => self ::RESOURCE_PATH . '/ ' . $ productSku . '/children ' ,
508+ 'resourcePath ' => self ::RESOURCE_PATH . '/ ' . $ productSku . '/children ' ,
419509 'httpMethod ' => Request::HTTP_METHOD_GET
420510 ],
421511 'soap ' => [
0 commit comments