7
7
8
8
namespace Magento \CatalogUrlRewrite \Test \Unit \Model \Category \Plugin \Store ;
9
9
10
- use Magento \Catalog \Model \ResourceModel \Category \Collection as CategoryCollection ;
11
10
use Magento \Catalog \Model \Category ;
12
11
use Magento \Catalog \Model \CategoryFactory ;
13
12
use Magento \Catalog \Model \Product ;
14
13
use Magento \Catalog \Model \ProductFactory ;
14
+ use Magento \Catalog \Model \ResourceModel \Category \Collection as CategoryCollection ;
15
15
use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
16
16
use Magento \CatalogUrlRewrite \Model \Category \Plugin \Store \View as StoreViewPlugin ;
17
17
use Magento \CatalogUrlRewrite \Model \CategoryUrlRewriteGenerator ;
18
18
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
19
19
use Magento \Framework \Model \AbstractModel ;
20
- use Magento \Framework \ TestFramework \ Unit \ Helper \ ObjectManager ;
21
- use Magento \Store \Model \ResourceModel \ Store ;
20
+ use Magento \Store \ Model \ ResourceModel \ Store as StoreResourceModel ;
21
+ use Magento \Store \Model \Store ;
22
22
use Magento \UrlRewrite \Model \UrlPersistInterface ;
23
23
use PHPUnit \Framework \MockObject \MockObject ;
24
24
use PHPUnit \Framework \TestCase ;
28
28
*/
29
29
class ViewTest extends TestCase
30
30
{
31
- /**
32
- * @var ObjectManager
33
- */
34
- private $ objectManager ;
35
-
36
31
/**
37
32
* @var StoreViewPlugin
38
33
*/
@@ -44,7 +39,7 @@ class ViewTest extends TestCase
44
39
private $ abstractModelMock ;
45
40
46
41
/**
47
- * @var Store |MockObject
42
+ * @var StoreResourceModel |MockObject
48
43
*/
49
44
private $ subjectMock ;
50
45
@@ -93,12 +88,11 @@ class ViewTest extends TestCase
93
88
*/
94
89
protected function setUp (): void
95
90
{
96
- $ this ->objectManager = new ObjectManager ($ this );
97
91
$ this ->abstractModelMock = $ this ->getMockBuilder (AbstractModel::class)
98
92
->disableOriginalConstructor ()
99
93
->setMethods (['isObjectNew ' ])
100
94
->getMockForAbstractClass ();
101
- $ this ->subjectMock = $ this ->getMockBuilder (Store ::class)
95
+ $ this ->subjectMock = $ this ->getMockBuilder (StoreResourceModel ::class)
102
96
->disableOriginalConstructor ()
103
97
->getMock ();
104
98
$ this ->urlPersistMock = $ this ->getMockBuilder (UrlPersistInterface::class)
@@ -131,15 +125,12 @@ protected function setUp(): void
131
125
->disableOriginalConstructor ()
132
126
->setMethods (['getCollection ' ])
133
127
->getMock ();
134
- $ this ->plugin = $ this ->objectManager ->getObject (
135
- StoreViewPlugin::class,
136
- [
137
- 'urlPersist ' => $ this ->urlPersistMock ,
138
- 'categoryFactory ' => $ this ->categoryFactoryMock ,
139
- 'productFactory ' => $ this ->productFactoryMock ,
140
- 'categoryUrlRewriteGenerator ' => $ this ->categoryUrlRewriteGeneratorMock ,
141
- 'productUrlRewriteGenerator ' => $ this ->productUrlRewriteGeneratorMock
142
- ]
128
+ $ this ->plugin = new StoreViewPlugin (
129
+ $ this ->urlPersistMock ,
130
+ $ this ->categoryFactoryMock ,
131
+ $ this ->productFactoryMock ,
132
+ $ this ->categoryUrlRewriteGeneratorMock ,
133
+ $ this ->productUrlRewriteGeneratorMock
143
134
);
144
135
}
145
136
@@ -150,13 +141,19 @@ protected function setUp(): void
150
141
*/
151
142
public function testAfterSave (): void
152
143
{
153
- $ origStoreMock = $ this ->getMockBuilder (\ Magento \ Store \ Model \ Store::class)
144
+ $ origStoreMock = $ this ->getMockBuilder (Store::class)
154
145
->disableOriginalConstructor ()
155
146
->getMock ();
156
147
$ reflectionStore = new \ReflectionClass ($ this ->plugin );
157
148
$ origStore = $ reflectionStore ->getProperty ('origStore ' );
158
149
$ origStore ->setAccessible (true );
159
150
$ origStore ->setValue ($ this ->plugin , $ origStoreMock );
151
+
152
+ $ origStoreMock ->expects ($ this ->atLeastOnce ())
153
+ ->method ('getData ' )
154
+ ->with ('group_id ' )
155
+ ->willReturn ('1 ' );
156
+
160
157
$ origStoreMock ->expects ($ this ->atLeastOnce ())
161
158
->method ('isObjectNew ' )
162
159
->willReturn (true );
@@ -203,7 +200,54 @@ public function testAfterSave(): void
203
200
204
201
$ this ->assertSame (
205
202
$ this ->subjectMock ,
206
- $ this ->plugin ->afterSave ($ this ->subjectMock , $ this ->subjectMock , $ this ->abstractModelMock )
203
+ $ this ->plugin ->afterSave ($ this ->subjectMock , $ this ->subjectMock )
204
+ );
205
+ }
206
+
207
+ public function testAfterSaveWhenNoGroupId ()
208
+ {
209
+ $ origStoreMock = $ this ->getMockBuilder (Store::class)
210
+ ->disableOriginalConstructor ()
211
+ ->getMock ();
212
+ $ reflectionStore = new \ReflectionClass ($ this ->plugin );
213
+ $ origStore = $ reflectionStore ->getProperty ('origStore ' );
214
+ $ origStore ->setAccessible (true );
215
+ $ origStore ->setValue ($ this ->plugin , $ origStoreMock );
216
+
217
+ $ origStoreMock ->expects ($ this ->atLeastOnce ())
218
+ ->method ('getData ' )
219
+ ->with ('group_id ' )
220
+ ->willReturn (null );
221
+
222
+ $ origStoreMock ->expects ($ this ->any ())
223
+ ->method ('isObjectNew ' )
224
+ ->willReturn (true );
225
+
226
+ $ this ->abstractModelMock ->expects ($ this ->any ())
227
+ ->method ('isObjectNew ' )
228
+ ->willReturn (true );
229
+ $ categoryCollection = $ this ->getMockBuilder (CategoryCollection::class)
230
+ ->disableOriginalConstructor ()
231
+ ->setMethods (['getIterator ' ])
232
+ ->getMock ();
233
+ $ categoryCollection ->expects ($ this ->any ())
234
+ ->method ('getIterator ' )
235
+ ->willReturn (new \ArrayIterator ([]));
236
+ $ this ->categoryMock ->expects ($ this ->never ())
237
+ ->method ('getCategories ' );
238
+ $ this ->categoryFactoryMock ->expects ($ this ->never ())->method ('create ' );
239
+ $ this ->productFactoryMock ->expects ($ this ->never ())->method ('create ' );
240
+ $ this ->productMock ->expects ($ this ->never ())->method ('getCollection ' );
241
+ $ this ->productCollectionMock ->expects ($ this ->never ())->method ('addCategoryIds ' );
242
+ $ this ->productCollectionMock ->expects ($ this ->never ())->method ('addAttributeToSelect ' );
243
+ $ this ->productCollectionMock ->expects ($ this ->never ())->method ('addStoreFilter ' );
244
+
245
+ $ this ->productCollectionMock ->expects ($ this ->never ())->method ('getIterator ' );
246
+ $ this ->productUrlRewriteGeneratorMock ->expects ($ this ->never ())->method ('generate ' );
247
+
248
+ $ this ->assertSame (
249
+ $ this ->subjectMock ,
250
+ $ this ->plugin ->afterSave ($ this ->subjectMock , $ this ->subjectMock )
207
251
);
208
252
}
209
253
0 commit comments