14
14
use Magento \CatalogUrlRewrite \Model \Category \Plugin \Category \Move as CategoryMovePlugin ;
15
15
use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
16
16
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
17
+ use Magento \Store \Model \StoreManagerInterface ;
17
18
use PHPUnit \Framework \MockObject \MockObject ;
18
19
use PHPUnit \Framework \TestCase ;
19
20
@@ -49,6 +50,11 @@ class MoveTest extends TestCase
49
50
*/
50
51
private $ categoryFactory ;
51
52
53
+ /**
54
+ * @var StoreManagerInterface|MockObject
55
+ */
56
+ private $ storeManagerMock ;
57
+
52
58
/**
53
59
* @var CategoryMovePlugin
54
60
*/
@@ -59,28 +65,45 @@ protected function setUp(): void
59
65
$ this ->objectManager = new ObjectManager ($ this );
60
66
$ this ->categoryUrlPathGeneratorMock = $ this ->getMockBuilder (CategoryUrlPathGenerator::class)
61
67
->disableOriginalConstructor ()
62
- ->setMethods (['getUrlPath ' ])
68
+ ->onlyMethods (['getUrlPath ' ])
63
69
->getMock ();
64
70
$ this ->childrenCategoriesProviderMock = $ this ->getMockBuilder (ChildrenCategoriesProvider::class)
65
71
->disableOriginalConstructor ()
66
- ->setMethods (['getChildren ' ])
72
+ ->onlyMethods (['getChildren ' ])
67
73
->getMock ();
68
74
$ this ->categoryFactory = $ this ->getMockBuilder (CategoryFactory::class)
69
75
->disableOriginalConstructor ()
70
76
->getMock ();
71
77
$ this ->subjectMock = $ this ->getMockBuilder (CategoryResourceModel::class)
72
78
->disableOriginalConstructor ()
79
+ ->onlyMethods (['saveAttribute ' ])
73
80
->getMock ();
81
+ $ this ->storeManagerMock = $ this ->getMockBuilder (StoreManagerInterface::class)
82
+ ->disableOriginalConstructor ()
83
+ ->getMockForAbstractClass ();
74
84
$ this ->categoryMock = $ this ->getMockBuilder (Category::class)
75
85
->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 ' ])
77
99
->getMock ();
78
100
$ this ->plugin = $ this ->objectManager ->getObject (
79
101
CategoryMovePlugin::class,
80
102
[
81
103
'categoryUrlPathGenerator ' => $ this ->categoryUrlPathGeneratorMock ,
82
104
'childrenCategoriesProvider ' => $ this ->childrenCategoriesProviderMock ,
83
- 'categoryFactory ' => $ this ->categoryFactory
105
+ 'categoryFactory ' => $ this ->categoryFactory ,
106
+ 'storeManager ' => $ this ->storeManagerMock
84
107
]
85
108
);
86
109
}
@@ -92,27 +115,41 @@ public function testAfterChangeParent()
92
115
{
93
116
$ urlPath = 'test/path ' ;
94
117
$ 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 ' )
107
132
->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
+
116
153
$ this ->assertSame (
117
154
$ this ->subjectMock ,
118
155
$ this ->plugin ->afterChangeParent (
0 commit comments