6
6
7
7
namespace Magento \Catalog \Test \Unit \Model ;
8
8
9
- use Magento \Framework \Exception \InputException ;
9
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
10
+ use Magento \Catalog \Api \Data \CategoryProductLinkInterface ;
11
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
12
+ use Magento \Catalog \Model \CategoryLinkRepository ;
13
+ use Magento \Catalog \Model \ResourceModel \Product ;
14
+ use Magento \Catalog \Model \Category ;
15
+ use Magento \Catalog \Model \Product as ProductModel ;
10
16
17
+ /**
18
+ * Test for \Magento\Catalog\Model\CategoryLinkRepository
19
+ * @SuppressWarnings(PHPMD.TooManyFields)
20
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
+ */
11
22
class CategoryLinkRepositoryTest extends \PHPUnit \Framework \TestCase
12
23
{
13
24
/**
14
- * @var \Magento\Catalog\Model\ CategoryLinkRepository
25
+ * @var CategoryLinkRepository
15
26
*/
16
27
protected $ model ;
17
28
18
29
/**
19
- * @var \PHPUnit_Framework_MockObject_MockObject
30
+ * @var CategoryRepositoryInterface| \PHPUnit_Framework_MockObject_MockObject
20
31
*/
21
32
protected $ categoryRepositoryMock ;
22
33
23
34
/**
24
- * @var \PHPUnit_Framework_MockObject_MockObject
35
+ * @var ProductRepositoryInterface| \PHPUnit_Framework_MockObject_MockObject
25
36
*/
26
37
protected $ productRepositoryMock ;
27
38
28
39
/**
29
- * @var \PHPUnit_Framework_MockObject_MockObject
40
+ * @var CategoryProductLinkInterface| \PHPUnit_Framework_MockObject_MockObject
30
41
*/
31
42
protected $ productLinkMock ;
32
43
44
+ /**
45
+ * @var Product|\PHPUnit_Framework_MockObject_MockObject
46
+ */
33
47
protected $ productResourceMock ;
34
48
49
+ /**
50
+ * Initialize required data
51
+ */
35
52
protected function setUp ()
36
53
{
37
-
38
- $ this ->productResourceMock = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Product::class)
54
+ $ this ->productResourceMock = $ this ->getMockBuilder (Product::class)
39
55
->disableOriginalConstructor ()
40
56
->setMethods (['getProductsIdsBySkus ' ])
41
57
->getMock ();
42
- $ this ->categoryRepositoryMock = $ this ->createMock (\ Magento \ Catalog \ Api \ CategoryRepositoryInterface::class);
43
- $ this ->productRepositoryMock = $ this ->createMock (\ Magento \ Catalog \ Api \ ProductRepositoryInterface::class);
44
- $ this ->productLinkMock = $ this ->createMock (\ Magento \ Catalog \ Api \ Data \ CategoryProductLinkInterface::class);
45
- $ this ->model = new \ Magento \ Catalog \ Model \ CategoryLinkRepository (
58
+ $ this ->categoryRepositoryMock = $ this ->createMock (CategoryRepositoryInterface::class);
59
+ $ this ->productRepositoryMock = $ this ->createMock (ProductRepositoryInterface::class);
60
+ $ this ->productLinkMock = $ this ->createMock (CategoryProductLinkInterface::class);
61
+ $ this ->model = new CategoryLinkRepository (
46
62
$ this ->categoryRepositoryMock ,
47
63
$ this ->productRepositoryMock ,
48
64
$ this ->productResourceMock
49
65
);
50
66
}
51
67
52
- public function testSave ()
68
+ /**
69
+ * Assign a product to the category
70
+ *
71
+ * @return void
72
+ */
73
+ public function testSave (): void
53
74
{
54
75
$ categoryId = 42 ;
55
76
$ productId = 55 ;
56
77
$ productPosition = 1 ;
57
78
$ sku = 'testSku ' ;
58
79
$ productPositions = [$ productId => $ productPosition ];
59
80
$ categoryMock = $ this ->createPartialMock (
60
- \ Magento \ Catalog \ Model \ Category::class,
81
+ Category::class,
61
82
['getPostedProducts ' , 'getProductsPosition ' , 'setPostedProducts ' , 'save ' ]
62
83
);
63
- $ productMock = $ this ->createMock (\ Magento \ Catalog \ Model \Product ::class);
84
+ $ productMock = $ this ->createMock (ProductModel ::class);
64
85
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getCategoryId ' )->willReturn ($ categoryId );
65
86
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ sku );
66
87
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
@@ -71,25 +92,27 @@ public function testSave()
71
92
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getPosition ' )->willReturn ($ productPosition );
72
93
$ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ($ productPositions );
73
94
$ categoryMock ->expects ($ this ->once ())->method ('save ' );
95
+
74
96
$ this ->assertTrue ($ this ->model ->save ($ this ->productLinkMock ));
75
97
}
76
98
77
99
/**
78
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
79
- * @expectedExceptionMessage Could not save product "55" with position 1 to category 42
100
+ * Assign a product to the category with `CouldNotSaveException`
101
+ *
102
+ * @return void
80
103
*/
81
- public function testSaveWithCouldNotSaveException ()
104
+ public function testSaveWithCouldNotSaveException (): void
82
105
{
83
106
$ categoryId = 42 ;
84
107
$ productId = 55 ;
85
108
$ productPosition = 1 ;
86
109
$ sku = 'testSku ' ;
87
110
$ productPositions = [$ productId => $ productPosition ];
88
111
$ categoryMock = $ this ->createPartialMock (
89
- \ Magento \ Catalog \ Model \ Category::class,
112
+ Category::class,
90
113
['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
91
114
);
92
- $ productMock = $ this ->createMock (\ Magento \ Catalog \ Model \Product ::class);
115
+ $ productMock = $ this ->createMock (ProductModel ::class);
93
116
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getCategoryId ' )->willReturn ($ categoryId );
94
117
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ sku );
95
118
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
@@ -101,20 +124,28 @@ public function testSaveWithCouldNotSaveException()
101
124
$ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ($ productPositions );
102
125
$ categoryMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ categoryId );
103
126
$ categoryMock ->expects ($ this ->once ())->method ('save ' )->willThrowException (new \Exception ());
127
+
128
+ $ this ->expectExceptionMessage ('Could not save product "55" with position 1 to category 42 ' );
129
+ $ this ->expectException (\Magento \Framework \Exception \CouldNotSaveException::class);
104
130
$ this ->model ->save ($ this ->productLinkMock );
105
131
}
106
132
107
- public function testDeleteByIds ()
133
+ /**
134
+ * Remove the product assignment from the category
135
+ *
136
+ * @return void
137
+ */
138
+ public function testDeleteByIds (): void
108
139
{
109
- $ categoryId = " 42 " ;
110
- $ productSku = " testSku " ;
140
+ $ categoryId = 42 ;
141
+ $ productSku = ' testSku ' ;
111
142
$ productId = 55 ;
112
143
$ productPositions = [55 => 1 ];
113
144
$ categoryMock = $ this ->createPartialMock (
114
- \ Magento \ Catalog \ Model \ Category::class,
145
+ Category::class,
115
146
['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
116
147
);
117
- $ productMock = $ this ->createMock (\ Magento \ Catalog \ Model \Product ::class);
148
+ $ productMock = $ this ->createMock (ProductModel ::class);
118
149
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
119
150
->willReturn ($ categoryMock );
120
151
$ this ->productRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ productSku )
@@ -123,24 +154,26 @@ public function testDeleteByIds()
123
154
$ productMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productId );
124
155
$ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ([]);
125
156
$ categoryMock ->expects ($ this ->once ())->method ('save ' );
157
+
126
158
$ this ->assertTrue ($ this ->model ->deleteByIds ($ categoryId , $ productSku ));
127
159
}
128
160
129
161
/**
130
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
131
- * @expectedExceptionMessage Could not save product "55" with position 1 to category 42
162
+ * Delete the product assignment from the category with `CouldNotSaveException`
163
+ *
164
+ * @return void
132
165
*/
133
- public function testDeleteByIdsWithCouldNotSaveException ()
166
+ public function testDeleteByIdsWithCouldNotSaveException (): void
134
167
{
135
- $ categoryId = " 42 " ;
136
- $ productSku = " testSku " ;
168
+ $ categoryId = 42 ;
169
+ $ productSku = ' testSku ' ;
137
170
$ productId = 55 ;
138
171
$ productPositions = [55 => 1 ];
139
172
$ categoryMock = $ this ->createPartialMock (
140
- \ Magento \ Catalog \ Model \ Category::class,
173
+ Category::class,
141
174
['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
142
175
);
143
- $ productMock = $ this ->createMock (\ Magento \ Catalog \ Model \Product ::class);
176
+ $ productMock = $ this ->createMock (ProductModel ::class);
144
177
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
145
178
->willReturn ($ categoryMock );
146
179
$ this ->productRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ productSku )
@@ -150,50 +183,61 @@ public function testDeleteByIdsWithCouldNotSaveException()
150
183
$ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ([]);
151
184
$ categoryMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ categoryId );
152
185
$ categoryMock ->expects ($ this ->once ())->method ('save ' )->willThrowException (new \Exception ());
186
+
187
+ $ this ->expectExceptionMessage ('Could not save product "55" with position 1 to category 42 ' );
188
+ $ this ->expectException (\Magento \Framework \Exception \CouldNotSaveException::class);
153
189
$ this ->model ->deleteByIds ($ categoryId , $ productSku );
154
190
}
155
191
156
192
/**
157
- * @expectedException \Magento\Framework\Exception\InputException
158
- * @expectedExceptionMessage The category doesn't contain the specified product.
193
+ * Delete the product assignment from the category with `InputException`
194
+ *
195
+ * @return void
159
196
*/
160
- public function testDeleteWithInputException ()
197
+ public function testDeleteWithInputException (): void
161
198
{
162
- $ categoryId = " 42 " ;
163
- $ productSku = " testSku " ;
199
+ $ categoryId = 42 ;
200
+ $ productSku = ' testSku ' ;
164
201
$ productId = 60 ;
165
202
$ productPositions = [55 => 1 ];
166
203
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getCategoryId ' )->willReturn ($ categoryId );
167
204
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ productSku );
168
205
$ categoryMock = $ this ->createPartialMock (
169
- \ Magento \ Catalog \ Model \ Category::class,
206
+ Category::class,
170
207
['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
171
208
);
172
- $ productMock = $ this ->createMock (\ Magento \ Catalog \ Model \Product ::class);
209
+ $ productMock = $ this ->createMock (ProductModel ::class);
173
210
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
174
211
->willReturn ($ categoryMock );
175
212
$ this ->productRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ productSku )
176
213
->willReturn ($ productMock );
177
214
$ categoryMock ->expects ($ this ->once ())->method ('getProductsPosition ' )->willReturn ($ productPositions );
178
215
$ productMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productId );
179
-
180
216
$ categoryMock ->expects ($ this ->never ())->method ('save ' );
217
+
218
+ $ this ->expectExceptionMessage ('The category doesn \'t contain the specified product. ' );
219
+ $ this ->expectException (\Magento \Framework \Exception \InputException::class);
181
220
$ this ->assertTrue ($ this ->model ->delete ($ this ->productLinkMock ));
182
221
}
183
222
184
- public function testDelete ()
223
+ /**
224
+ * Delete the product assignment from the category
225
+ *
226
+ * @return void
227
+ */
228
+ public function testDelete (): void
185
229
{
186
- $ categoryId = " 42 " ;
187
- $ productSku = " testSku " ;
230
+ $ categoryId = 42 ;
231
+ $ productSku = ' testSku ' ;
188
232
$ productId = 55 ;
189
233
$ productPositions = [55 => 1 ];
190
234
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getCategoryId ' )->willReturn ($ categoryId );
191
235
$ this ->productLinkMock ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ productSku );
192
236
$ categoryMock = $ this ->createPartialMock (
193
- \ Magento \ Catalog \ Model \ Category::class,
237
+ Category::class,
194
238
['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
195
239
);
196
- $ productMock = $ this ->createMock (\ Magento \ Catalog \ Model \Product ::class);
240
+ $ productMock = $ this ->createMock (ProductModel ::class);
197
241
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
198
242
->willReturn ($ categoryMock );
199
243
$ this ->productRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ productSku )
@@ -202,17 +246,23 @@ public function testDelete()
202
246
$ productMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productId );
203
247
$ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ([]);
204
248
$ categoryMock ->expects ($ this ->once ())->method ('save ' );
249
+
205
250
$ this ->assertTrue ($ this ->model ->delete ($ this ->productLinkMock ));
206
251
}
207
252
208
- public function testDeleteBySkus ()
253
+ /**
254
+ * Delete by products skus
255
+ *
256
+ * @return void
257
+ */
258
+ public function testDeleteBySkus (): void
209
259
{
210
260
$ categoryId = 42 ;
211
- $ productSku = " testSku " ;
261
+ $ productSku = ' testSku ' ;
212
262
$ productId = 55 ;
213
263
$ productPositions = [55 => 1 ];
214
264
$ categoryMock = $ this ->createPartialMock (
215
- \ Magento \ Catalog \ Model \ Category::class,
265
+ Category::class,
216
266
['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
217
267
);
218
268
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
@@ -222,38 +272,44 @@ public function testDeleteBySkus()
222
272
$ categoryMock ->expects ($ this ->once ())->method ('getProductsPosition ' )->willReturn ($ productPositions );
223
273
$ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ([]);
224
274
$ categoryMock ->expects ($ this ->once ())->method ('save ' );
275
+
225
276
$ this ->assertTrue ($ this ->model ->deleteBySkus ($ categoryId , [$ productSku ]));
226
277
}
227
278
228
279
/**
229
- * @expectedException \Magento\Framework\Exception\InputException
230
- * @expectedExceptionMessage The category doesn't contain the specified products.
280
+ * Delete by products skus with `InputException`
281
+ *
282
+ * @return void
231
283
*/
232
- public function testDeleteBySkusWithInputException ()
284
+ public function testDeleteBySkusWithInputException (): void
233
285
{
234
286
$ categoryId = 42 ;
235
- $ productSku = " testSku " ;
287
+ $ productSku = ' testSku ' ;
236
288
$ categoryMock = $ this ->createPartialMock (
237
- \ Magento \ Catalog \ Model \ Category::class,
289
+ Category::class,
238
290
['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
239
291
);
240
292
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
241
293
->willReturn ($ categoryMock );
294
+
295
+ $ this ->expectExceptionMessage ('The category doesn \'t contain the specified products. ' );
296
+ $ this ->expectException (\Magento \Framework \Exception \InputException::class);
242
297
$ this ->model ->deleteBySkus ($ categoryId , [$ productSku ]);
243
298
}
244
299
245
300
/**
246
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
247
- * @expectedExceptionMessage Could not save products "testSku" to category 42
301
+ * Delete by products skus with `CouldNotSaveException`
302
+ *
303
+ * @return void
248
304
*/
249
- public function testDeleteSkusIdsWithCouldNotSaveException ()
305
+ public function testDeleteSkusIdsWithCouldNotSaveException (): void
250
306
{
251
307
$ categoryId = 42 ;
252
- $ productSku = " testSku " ;
308
+ $ productSku = ' testSku ' ;
253
309
$ productId = 55 ;
254
310
$ productPositions = [55 => 1 ];
255
311
$ categoryMock = $ this ->createPartialMock (
256
- \ Magento \ Catalog \ Model \ Category::class,
312
+ Category::class,
257
313
['getProductsPosition ' , 'setPostedProducts ' , 'save ' , 'getId ' ]
258
314
);
259
315
$ this ->categoryRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ categoryId )
@@ -264,6 +320,9 @@ public function testDeleteSkusIdsWithCouldNotSaveException()
264
320
$ categoryMock ->expects ($ this ->once ())->method ('setPostedProducts ' )->with ([]);
265
321
$ categoryMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ categoryId );
266
322
$ categoryMock ->expects ($ this ->once ())->method ('save ' )->willThrowException (new \Exception ());
323
+
324
+ $ this ->expectExceptionMessage ('Could not save products "testSku" to category 42 ' );
325
+ $ this ->expectException (\Magento \Framework \Exception \CouldNotSaveException::class);
267
326
$ this ->assertTrue ($ this ->model ->deleteBySkus ($ categoryId , [$ productSku ]));
268
327
}
269
328
}
0 commit comments