4
4
* Copyright © Magento, Inc. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
+ declare (strict_types=1 );
8
+
7
9
namespace Magento \ConfigurableProduct \Api ;
8
10
9
11
use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
@@ -43,8 +45,10 @@ protected function setUp(): void
43
45
44
46
/**
45
47
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
48
+ *
49
+ * @return void
46
50
*/
47
- public function testGetChildren ()
51
+ public function testGetChildren (): void
48
52
{
49
53
$ productSku = 'configurable ' ;
50
54
@@ -72,8 +76,10 @@ public function testGetChildren()
72
76
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
73
77
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_simple_77.php
74
78
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/delete_association.php
79
+ *
80
+ * @return void
75
81
*/
76
- public function testAddChild ()
82
+ public function testAddChild (): void
77
83
{
78
84
$ productSku = 'configurable ' ;
79
85
$ childSku = 'simple_77 ' ;
@@ -85,8 +91,10 @@ public function testAddChild()
85
91
* Test the full flow of creating a configurable product and adding a child via REST
86
92
*
87
93
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
94
+ *
95
+ * @return void
88
96
*/
89
- public function testAddChildFullRestCreation ()
97
+ public function testAddChildFullRestCreation (): void
90
98
{
91
99
$ productSku = 'configurable-product-sku ' ;
92
100
$ childSku = 'simple-product-sku ' ;
@@ -96,7 +104,7 @@ public function testAddChildFullRestCreation()
96
104
97
105
$ this ->addOptionToConfigurableProduct (
98
106
$ productSku ,
99
- $ attribute ->getAttributeId (),
107
+ ( int ) $ attribute ->getAttributeId (),
100
108
[
101
109
[
102
110
'value_index ' => $ attribute ->getOptions ()[1 ]->getValue ()
@@ -139,8 +147,10 @@ public function testAddChildFullRestCreation()
139
147
* configurable product.
140
148
*
141
149
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attributes_for_position_test.php
150
+ *
151
+ * @return void
142
152
*/
143
- public function testConfigurableOptionPositionPreservation ()
153
+ public function testConfigurableOptionPositionPreservation (): void
144
154
{
145
155
$ productSku = 'configurable-product-sku ' ;
146
156
$ childProductSkus = [
@@ -160,19 +170,19 @@ public function testConfigurableOptionPositionPreservation()
160
170
/** @var Attribute $attribute */
161
171
$ attribute = $ this ->attributeRepository ->get ('catalog_product ' , $ attributeToAdd );
162
172
163
- /** @var Option $options[] */
173
+ /** @var Option $options [] */
164
174
$ options = $ attribute ->getOptions ();
165
175
array_shift ($ options );
166
176
167
177
$ attributeOptions [$ attributeToAdd ] = $ options ;
168
178
169
179
$ valueIndexesData = [];
170
180
foreach ($ options as $ option ) {
171
- $ valueIndexesData []['value_index ' ]= $ option ->getValue ();
181
+ $ valueIndexesData []['value_index ' ] = $ option ->getValue ();
172
182
}
173
183
$ this ->addOptionToConfigurableProduct (
174
184
$ productSku ,
175
- $ attribute ->getAttributeId (),
185
+ ( int ) $ attribute ->getAttributeId (),
176
186
$ valueIndexesData ,
177
187
$ position
178
188
);
@@ -284,7 +294,14 @@ protected function getConfigurableAttribute(string $productSku): array
284
294
return $ this ->_webApiCall ($ serviceInfo , ['sku ' => $ productSku ]);
285
295
}
286
296
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 )
288
305
{
289
306
$ serviceInfo = [
290
307
'rest ' => [
@@ -300,7 +317,13 @@ private function addChild($productSku, $childSku)
300
317
return $ this ->_webApiCall ($ serviceInfo , ['sku ' => $ productSku , 'childSku ' => $ childSku ]);
301
318
}
302
319
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 )
304
327
{
305
328
$ requestData = [
306
329
'product ' => [
@@ -325,8 +348,21 @@ protected function createConfigurableProduct($productSku)
325
348
return $ this ->_webApiCall ($ serviceInfo , $ requestData );
326
349
}
327
350
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
+ ) {
330
366
$ requestData = [
331
367
'sku ' => $ productSku ,
332
368
'option ' => [
@@ -339,7 +375,7 @@ protected function addOptionToConfigurableProduct($productSku, $attributeId, $at
339
375
];
340
376
$ serviceInfo = [
341
377
'rest ' => [
342
- 'resourcePath ' => '/V1/configurable-products/ ' . $ productSku .'/options ' ,
378
+ 'resourcePath ' => '/V1/configurable-products/ ' . $ productSku . '/options ' ,
343
379
'httpMethod ' => Request::HTTP_METHOD_POST ,
344
380
],
345
381
'soap ' => [
@@ -381,15 +417,67 @@ protected function createSimpleProduct($sku, $customAttributes)
381
417
382
418
/**
383
419
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
420
+ *
421
+ * @return void
384
422
*/
385
- public function testRemoveChild ()
423
+ public function testRemoveChild (): void
386
424
{
387
425
$ productSku = 'configurable ' ;
388
426
$ childSku = 'simple_10 ' ;
389
427
$ this ->assertTrue ($ this ->removeChild ($ productSku , $ childSku ));
390
428
}
391
429
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 )
393
481
{
394
482
$ resourcePath = self ::RESOURCE_PATH . '/%s/children/%s ' ;
395
483
$ serviceInfo = [
@@ -408,14 +496,16 @@ protected function removeChild($productSku, $childSku)
408
496
}
409
497
410
498
/**
499
+ * Get child products
500
+ *
411
501
* @param string $productSku
412
502
* @return string[]
413
503
*/
414
- protected function getChildren ($ productSku )
504
+ protected function getChildren (string $ productSku )
415
505
{
416
506
$ serviceInfo = [
417
507
'rest ' => [
418
- 'resourcePath ' => self ::RESOURCE_PATH . '/ ' . $ productSku . '/children ' ,
508
+ 'resourcePath ' => self ::RESOURCE_PATH . '/ ' . $ productSku . '/children ' ,
419
509
'httpMethod ' => Request::HTTP_METHOD_GET
420
510
],
421
511
'soap ' => [
0 commit comments