@@ -271,15 +271,18 @@ public function testStockState()
271
271
}
272
272
273
273
/**
274
- * Tests adding of custom options with existing and new product
274
+ * Tests adding of custom options with existing and new product.
275
275
*
276
276
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
277
277
* @dataProvider getBehaviorDataProvider
278
278
* @param string $importFile
279
279
* @param string $sku
280
+ * @param int $expectedOptionsQty
280
281
* @magentoAppIsolation enabled
282
+ *
283
+ * @return void
281
284
*/
282
- public function testSaveCustomOptions ($ importFile , $ sku)
285
+ public function testSaveCustomOptions (string $ importFile , string $ sku, int $ expectedOptionsQty ): void
283
286
{
284
287
$ pathToFile = __DIR__ . '/_files/ ' . $ importFile ;
285
288
$ importModel = $ this ->createImportModel ($ pathToFile );
@@ -312,6 +315,7 @@ public function testSaveCustomOptions($importFile, $sku)
312
315
// assert of options data
313
316
$ this ->assertCount (count ($ expectedData ['data ' ]), $ actualData ['data ' ]);
314
317
$ this ->assertCount (count ($ expectedData ['values ' ]), $ actualData ['values ' ]);
318
+ $ this ->assertCount ($ expectedOptionsQty , $ actualData ['options ' ]);
315
319
foreach ($ expectedData ['options ' ] as $ expectedId => $ expectedOption ) {
316
320
$ elementExist = false ;
317
321
// find value in actual options and values
@@ -411,17 +415,24 @@ public function testSaveCustomOptionsWithMultipleStoreViews()
411
415
/**
412
416
* @return array
413
417
*/
414
- public function getBehaviorDataProvider ()
418
+ public function getBehaviorDataProvider (): array
415
419
{
416
420
return [
417
421
'Append behavior with existing product ' => [
418
- '$importFile ' => 'product_with_custom_options.csv ' ,
419
- '$sku ' => 'simple ' ,
422
+ 'importFile ' => 'product_with_custom_options.csv ' ,
423
+ 'sku ' => 'simple ' ,
424
+ 'expectedOptionsQty ' => 6 ,
425
+ ],
426
+ 'Append behavior with existing product and without options in import file ' => [
427
+ 'importFile ' => 'product_without_custom_options.csv ' ,
428
+ 'sku ' => 'simple ' ,
429
+ 'expectedOptionsQty ' => 0 ,
420
430
],
421
431
'Append behavior with new product ' => [
422
- '$importFile ' => 'product_with_custom_options_new.csv ' ,
423
- '$sku ' => 'simple_new ' ,
424
- ]
432
+ 'importFile ' => 'product_with_custom_options_new.csv ' ,
433
+ 'sku ' => 'simple_new ' ,
434
+ 'expectedOptionsQty ' => 4 ,
435
+ ],
425
436
];
426
437
}
427
438
@@ -571,43 +582,45 @@ protected function getExpectedOptionsData(string $pathToFile, string $storeCode
571
582
break ;
572
583
}
573
584
}
574
- foreach (explode ('| ' , $ productData ['data ' ][$ storeRowId ]['custom_options ' ]) as $ optionData ) {
575
- $ option = array_values (
576
- array_map (
577
- function ($ input ) {
578
- $ data = explode ('= ' , $ input );
579
- return [$ data [0 ] => $ data [1 ]];
580
- },
581
- explode (', ' , $ optionData )
582
- )
583
- );
584
- $ option = array_merge (...$ option );
585
-
586
- if (!empty ($ option ['type ' ]) && !empty ($ option ['name ' ])) {
587
- $ lastOptionKey = $ option ['type ' ] . '| ' . $ option ['name ' ];
588
- if (!isset ($ expectedOptions [$ expectedOptionId ])
589
- || $ expectedOptions [$ expectedOptionId ] != $ lastOptionKey ) {
590
- $ expectedOptionId ++;
591
- $ expectedOptions [$ expectedOptionId ] = $ lastOptionKey ;
592
- $ expectedData [$ expectedOptionId ] = [];
593
- foreach ($ this ->_assertOptions as $ assertKey => $ assertFieldName ) {
594
- if (array_key_exists ($ assertFieldName , $ option )
595
- && !(($ assertFieldName == 'price ' || $ assertFieldName == 'sku ' )
596
- && in_array ($ option ['type ' ], $ this ->specificTypes ))
597
- ) {
598
- $ expectedData [$ expectedOptionId ][$ assertKey ] = $ option [$ assertFieldName ];
585
+ if (!empty ($ productData ['data ' ][$ storeRowId ]['custom_options ' ])) {
586
+ foreach (explode ('| ' , $ productData ['data ' ][$ storeRowId ]['custom_options ' ]) as $ optionData ) {
587
+ $ option = array_values (
588
+ array_map (
589
+ function ($ input ) {
590
+ $ data = explode ('= ' , $ input );
591
+ return [$ data [0 ] => $ data [1 ]];
592
+ },
593
+ explode (', ' , $ optionData )
594
+ )
595
+ );
596
+ $ option = array_merge (...$ option );
597
+
598
+ if (!empty ($ option ['type ' ]) && !empty ($ option ['name ' ])) {
599
+ $ lastOptionKey = $ option ['type ' ] . '| ' . $ option ['name ' ];
600
+ if (!isset ($ expectedOptions [$ expectedOptionId ])
601
+ || $ expectedOptions [$ expectedOptionId ] != $ lastOptionKey ) {
602
+ $ expectedOptionId ++;
603
+ $ expectedOptions [$ expectedOptionId ] = $ lastOptionKey ;
604
+ $ expectedData [$ expectedOptionId ] = [];
605
+ foreach ($ this ->_assertOptions as $ assertKey => $ assertFieldName ) {
606
+ if (array_key_exists ($ assertFieldName , $ option )
607
+ && !(($ assertFieldName == 'price ' || $ assertFieldName == 'sku ' )
608
+ && in_array ($ option ['type ' ], $ this ->specificTypes ))
609
+ ) {
610
+ $ expectedData [$ expectedOptionId ][$ assertKey ] = $ option [$ assertFieldName ];
611
+ }
599
612
}
600
613
}
601
614
}
602
- }
603
- $ optionValue = [];
604
- if (! empty ( $ option [ ' name ' ]) && ! empty ( $ option [ ' option_title ' ]) ) {
605
- foreach ( $ this -> _assertOptionValues as $ assertKey => $ assertFieldName ) {
606
- if ( isset ( $ option [$ assertFieldName ])) {
607
- $ optionValue [ $ assertKey ] = $ option [ $ assertFieldName ];
615
+ $ optionValue = [];
616
+ if (! empty ( $ option [ ' name ' ]) && ! empty ( $ option [ ' option_title ' ])) {
617
+ foreach ( $ this -> _assertOptionValues as $ assertKey => $ assertFieldName ) {
618
+ if ( isset ( $ option [ $ assertFieldName]) ) {
619
+ $ optionValue [ $ assertKey ] = $ option [$ assertFieldName ];
620
+ }
608
621
}
622
+ $ expectedValues [$ expectedOptionId ][] = $ optionValue ;
609
623
}
610
- $ expectedValues [$ expectedOptionId ][] = $ optionValue ;
611
624
}
612
625
}
613
626
0 commit comments