Skip to content

Commit 5b3a131

Browse files
#25249: fix static
1 parent 00750f4 commit 5b3a131

File tree

1 file changed

+116
-57
lines changed

1 file changed

+116
-57
lines changed

app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkRepositoryTest.php

Lines changed: 116 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,82 @@
66

77
namespace Magento\Catalog\Test\Unit\Model;
88

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;
1016

17+
/**
18+
* Test for \Magento\Catalog\Model\CategoryLinkRepository
19+
* @SuppressWarnings(PHPMD.TooManyFields)
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
*/
1122
class CategoryLinkRepositoryTest extends \PHPUnit\Framework\TestCase
1223
{
1324
/**
14-
* @var \Magento\Catalog\Model\CategoryLinkRepository
25+
* @var CategoryLinkRepository
1526
*/
1627
protected $model;
1728

1829
/**
19-
* @var \PHPUnit_Framework_MockObject_MockObject
30+
* @var CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
2031
*/
2132
protected $categoryRepositoryMock;
2233

2334
/**
24-
* @var \PHPUnit_Framework_MockObject_MockObject
35+
* @var ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
2536
*/
2637
protected $productRepositoryMock;
2738

2839
/**
29-
* @var \PHPUnit_Framework_MockObject_MockObject
40+
* @var CategoryProductLinkInterface|\PHPUnit_Framework_MockObject_MockObject
3041
*/
3142
protected $productLinkMock;
3243

44+
/**
45+
* @var Product|\PHPUnit_Framework_MockObject_MockObject
46+
*/
3347
protected $productResourceMock;
3448

49+
/**
50+
* Initialize required data
51+
*/
3552
protected function setUp()
3653
{
37-
38-
$this->productResourceMock = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product::class)
54+
$this->productResourceMock = $this->getMockBuilder(Product::class)
3955
->disableOriginalConstructor()
4056
->setMethods(['getProductsIdsBySkus'])
4157
->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(
4662
$this->categoryRepositoryMock,
4763
$this->productRepositoryMock,
4864
$this->productResourceMock
4965
);
5066
}
5167

52-
public function testSave()
68+
/**
69+
* Assign a product to the category
70+
*
71+
* @return void
72+
*/
73+
public function testSave(): void
5374
{
5475
$categoryId = 42;
5576
$productId = 55;
5677
$productPosition = 1;
5778
$sku = 'testSku';
5879
$productPositions = [$productId => $productPosition];
5980
$categoryMock = $this->createPartialMock(
60-
\Magento\Catalog\Model\Category::class,
81+
Category::class,
6182
['getPostedProducts', 'getProductsPosition', 'setPostedProducts', 'save']
6283
);
63-
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
84+
$productMock = $this->createMock(ProductModel::class);
6485
$this->productLinkMock->expects($this->once())->method('getCategoryId')->willReturn($categoryId);
6586
$this->productLinkMock->expects($this->once())->method('getSku')->willReturn($sku);
6687
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
@@ -71,25 +92,27 @@ public function testSave()
7192
$this->productLinkMock->expects($this->once())->method('getPosition')->willReturn($productPosition);
7293
$categoryMock->expects($this->once())->method('setPostedProducts')->with($productPositions);
7394
$categoryMock->expects($this->once())->method('save');
95+
7496
$this->assertTrue($this->model->save($this->productLinkMock));
7597
}
7698

7799
/**
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
80103
*/
81-
public function testSaveWithCouldNotSaveException()
104+
public function testSaveWithCouldNotSaveException(): void
82105
{
83106
$categoryId = 42;
84107
$productId = 55;
85108
$productPosition = 1;
86109
$sku = 'testSku';
87110
$productPositions = [$productId => $productPosition];
88111
$categoryMock = $this->createPartialMock(
89-
\Magento\Catalog\Model\Category::class,
112+
Category::class,
90113
['getProductsPosition', 'setPostedProducts', 'save', 'getId']
91114
);
92-
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
115+
$productMock = $this->createMock(ProductModel::class);
93116
$this->productLinkMock->expects($this->once())->method('getCategoryId')->willReturn($categoryId);
94117
$this->productLinkMock->expects($this->once())->method('getSku')->willReturn($sku);
95118
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
@@ -101,20 +124,28 @@ public function testSaveWithCouldNotSaveException()
101124
$categoryMock->expects($this->once())->method('setPostedProducts')->with($productPositions);
102125
$categoryMock->expects($this->once())->method('getId')->willReturn($categoryId);
103126
$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);
104130
$this->model->save($this->productLinkMock);
105131
}
106132

