6
6
7
7
namespace Magento \Catalog \Test \Unit \Model ;
8
8
9
+ use Magento \Framework \Exception \InputException ;
10
+
9
11
class CategoryLinkRepositoryTest extends \PHPUnit \Framework \TestCase
10
12
{
11
13
/**
@@ -28,14 +30,22 @@ class CategoryLinkRepositoryTest extends \PHPUnit\Framework\TestCase
28
30
*/
29
31
protected $ productLinkMock ;
30
32
33
+ protected $ productResourceMock ;
34
+
31
35
protected function setUp ()
32
36
{
37
+
38
+ $ this ->productResourceMock = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Product::class)
39
+ ->disableOriginalConstructor ()
40
+ ->setMethods (['getProductsIdsBySkus ' ])
41
+ ->getMock ();
33
42
$ this ->categoryRepositoryMock = $ this ->createMock (\Magento \Catalog \Api \CategoryRepositoryInterface::class);
34
43
$ this ->productRepositoryMock = $ this ->createMock (\Magento \Catalog \Api \ProductRepositoryInterface::class);
35
44
$ this ->productLinkMock = $ this ->createMock (\Magento \Catalog \Api \Data \CategoryProductLinkInterface::class);
36
45
$ this ->model = new \Magento \Catalog \Model \CategoryLinkRepository (
37
46
$ this ->categoryRepositoryMock ,
38
- $ this ->productRepositoryMock
47
+ $ this ->productRepositoryMock ,
48
+ $ this ->productResourceMock
39
49
);
40
50
}
41
51
@@ -194,4 +204,66 @@ public function testDelete()
194
204
$ categoryMock ->expects ($ this ->once ())->method ('save ' );
195
205
$ this ->assertTrue ($ this ->model ->delete ($ this ->productLinkMock ));
196
206
}
207
+
208
+ public function testDeleteBySkus ()
209
+ {
210
+ $ categoryId = "42 " ;
211
+ $ productSku = "testSku " ;
212
+ $ productId = 55 ;
213
+ $ productPositions = [55 => 1 ];
214
+ $ categoryMock = $ this ->createPartialMock (
215
+ \Magento \Catalog \Model \Category::class,
216
+ ['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
217
+ );
218
+ $ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
219
+ ->willReturn ($ categoryMock );
220
+ $ this ->productResourceMock ->expects ($ this ->once ())->method ('getProductsIdsBySkus ' )
221
+ ->willReturn (['testSku ' => $ productId ]);
222
+ $ categoryMock ->expects ($ this ->once ())->method ('getProductsPosition ' )->willReturn ($ productPositions );
223
+ $ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ([]);
224
+ $ categoryMock ->expects ($ this ->once ())->method ('save ' );
225
+ $ this ->assertTrue ($ this ->model ->deleteBySkus ($ categoryId , [$ productSku ]));
226
+ }
227
+
228
+ /**
229
+ * @expectedException \Magento\Framework\Exception\InputException
230
+ * @expectedExceptionMessage The category doesn't contain the specified products.
231
+ */
232
+ public function testDeleteBySkusWithInputException ()
233
+ {
234
+ $ categoryId = "42 " ;
235
+ $ productSku = "testSku " ;
236
+ $ categoryMock = $ this ->createPartialMock (
237
+ \Magento \Catalog \Model \Category::class,
238
+ ['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
239
+ );
240
+ $ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
241
+ ->willReturn ($ categoryMock );
242
+ $ this ->model ->deleteBySkus ($ categoryId , [$ productSku ]);
243
+ }
244
+
245
+ /**
246
+ * @expectedException \Magento\Framework\Exception\CouldNotSaveException
247
+ * @expectedExceptionMessage Could not save products "testSku" to category 42
248
+ */
249
+ public function testDeleteSkusIdsWithCouldNotSaveException ()
250
+ {
251
+ $ categoryId = "42 " ;
252
+ $ productSku = "testSku " ;
253
+ $ productId = 55 ;
254
+ $ productPositions = [55 => 1 ];
255
+ $ categoryMock = $ this ->createPartialMock (
256
+ \Magento \Catalog \Model \Category::class,
257
+ ['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
258
+ );
259
+ $ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
260
+ ->willReturn ($ categoryMock );
261
+ $ this ->productResourceMock ->expects ($ this ->once ())->method ('getProductsIdsBySkus ' )
262
+ ->willReturn (['testSku ' => $ productId ]);
263
+ $ categoryMock ->expects ($ this ->once ())->method ('getProductsPosition ' )->willReturn ($ productPositions );
264
+ $ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ([]);
265
+ $ categoryMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ categoryId );
266
+ $ categoryMock ->expects ($ this ->once ())->method ('save ' )->willThrowException (new \Exception ());
267
+ $ this ->assertTrue ($ this ->model ->deleteBySkus ($ categoryId , [$ productSku ]));
268
+ }
197
269
}
0 commit comments