Skip to content

Commit 00db642

Browse files
committed
add auto tests
1 parent c50bb9a commit 00db642

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

app/code/Magento/Catalog/Api/CategoryListDeleteBySkuInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
interface CategoryListDeleteBySkuInterface
1414
{
1515
/**
16-
* delete by skus list
16+
* Delete by skus list
1717
*
1818
* @param int $categoryId
1919
* @param array $productSkuList

app/code/Magento/Catalog/Model/CategoryLinkRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function deleteBySkus($categoryId, array $productSkuList)
125125

126126
$productPositions = $category->getProductsPosition();
127127

128-
foreach ($products as $productSku => $productId) {
128+
foreach ($products as $productId) {
129129
if (isset($productPositions[$productId])) {
130130
unset($productPositions[$productId]);
131131
}

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

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

9+
use Magento\Framework\Exception\InputException;
10+
911
class CategoryLinkRepositoryTest extends \PHPUnit\Framework\TestCase
1012
{
1113
/**
@@ -28,14 +30,22 @@ class CategoryLinkRepositoryTest extends \PHPUnit\Framework\TestCase
2830
*/
2931
protected $productLinkMock;
3032

33+
protected $productResourceMock;
34+
3135
protected function setUp()
3236
{
37+
38+
$this->productResourceMock = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product::class)
39+
->disableOriginalConstructor()
40+
->setMethods(['getProductsIdsBySkus'])
41+
->getMock();
3342
$this->categoryRepositoryMock = $this->createMock(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
3443
$this->productRepositoryMock = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
3544
$this->productLinkMock = $this->createMock(\Magento\Catalog\Api\Data\CategoryProductLinkInterface::class);
3645
$this->model = new \Magento\Catalog\Model\CategoryLinkRepository(
3746
$this->categoryRepositoryMock,
38-
$this->productRepositoryMock
47+
$this->productRepositoryMock,
48+
$this->productResourceMock
3949
);
4050
}
4151

@@ -194,4 +204,66 @@ public function testDelete()
194204
$categoryMock->expects($this->once())->method('save');
195205
$this->assertTrue($this->model->delete($this->productLinkMock));
196206
}
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+
}
197269
}

0 commit comments

Comments
 (0)