@@ -638,53 +638,35 @@ public function testPrepareForCartAdvancedWithProductsStrictFalse(): void
638638 }
639639
640640 /**
641- * Verify prepare for cart with Product strict option true
641+ * Test prepareForCartAdvanced() method in full mode
642642 *
643- * @return void
643+ * @dataProvider prepareForCartAdvancedWithProductsStrictTrueDataProvider
644+ * @param array $subProducts
645+ * @param array $buyRequest
646+ * @param mixed $expectedResult
644647 */
645- public function testPrepareForCartAdvancedWithProductsStrictTrue (): void
646- {
647- $ associatedProduct = $ this ->createMock (\Magento \Catalog \Model \Product::class);
648- $ associatedId = 9384 ;
649- $ associatedProduct ->expects ($ this ->atLeastOnce ())->method ('getId ' )->will ($ this ->returnValue ($ associatedId ));
650-
651- $ typeMock = $ this ->createPartialMock (
652- \Magento \Catalog \Model \Product \Type \AbstractType::class,
653- ['_prepareProduct ' , 'deleteTypeSpecificData ' ]
654- );
655- $ associatedPrepareResult = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
656- ->setMockClassName ('resultProduct ' )
657- ->disableOriginalConstructor ()
658- ->getMock ();
659- $ typeMock ->expects ($ this ->once ())->method ('_prepareProduct ' )->willReturn ([$ associatedPrepareResult ]);
660-
661- $ associatedProduct ->expects ($ this ->once ())->method ('getTypeInstance ' )->willReturn ($ typeMock );
662-
663- $ buyRequest = new \Magento \Framework \DataObject ();
664- $ buyRequest ->setSuperGroup ([$ associatedId => 1 ]);
665-
666- $ this ->serializer ->expects ($ this ->any ())
667- ->method ('serialize ' )
668- ->willReturn (json_encode ($ buyRequest ->getData ()));
669-
670- $ cached = true ;
671- $ this ->product
672- ->expects ($ this ->atLeastOnce ())
673- ->method ('hasData ' )
674- ->will ($ this ->returnValue ($ cached ));
675- $ this ->product
676- ->expects ($ this ->atLeastOnce ())
677- ->method ('getData ' )
678- ->will ($ this ->returnValue ([$ associatedProduct ]));
679-
680- $ associatedPrepareResult ->expects ($ this ->at (1 ))->method ('addCustomOption ' )->with (
681- 'product_type ' ,
682- 'grouped ' ,
683- $ this ->product
684- );
648+ public function testPrepareForCartAdvancedWithProductsStrictTrue (
649+ array $ subProducts ,
650+ array $ buyRequest ,
651+ $ expectedResult
652+ ) {
653+ $ associatedProducts = $ this ->configureProduct ($ subProducts );
654+ $ buyRequestObject = new \Magento \Framework \DataObject ();
655+ $ buyRequestObject ->setSuperGroup ($ buyRequest );
656+ $ associatedProductsById = [];
657+ foreach ($ associatedProducts as $ associatedProduct ) {
658+ $ associatedProductsById [$ associatedProduct ->getId ()] = $ associatedProduct ;
659+ }
660+ if (is_array ($ expectedResult )) {
661+ $ expectedResultArray = $ expectedResult ;
662+ $ expectedResult = [];
663+ foreach ($ expectedResultArray as $ id ) {
664+ $ expectedResult [] = $ associatedProductsById [$ id ];
665+ }
666+ }
685667 $ this ->assertEquals (
686- [ $ associatedPrepareResult ] ,
687- $ this ->_model ->prepareForCartAdvanced ($ buyRequest , $ this ->product )
668+ $ expectedResult ,
669+ $ this ->_model ->prepareForCartAdvanced ($ buyRequestObject , $ this ->product )
688670 );
689671 }
690672
@@ -729,4 +711,120 @@ public function testFlushAssociatedProductsCache(): void
729711 ->willReturnSelf ();
730712 $ this ->assertEquals ($ productMock , $ this ->_model ->flushAssociatedProductsCache ($ productMock ));
731713 }
714+
715+ /**
716+ * @return array
717+ */
718+ public function prepareForCartAdvancedWithProductsStrictTrueDataProvider (): array
719+ {
720+ return [
721+ [
722+ [
723+ [
724+ 'getId ' => 1 ,
725+ 'getQty ' => 100 ,
726+ 'isSalable ' => true
727+ ],
728+ [
729+ 'getId ' => 2 ,
730+ 'getQty ' => 200 ,
731+ 'isSalable ' => true
732+ ]
733+ ],
734+ [
735+ 1 => 2 ,
736+ 2 => 1 ,
737+ ],
738+ [1 , 2 ]
739+ ],
740+ [
741+ [
742+ [
743+ 'getId ' => 1 ,
744+ 'getQty ' => 100 ,
745+ 'isSalable ' => true
746+ ],
747+ [
748+ 'getId ' => 2 ,
749+ 'getQty ' => 0 ,
750+ 'isSalable ' => false
751+ ]
752+ ],
753+ [
754+ 1 => 2 ,
755+ ],
756+ [1 ]
757+ ],
758+ [
759+ [
760+ [
761+ 'getId ' => 1 ,
762+ 'getQty ' => 0 ,
763+ 'isSalable ' => true
764+ ],
765+ [
766+ 'getId ' => 2 ,
767+ 'getQty ' => 0 ,
768+ 'isSalable ' => false
769+ ]
770+ ],
771+ [
772+ ],
773+ 'Please specify the quantity of product(s). '
774+ ],
775+ [
776+ [
777+ [
778+ 'getId ' => 1 ,
779+ 'getQty ' => 0 ,
780+ 'isSalable ' => false
781+ ],
782+ [
783+ 'getId ' => 2 ,
784+ 'getQty ' => 0 ,
785+ 'isSalable ' => false
786+ ]
787+ ],
788+ [
789+ ],
790+ 'Please specify the quantity of product(s). '
791+ ]
792+ ];
793+ }
794+
795+ /**
796+ * Configure sub-products of grouped product
797+ *
798+ * @param array $subProducts
799+ * @return array
800+ */
801+ private function configureProduct (array $ subProducts ): array
802+ {
803+ $ associatedProducts = [];
804+ foreach ($ subProducts as $ data ) {
805+ $ associatedProduct = $ this ->createMock (\Magento \Catalog \Model \Product::class);
806+ foreach ($ data as $ method => $ value ) {
807+ $ associatedProduct ->method ($ method )->willReturn ($ value );
808+ }
809+ $ associatedProducts [] = $ associatedProduct ;
810+
811+ $ typeMock = $ this ->createPartialMock (
812+ \Magento \Catalog \Model \Product \Type \AbstractType::class,
813+ ['_prepareProduct ' , 'deleteTypeSpecificData ' ]
814+ );
815+ $ typeMock ->method ('_prepareProduct ' )->willReturn ([$ associatedProduct ]);
816+ $ associatedProduct ->method ('getTypeInstance ' )->willReturn ($ typeMock );
817+ }
818+ $ this ->product
819+ ->expects ($ this ->atLeastOnce ())
820+ ->method ('hasData ' )
821+ ->with ('_cache_instance_associated_products ' )
822+ ->willReturn (true );
823+ $ this ->product
824+ ->expects ($ this ->atLeastOnce ())
825+ ->method ('getData ' )
826+ ->with ('_cache_instance_associated_products ' )
827+ ->willReturn ($ associatedProducts );
828+ return $ associatedProducts ;
829+ }
732830}
0 commit comments