107-
public function testDeleteByIds()
133+
/**
134+
* Remove the product assignment from the category
135+
*
136+
* @return void
137+
*/
138+
public function testDeleteByIds(): void
108139
{
109-
$categoryId = "42";
110-
$productSku = "testSku";
140+
$categoryId = 42;
141+
$productSku = 'testSku';
111142
$productId = 55;
112143
$productPositions = [55 => 1];
113144
$categoryMock = $this->createPartialMock(
114-
\Magento\Catalog\Model\Category::class,
145+
Category::class,
115146
['getProductsPosition', 'setPostedProducts', 'save', 'getId']
116147
);
117-
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
148+
$productMock = $this->createMock(ProductModel::class);
118149
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
119150
->willReturn($categoryMock);
120151
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
@@ -123,24 +154,26 @@ public function testDeleteByIds()
123154
$productMock->expects($this->once())->method('getId')->willReturn($productId);
124155
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
125156
$categoryMock->expects($this->once())->method('save');
157+
126158
$this->assertTrue($this->model->deleteByIds($categoryId, $productSku));
127159
}
128160

129161
/**
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
132165
*/
133-
public function testDeleteByIdsWithCouldNotSaveException()
166+
public function testDeleteByIdsWithCouldNotSaveException(): void
134167
{
135-
$categoryId = "42";
136-
$productSku = "testSku";
168+
$categoryId = 42;
169+
$productSku = 'testSku';
137170
$productId = 55;
138171
$productPositions = [55 => 1];
139172
$categoryMock = $this->createPartialMock(
140-
\Magento\Catalog\Model\Category::class,
173+
Category::class,
141174
['getProductsPosition', 'setPostedProducts', 'save', 'getId']
142175
);
143-
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
176+
$productMock = $this->createMock(ProductModel::class);
144177
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
145178
->willReturn($categoryMock);
146179
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
@@ -150,50 +183,61 @@ public function testDeleteByIdsWithCouldNotSaveException()
150183
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
151184
$categoryMock->expects($this->once())->method('getId')->willReturn($categoryId);
152185
$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);
153189
$this->model->deleteByIds($categoryId, $productSku);
154190
}
155191

156192
/**
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
159196
*/
160-
public function testDeleteWithInputException()
197+
public function testDeleteWithInputException(): void
161198
{
162-
$categoryId = "42";
163-
$productSku = "testSku";
199+
$categoryId = 42;
200+
$productSku = 'testSku';
164201
$productId = 60;
165202
$productPositions = [55 => 1];
166203
$this->productLinkMock->expects($this->once())->method('getCategoryId')->willReturn($categoryId);
167204
$this->productLinkMock->expects($this->once())->method('getSku')->willReturn($productSku);
168205
$categoryMock = $this->createPartialMock(
169-
\Magento\Catalog\Model\Category::class,
206+
Category::class,
170207
['getProductsPosition', 'setPostedProducts', 'save', 'getId']
171208
);
172-
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
209+
$productMock = $this->createMock(ProductModel::class);
173210
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
174211
->willReturn($categoryMock);
175212
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
176213
->willReturn($productMock);
177214
$categoryMock->expects($this->once())->method('getProductsPosition')->willReturn($productPositions);
178215
$productMock->expects($this->once())->method('getId')->willReturn($productId);
179-
180216
$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);
181220
$this->assertTrue($this->model->delete($this->productLinkMock));
182221
}
183222

