1414use Magento \CatalogUrlRewrite \Model \Category \Plugin \Category \Move as CategoryMovePlugin ;
1515use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
1616use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
17+ use Magento \Store \Model \StoreManagerInterface ;
1718use PHPUnit \Framework \MockObject \MockObject ;
1819use PHPUnit \Framework \TestCase ;
1920
@@ -49,6 +50,11 @@ class MoveTest extends TestCase
4950 */
5051 private $ categoryFactory ;
5152
53+ /**
54+ * @var StoreManagerInterface|MockObject
55+ */
56+ private $ storeManagerMock ;
57+
5258 /**
5359 * @var CategoryMovePlugin
5460 */
@@ -59,28 +65,45 @@ protected function setUp(): void
5965 $ this ->objectManager = new ObjectManager ($ this );
6066 $ this ->categoryUrlPathGeneratorMock = $ this ->getMockBuilder (CategoryUrlPathGenerator::class)
6167 ->disableOriginalConstructor ()
62- ->setMethods (['getUrlPath ' ])
68+ ->onlyMethods (['getUrlPath ' ])
6369 ->getMock ();
6470 $ this ->childrenCategoriesProviderMock = $ this ->getMockBuilder (ChildrenCategoriesProvider::class)
6571 ->disableOriginalConstructor ()
66- ->setMethods (['getChildren ' ])
72+ ->onlyMethods (['getChildren ' ])
6773 ->getMock ();
6874 $ this ->categoryFactory = $ this ->getMockBuilder (CategoryFactory::class)
6975 ->disableOriginalConstructor ()
7076 ->getMock ();
7177 $ this ->subjectMock = $ this ->getMockBuilder (CategoryResourceModel::class)
7278 ->disableOriginalConstructor ()
79+ ->onlyMethods (['saveAttribute ' ])
7380 ->getMock ();
81+ $ this ->storeManagerMock = $ this ->getMockBuilder (StoreManagerInterface::class)
82+ ->disableOriginalConstructor ()
83+ ->getMockForAbstractClass ();
7484 $ this ->categoryMock = $ this ->getMockBuilder (Category::class)
7585 ->disableOriginalConstructor ()
76- ->setMethods (['getResource ' , 'setUrlPath ' , 'getStoreIds ' , 'getStoreId ' , 'setStoreId ' ])
86+ ->onlyMethods (
87+ [
88+ 'getResource ' ,
89+ 'getStoreIds ' ,
90+ 'getStoreId ' ,
91+ 'setStoreId ' ,
92+ 'getData ' ,
93+ 'getOrigData ' ,
94+ 'getId ' ,
95+ 'getUrlKey '
96+ ]
97+ )
98+ ->addMethods (['setUrlPath ' , 'unsUrlPath ' , 'setUrlKey ' ])
7799 ->getMock ();
78100 $ this ->plugin = $ this ->objectManager ->getObject (
79101 CategoryMovePlugin::class,
80102 [
81103 'categoryUrlPathGenerator ' => $ this ->categoryUrlPathGeneratorMock ,
82104 'childrenCategoriesProvider ' => $ this ->childrenCategoriesProviderMock ,
83- 'categoryFactory ' => $ this ->categoryFactory
105+ 'categoryFactory ' => $ this ->categoryFactory ,
106+ 'storeManager ' => $ this ->storeManagerMock
84107 ]
85108 );
86109 }
@@ -92,27 +115,41 @@ public function testAfterChangeParent()
92115 {
93116 $ urlPath = 'test/path ' ;
94117 $ storeIds = [0 , 1 ];
95- $ originalCategory = $ this ->getMockBuilder (Category::class)
96- ->disableOriginalConstructor ()
97- ->getMock ();
98- $ this ->categoryFactory ->method ('create ' )
99- ->willReturn ($ originalCategory );
100- $ this ->categoryMock ->method ('getResource ' )
101- ->willReturn ($ this ->subjectMock );
102- $ this ->categoryMock ->expects ($ this ->once ())
103- ->method ('getStoreIds ' )
104- ->willReturn ($ storeIds );
105- $ this ->childrenCategoriesProviderMock ->expects ($ this ->exactly (2 ))
106- ->method ('getChildren ' )
118+
119+ $ this ->storeManagerMock ->expects ($ this ->exactly (2 ))->method ('hasSingleStore ' )->willReturn (false );
120+ $ this ->categoryMock ->expects ($ this ->exactly (4 ))->method ('getStoreId ' )
121+ ->willReturnOnConsecutiveCalls (0 , 0 , 1 , 0 );
122+ $ this ->categoryMock ->expects ($ this ->once ())->method ('getStoreIds ' )->willReturn ($ storeIds );
123+ $ this ->categoryMock ->expects ($ this ->exactly (4 ))->method ('setStoreId ' )
124+ ->willReturnOnConsecutiveCalls (0 , 0 , 1 , 0 );
125+
126+ $ this ->categoryMock ->expects ($ this ->exactly (2 ))->method ('getData ' )
127+ ->willReturnOnConsecutiveCalls ('1/3/5 ' , '1/3/5 ' );
128+ $ this ->categoryMock ->expects ($ this ->exactly (2 ))->method ('getOrigData ' )
129+ ->willReturnOnConsecutiveCalls ('1/2/5 ' , '1/2/5 ' );
130+ $ this ->categoryMock ->expects ($ this ->exactly (6 ))->method ('unsUrlPath ' )->willReturnSelf ();
131+ $ this ->childrenCategoriesProviderMock ->expects ($ this ->exactly (4 ))->method ('getChildren ' )
107132 ->with ($ this ->categoryMock , true )
108- ->willReturn ([]);
109- $ this ->categoryUrlPathGeneratorMock ->expects ($ this ->exactly (2 ))
110- ->method ('getUrlPath ' )
111- ->with ($ this ->categoryMock )
112- ->willReturn ($ urlPath );
113- $ this ->categoryMock ->expects ($ this ->exactly (2 ))
114- ->method ('setUrlPath ' )
115- ->with ($ urlPath );
133+ ->willReturnOnConsecutiveCalls ([$ this ->categoryMock ], [$ this ->categoryMock ], [], []);
134+
135+ $ this ->categoryMock ->expects ($ this ->exactly (6 ))->method ('getResource ' )->willReturn ($ this ->subjectMock );
136+ $ this ->subjectMock ->expects ($ this ->exactly (6 ))->method ('saveAttribute ' )
137+ ->with ($ this ->categoryMock , 'url_path ' )->willReturnSelf ();
138+ $ this ->categoryMock ->expects ($ this ->exactly (2 ))->method ('getId ' )->willReturnSelf ();
139+
140+ $ originalCategory = $ this ->getMockBuilder (Category::class)->disableOriginalConstructor ()->getMock ();
141+ $ originalCategory ->expects ($ this ->exactly (2 ))->method ('getUrlKey ' )->willReturn ('url-key ' );
142+ $ originalCategory ->expects ($ this ->exactly (2 ))->method ('setStoreId ' )->willReturnSelf ();
143+ $ originalCategory ->expects ($ this ->exactly (2 ))->method ('load ' )->willReturnSelf ();
144+ $ this ->categoryFactory ->expects ($ this ->exactly (2 ))->method ('create ' )
145+ ->willReturn ($ originalCategory );
146+ $ this ->categoryMock ->expects ($ this ->exactly (2 ))->method ('setUrlKey ' )->with ('url-key ' )
147+ ->willReturnSelf ();
148+
149+ $ this ->categoryUrlPathGeneratorMock ->expects ($ this ->exactly (3 ))->method ('getUrlPath ' )
150+ ->with ($ this ->categoryMock )->willReturn ($ urlPath );
151+ $ this ->categoryMock ->expects ($ this ->exactly (3 ))->method ('setUrlPath ' )->with ($ urlPath );
152+
116153 $ this ->assertSame (
117154 $ this ->subjectMock ,
118155 $ this ->plugin ->afterChangeParent (
0 commit comments