@@ -764,77 +764,13 @@ public function testGroupCollectionCreatesAndSavesGroup()
764764 $ this ->productHelperMock ->method ('getAttributeSourceModelByInputType ' )->with ('text ' )->willReturn (null );
765765 $ this ->productHelperMock ->method ('getAttributeBackendModelByInputType ' )->with ('text ' )->willReturn (null );
766766
767- // Attribute model supporting the required setters
768- $ attributeModel = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Eav \Attribute::class)
769- ->disableOriginalConstructor ()
770- ->onlyMethods ([
771- 'getDefaultValueByInput ' ,
772- 'getBackendType ' ,
773- 'getFrontendClass ' ,
774- 'addData ' ,
775- 'save ' ,
776- 'setEntityTypeId ' ,
777- 'setIsUserDefined ' ,
778- 'getId '
779- ])->getMock ();
780- $ attributeModel ->method ('getDefaultValueByInput ' )->with ('text ' )->willReturn (null );
781- $ attributeModel ->method ('addData ' )->willReturnSelf ();
782- $ attributeModel ->method ('save ' )->willReturnSelf ();
783- $ attributeModel ->method ('setEntityTypeId ' )->willReturnSelf ();
784- $ attributeModel ->method ('setIsUserDefined ' )->willReturnSelf ();
785- $ attributeModel ->method ('getId ' )->willReturn (null );
786- $ localAttributeFactory = $ this ->getMockBuilder (AttributeFactory::class)
787- ->onlyMethods (['create ' ])
788- ->disableOriginalConstructor ()
789- ->getMock ();
790- $ localAttributeFactory ->method ('create ' )->willReturn ($ attributeModel );
791-
792- $ collectionMock = $ this ->getMockBuilder (AttributeGroupCollection::class)
793- ->disableOriginalConstructor ()
794- ->onlyMethods ([
795- 'setAttributeSetFilter ' ,
796- 'addFieldToFilter ' ,
797- 'setPageSize ' ,
798- 'load ' ,
799- 'getFirstItem '
800- ])->getMock ();
801- $ groupMock = $ this ->getMockBuilder (AttributeGroup::class)
802- ->disableOriginalConstructor ()
803- ->onlyMethods (['getId ' ,'save ' ])
804- ->getMock ();
805- $ groupMock ->expects ($ this ->exactly (2 ))->method ('getId ' )->willReturnOnConsecutiveCalls (null , 5 );
806- $ groupMock ->expects ($ this ->once ())->method ('save ' )->willReturnSelf ();
807-
808- $ collectionMock ->expects ($ this ->once ())->method ('setAttributeSetFilter ' )->with (5 )->willReturnSelf ();
809- $ collectionMock ->expects ($ this ->once ())
810- ->method ('addFieldToFilter ' )
811- ->with ('attribute_group_code ' , 'group_code ' )
812- ->willReturnSelf ();
813- $ collectionMock ->expects ($ this ->once ())->method ('setPageSize ' )->with (1 )->willReturnSelf ();
814- $ collectionMock ->expects ($ this ->once ())->method ('load ' )->willReturnSelf ();
815- $ collectionMock ->expects ($ this ->once ())->method ('getFirstItem ' )->willReturn ($ groupMock );
816-
817- $ this ->groupCollectionFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ collectionMock );
767+ $ localAttributeFactory = $ this ->createAttributeFactoryForGroupCollectionTest ();
768+ $ this ->prepareGroupCollectionFactoryForGroupCreation ();
818769
819770 $ this ->resultFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturn ($ this ->redirectMock );
820771 $ this ->redirectMock ->expects ($ this ->any ())->method ('setPath ' )->willReturnSelf ();
821772
822- $ controller = $ this ->objectManager ->getObject (Save::class, [
823- 'context ' => $ this ->contextMock ,
824- 'attributeLabelCache ' => $ this ->attributeLabelCacheMock ,
825- 'coreRegistry ' => $ this ->coreRegistryMock ,
826- 'resultPageFactory ' => $ this ->resultPageFactoryMock ,
827- 'buildFactory ' => $ this ->buildFactoryMock ,
828- 'filterManager ' => $ this ->filterManagerMock ,
829- 'productHelper ' => $ this ->productHelperMock ,
830- 'attributeFactory ' => $ localAttributeFactory ,
831- 'validatorFactory ' => $ this ->validatorFactoryMock ,
832- 'groupCollectionFactory ' => $ this ->groupCollectionFactoryMock ,
833- 'layoutFactory ' => $ this ->layoutFactoryMock ,
834- 'formDataSerializer ' => $ this ->formDataSerializerMock ,
835- 'presentation ' => $ this ->presentationMock ,
836- '_session ' => $ this ->sessionMock
837- ]);
773+ $ controller = $ this ->createControllerWithAttributeFactory ($ localAttributeFactory );
838774
839775 $ this ->assertInstanceOf (ResultRedirect::class, $ controller ->execute ());
840776 }
@@ -1053,4 +989,84 @@ public function testNewAttributeSetGenericExceptionAddsExceptionMessage()
1053989
1054990 $ this ->assertInstanceOf (ResultRedirect::class, $ this ->getModel ()->execute ());
1055991 }
992+
993+ private function createAttributeFactoryForGroupCollectionTest ()
994+ {
995+ $ attributeModel = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Eav \Attribute::class)
996+ ->disableOriginalConstructor ()
997+ ->onlyMethods ([
998+ 'getDefaultValueByInput ' ,
999+ 'getBackendType ' ,
1000+ 'getFrontendClass ' ,
1001+ 'addData ' ,
1002+ 'save ' ,
1003+ 'setEntityTypeId ' ,
1004+ 'setIsUserDefined ' ,
1005+ 'getId '
1006+ ])->getMock ();
1007+ $ attributeModel ->method ('getDefaultValueByInput ' )->with ('text ' )->willReturn (null );
1008+ $ attributeModel ->method ('addData ' )->willReturnSelf ();
1009+ $ attributeModel ->method ('save ' )->willReturnSelf ();
1010+ $ attributeModel ->method ('setEntityTypeId ' )->willReturnSelf ();
1011+ $ attributeModel ->method ('setIsUserDefined ' )->willReturnSelf ();
1012+ $ attributeModel ->method ('getId ' )->willReturn (null );
1013+
1014+ $ localAttributeFactory = $ this ->getMockBuilder (AttributeFactory::class)
1015+ ->onlyMethods (['create ' ])
1016+ ->disableOriginalConstructor ()
1017+ ->getMock ();
1018+ $ localAttributeFactory ->method ('create ' )->willReturn ($ attributeModel );
1019+
1020+ return $ localAttributeFactory ;
1021+ }
1022+
1023+ private function prepareGroupCollectionFactoryForGroupCreation ()
1024+ {
1025+ $ collectionMock = $ this ->getMockBuilder (AttributeGroupCollection::class)
1026+ ->disableOriginalConstructor ()
1027+ ->onlyMethods ([
1028+ 'setAttributeSetFilter ' ,
1029+ 'addFieldToFilter ' ,
1030+ 'setPageSize ' ,
1031+ 'load ' ,
1032+ 'getFirstItem '
1033+ ])->getMock ();
1034+ $ groupMock = $ this ->getMockBuilder (AttributeGroup::class)
1035+ ->disableOriginalConstructor ()
1036+ ->onlyMethods (['getId ' ,'save ' ])
1037+ ->getMock ();
1038+ $ groupMock ->expects ($ this ->exactly (2 ))->method ('getId ' )->willReturnOnConsecutiveCalls (null , 5 );
1039+ $ groupMock ->expects ($ this ->once ())->method ('save ' )->willReturnSelf ();
1040+
1041+ $ collectionMock ->expects ($ this ->once ())->method ('setAttributeSetFilter ' )->with (5 )->willReturnSelf ();
1042+ $ collectionMock ->expects ($ this ->once ())
1043+ ->method ('addFieldToFilter ' )
1044+ ->with ('attribute_group_code ' , 'group_code ' )
1045+ ->willReturnSelf ();
1046+ $ collectionMock ->expects ($ this ->once ())->method ('setPageSize ' )->with (1 )->willReturnSelf ();
1047+ $ collectionMock ->expects ($ this ->once ())->method ('load ' )->willReturnSelf ();
1048+ $ collectionMock ->expects ($ this ->once ())->method ('getFirstItem ' )->willReturn ($ groupMock );
1049+
1050+ $ this ->groupCollectionFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ collectionMock );
1051+ }
1052+
1053+ private function createControllerWithAttributeFactory ($ localAttributeFactory )
1054+ {
1055+ return $ this ->objectManager ->getObject (Save::class, [
1056+ 'context ' => $ this ->contextMock ,
1057+ 'attributeLabelCache ' => $ this ->attributeLabelCacheMock ,
1058+ 'coreRegistry ' => $ this ->coreRegistryMock ,
1059+ 'resultPageFactory ' => $ this ->resultPageFactoryMock ,
1060+ 'buildFactory ' => $ this ->buildFactoryMock ,
1061+ 'filterManager ' => $ this ->filterManagerMock ,
1062+ 'productHelper ' => $ this ->productHelperMock ,
1063+ 'attributeFactory ' => $ localAttributeFactory ,
1064+ 'validatorFactory ' => $ this ->validatorFactoryMock ,
1065+ 'groupCollectionFactory ' => $ this ->groupCollectionFactoryMock ,
1066+ 'layoutFactory ' => $ this ->layoutFactoryMock ,
1067+ 'formDataSerializer ' => $ this ->formDataSerializerMock ,
1068+ 'presentation ' => $ this ->presentationMock ,
1069+ '_session ' => $ this ->sessionMock
1070+ ]);
1071+ }
10561072}
0 commit comments