184-
public function testDelete()
223+
/**
224+
* Delete the product assignment from the category
225+
*
226+
* @return void
227+
*/
228+
public function testDelete(): void
185229
{
186-
$categoryId = "42";
187-
$productSku = "testSku";
230+
$categoryId = 42;
231+
$productSku = 'testSku';
188232
$productId = 55;
189233
$productPositions = [55 => 1];
190234
$this->productLinkMock->expects($this->once())->method('getCategoryId')->willReturn($categoryId);
191235
$this->productLinkMock->expects($this->once())->method('getSku')->willReturn($productSku);
192236
$categoryMock = $this->createPartialMock(
193-
\Magento\Catalog\Model\Category::class,
237+
Category::class,
194238
['getProductsPosition', 'setPostedProducts', 'save', 'getId']
195239
);
196-
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
240+
$productMock = $this->createMock(ProductModel::class);
197241
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
198242
->willReturn($categoryMock);
199243
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
@@ -202,17 +246,23 @@ public function testDelete()
202246
$productMock->expects($this->once())->method('getId')->willReturn($productId);
203247
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
204248
$categoryMock->expects($this->once())->method('save');
249+
205250
$this->assertTrue($this->model->delete($this->productLinkMock));
206251
}
207252

208-
public function testDeleteBySkus()
253+
/**
254+
* Delete by products skus
255+
*
256+
* @return void
257+
*/
258+
public function testDeleteBySkus(): void
209259
{
210260
$categoryId = 42;
211-
$productSku = "testSku";
261+
$productSku = 'testSku';
212262
$productId = 55;
213263
$productPositions = [55 => 1];
214264
$categoryMock = $this->createPartialMock(
215-
\Magento\Catalog\Model\Category::class,
265+
Category::class,
216266
['getProductsPosition', 'setPostedProducts', 'save', 'getId']
217267
);
218268
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
@@ -222,38 +272,44 @@ public function testDeleteBySkus()
222272
$categoryMock->expects($this->once())->method('getProductsPosition')->willReturn($productPositions);
223273
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
224274
$categoryMock->expects($this->once())->method('save');
275+
225276
$this->assertTrue($this->model->deleteBySkus($categoryId, [$productSku]));
226277
}
227278

228279
/**
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
231283
*/
232-
public function testDeleteBySkusWithInputException()
284+
public function testDeleteBySkusWithInputException(): void
233285
{
234286
$categoryId = 42;
235-
$productSku = "testSku";
287+
$productSku = 'testSku';
236288
$categoryMock = $this->createPartialMock(
237-
\Magento\Catalog\Model\Category::class,
289+
Category::class,
238290
['getProductsPosition', 'setPostedProducts', 'save', 'getId']
239291
);
240292
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
241293
->willReturn($categoryMock);
294+
295+
$this->expectExceptionMessage('The category doesn\'t contain the specified products.');
296+
$this->expectException(\Magento\Framework\Exception\InputException::class);
242297
$this->model->deleteBySkus($categoryId, [$productSku]);
243298
}
244299

245300
/**
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
248304
*/
249-
public function testDeleteSkusIdsWithCouldNotSaveException()
305+
public function testDeleteSkusIdsWithCouldNotSaveException(): void
250306
{
251307
$categoryId = 42;
252-
$productSku = "testSku";
308+
$productSku = 'testSku';
253309
$productId = 55;
254310
$productPositions = [55 => 1];
255311
$categoryMock = $this->createPartialMock(
256-
\Magento\Catalog\Model\Category::class,
312+
Category::class,
257313
['getProductsPosition', 'setPostedProducts', 'save', 'getId']
258314
);
259315
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
@@ -264,6 +320,9 @@ public function testDeleteSkusIdsWithCouldNotSaveException()
264320
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
265321
$categoryMock->expects($this->once())->method('getId')->willReturn($categoryId);
266322
$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);
267326
$this->assertTrue($this->model->deleteBySkus($categoryId, [$productSku]));
268327
}
269328
}

0 commit comments

Comments
 (0)