@@ -54,9 +54,7 @@ protected function setUp()
54
54
$ productFactoryMock = $ this ->createMock (\Magento \Catalog \Model \ProductFactory::class);
55
55
$ this ->catalogProductLink = $ this ->createMock (\Magento \GroupedProduct \Model \ResourceModel \Product \Link::class);
56
56
$ this ->productStatusMock = $ this ->createMock (\Magento \Catalog \Model \Product \Attribute \Source \Status::class);
57
- $ this ->serializer = $ this ->getMockBuilder (\Magento \Framework \Serialize \Serializer \Json::class)
58
- ->setMethods (['serialize ' ])
59
- ->getMockForAbstractClass ();
57
+ $ this ->serializer = $ this ->objectHelper ->getObject (\Magento \Framework \Serialize \Serializer \Json::class);
60
58
61
59
$ this ->_model = $ this ->objectHelper ->getObject (
62
60
\Magento \GroupedProduct \Model \Product \Type \Grouped::class,
@@ -419,9 +417,6 @@ public function testPrepareForCartAdvancedNoProductsStrictFalse()
419
417
->expects ($ this ->atLeastOnce ())
420
418
->method ('getData ' )
421
419
->will ($ this ->returnValue ($ associatedProducts ));
422
- $ this ->serializer ->expects ($ this ->any ())
423
- ->method ('serialize ' )
424
- ->willReturn (json_encode ($ buyRequest ->getData ()));
425
420
426
421
$ this ->assertEquals (
427
422
[0 => $ this ->product ],
@@ -521,10 +516,6 @@ public function testPrepareForCartAdvancedWithProductsStrictFalse()
521
516
$ buyRequest = new \Magento \Framework \DataObject ();
522
517
$ buyRequest ->setSuperGroup ([$ associatedId => 1 ]);
523
518
524
- $ this ->serializer ->expects ($ this ->any ())
525
- ->method ('serialize ' )
526
- ->willReturn (json_encode ($ buyRequest ->getData ()));
527
-
528
519
$ cached = true ;
529
520
$ this ->product
530
521
->expects ($ this ->atLeastOnce ())
@@ -541,49 +532,36 @@ public function testPrepareForCartAdvancedWithProductsStrictFalse()
541
532
);
542
533
}
543
534
544
- public function testPrepareForCartAdvancedWithProductsStrictTrue ()
545
- {
546
- $ associatedProduct = $ this ->createMock (\Magento \Catalog \Model \Product::class);
547
- $ associatedId = 9384 ;
548
- $ associatedProduct ->expects ($ this ->atLeastOnce ())->method ('getId ' )->will ($ this ->returnValue ($ associatedId ));
549
-
550
- $ typeMock = $ this ->createPartialMock (
551
- \Magento \Catalog \Model \Product \Type \AbstractType::class,
552
- ['_prepareProduct ' , 'deleteTypeSpecificData ' ]
553
- );
554
- $ associatedPrepareResult = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
555
- ->setMockClassName ('resultProduct ' )
556
- ->disableOriginalConstructor ()
557
- ->getMock ();
558
- $ typeMock ->expects ($ this ->once ())->method ('_prepareProduct ' )->willReturn ([$ associatedPrepareResult ]);
559
-
560
- $ associatedProduct ->expects ($ this ->once ())->method ('getTypeInstance ' )->willReturn ($ typeMock );
561
-
562
- $ buyRequest = new \Magento \Framework \DataObject ();
563
- $ buyRequest ->setSuperGroup ([$ associatedId => 1 ]);
564
-
565
- $ this ->serializer ->expects ($ this ->any ())
566
- ->method ('serialize ' )
567
- ->willReturn (json_encode ($ buyRequest ->getData ()));
568
-
569
- $ cached = true ;
570
- $ this ->product
571
- ->expects ($ this ->atLeastOnce ())
572
- ->method ('hasData ' )
573
- ->will ($ this ->returnValue ($ cached ));
574
- $ this ->product
575
- ->expects ($ this ->atLeastOnce ())
576
- ->method ('getData ' )
577
- ->will ($ this ->returnValue ([$ associatedProduct ]));
578
-
579
- $ associatedPrepareResult ->expects ($ this ->at (1 ))->method ('addCustomOption ' )->with (
580
- 'product_type ' ,
581
- 'grouped ' ,
582
- $ this ->product
583
- );
535
+ /**
536
+ * Test prepareForCartAdvanced() method in full mode
537
+ *
538
+ * @dataProvider prepareForCartAdvancedWithProductsStrictTrueDataProvider
539
+ * @param array $subProducts
540
+ * @param array $buyRequest
541
+ * @param mixed $expectedResult
542
+ */
543
+ public function testPrepareForCartAdvancedWithProductsStrictTrue (
544
+ array $ subProducts ,
545
+ array $ buyRequest ,
546
+ $ expectedResult
547
+ ) {
548
+ $ associatedProducts = $ this ->configureProduct ($ subProducts );
549
+ $ buyRequestObject = new \Magento \Framework \DataObject ();
550
+ $ buyRequestObject ->setSuperGroup ($ buyRequest );
551
+ $ associatedProductsById = [];
552
+ foreach ($ associatedProducts as $ associatedProduct ) {
553
+ $ associatedProductsById [$ associatedProduct ->getId ()] = $ associatedProduct ;
554
+ }
555
+ if (is_array ($ expectedResult )) {
556
+ $ expectedResultArray = $ expectedResult ;
557
+ $ expectedResult = [];
558
+ foreach ($ expectedResultArray as $ id ) {
559
+ $ expectedResult [] = $ associatedProductsById [$ id ];
560
+ }
561
+ }
584
562
$ this ->assertEquals (
585
- [ $ associatedPrepareResult ] ,
586
- $ this ->_model ->prepareForCartAdvanced ($ buyRequest , $ this ->product )
563
+ $ expectedResult ,
564
+ $ this ->_model ->prepareForCartAdvanced ($ buyRequestObject , $ this ->product )
587
565
);
588
566
}
589
567
@@ -618,4 +596,120 @@ public function testFlushAssociatedProductsCache()
618
596
->willReturnSelf ();
619
597
$ this ->assertEquals ($ productMock , $ this ->_model ->flushAssociatedProductsCache ($ productMock ));
620
598
}
599
+
600
+ /**
601
+ * @return array
602
+ */
603
+ public function prepareForCartAdvancedWithProductsStrictTrueDataProvider (): array
604
+ {
605
+ return [
606
+ [
607
+ [
608
+ [
609
+ 'getId ' => 1 ,
610
+ 'getQty ' => 100 ,
611
+ 'isSalable ' => true
612
+ ],
613
+ [
614
+ 'getId ' => 2 ,
615
+ 'getQty ' => 200 ,
616
+ 'isSalable ' => true
617
+ ]
618
+ ],
619
+ [
620
+ 1 => 2 ,
621
+ 2 => 1 ,
622
+ ],
623
+ [1 , 2 ]
624
+ ],
625
+ [
626
+ [
627
+ [
628
+ 'getId ' => 1 ,
629
+ 'getQty ' => 100 ,
630
+ 'isSalable ' => true
631
+ ],
632
+ [
633
+ 'getId ' => 2 ,
634
+ 'getQty ' => 0 ,
635
+ 'isSalable ' => false
636
+ ]
637
+ ],
638
+ [
639
+ 1 => 2 ,
640
+ ],
641
+ [1 ]
642
+ ],
643
+ [
644
+ [
645
+ [
646
+ 'getId ' => 1 ,
647
+ 'getQty ' => 0 ,
648
+ 'isSalable ' => true
649
+ ],
650
+ [
651
+ 'getId ' => 2 ,
652
+ 'getQty ' => 0 ,
653
+ 'isSalable ' => false
654
+ ]
655
+ ],
656
+ [
657
+ ],
658
+ 'Please specify the quantity of product(s). '
659
+ ],
660
+ [
661
+ [
662
+ [
663
+ 'getId ' => 1 ,
664
+ 'getQty ' => 0 ,
665
+ 'isSalable ' => false
666
+ ],
667
+ [
668
+ 'getId ' => 2 ,
669
+ 'getQty ' => 0 ,
670
+ 'isSalable ' => false
671
+ ]
672
+ ],
673
+ [
674
+ ],
675
+ 'Please specify the quantity of product(s). '
676
+ ]
677
+ ];
678
+ }
679
+
680
+ /**
681
+ * Configure sub-products of grouped product
682
+ *
683
+ * @param array $subProducts
684
+ * @return array
685
+ */
686
+ private function configureProduct (array $ subProducts ): array
687
+ {
688
+ $ associatedProducts = [];
689
+ foreach ($ subProducts as $ data ) {
690
+ $ associatedProduct = $ this ->createMock (\Magento \Catalog \Model \Product::class);
691
+ foreach ($ data as $ method => $ value ) {
692
+ $ associatedProduct ->method ($ method )->willReturn ($ value );
693
+ }
694
+ $ associatedProducts [] = $ associatedProduct ;
695
+
696
+ $ typeMock = $ this ->createPartialMock (
697
+ \Magento \Catalog \Model \Product \Type \AbstractType::class,
698
+ ['_prepareProduct ' , 'deleteTypeSpecificData ' ]
699
+ );
700
+ $ typeMock ->method ('_prepareProduct ' )->willReturn ([$ associatedProduct ]);
701
+ $ associatedProduct ->method ('getTypeInstance ' )->willReturn ($ typeMock );
702
+ }
703
+ $ this ->product
704
+ ->expects ($ this ->atLeastOnce ())
705
+ ->method ('hasData ' )
706
+ ->with ('_cache_instance_associated_products ' )
707
+ ->willReturn (true );
708
+ $ this ->product
709
+ ->expects ($ this ->atLeastOnce ())
710
+ ->method ('getData ' )
711
+ ->with ('_cache_instance_associated_products ' )
712
+ ->willReturn ($ associatedProducts );
713
+ return $ associatedProducts ;
714
+ }
621
715
}
0 commit comments