9 use \PHPUnit_Framework_MockObject_MockObject as MockObject;
21 private $resourceMock;
26 private $metadataPoolMock;
31 private $dbSelectMock;
36 private $connectionMock;
40 $this->resourceMock = $this->getMockBuilder(\
Magento\Framework\
App\ResourceConnection::class)
41 ->disableOriginalConstructor()
43 $this->metadataPoolMock = $this->getMockBuilder(\
Magento\Framework\EntityManager\MetadataPool::class)
44 ->disableOriginalConstructor()
48 $this->metadataPoolMock,
53 private function prepareAdapter()
55 $this->dbSelectMock = $this->getMockBuilder(\
Magento\Framework\DB\Select::class)
56 ->disableOriginalConstructor()
58 $this->connectionMock = $this->getMockBuilder(\
Magento\Framework\DB\Adapter\AdapterInterface::class)
59 ->getMockForAbstractClass();
60 $this->connectionMock->expects($this->any())->method(
'select')->willReturn($this->dbSelectMock);
61 $this->resourceMock->expects($this->any())
62 ->method(
'getConnection')
63 ->willReturn($this->connectionMock);
66 private function prepareMetadata()
68 $categoryLinkMetadata = $this->getMockBuilder(\
Magento\Framework\EntityManager\EntityMetadataInterface::class)
69 ->getMockForAbstractClass();
70 $categoryLinkMetadata->expects($this->any())->method(
'getEntityTable')->willReturn(
'category_link_table');
71 $categoryEntityMetadata = $this->getMockBuilder(\
Magento\Framework\EntityManager\EntityMetadataInterface::class)
72 ->getMockForAbstractClass();
73 $categoryEntityMetadata->expects($this->any())->method(
'getEntityTable')->willReturn(
'category_entity_table');
74 $this->metadataPoolMock->expects($this->any())->method(
'getMetadata')->willReturnMap(
76 [\
Magento\Catalog\Api\Data\CategoryLinkInterface::class, $categoryLinkMetadata],
77 [\
Magento\Catalog\Api\Data\CategoryInterface::class, $categoryEntityMetadata],
84 $this->prepareAdapter();
85 $this->prepareMetadata();
86 $product = $this->getMockBuilder(\
Magento\Catalog\Api\Data\ProductInterface::class)->getMockForAbstractClass();
87 $product->expects($this->any())->method(
'getId')->willReturn(1);
88 $this->connectionMock->expects($this->once())->method(
'fetchAll')->with($this->dbSelectMock)->willReturn(
90 [
'category_id' => 3,
'position' => 10],
91 [
'category_id' => 4,
'position' => 20],
97 [
'category_id' => 3,
'position' => 10],
98 [
'category_id' => 4,
'position' => 20],
100 $this->model->getCategoryLinks(
$product, [3, 4])
112 $this->prepareAdapter();
113 $this->prepareMetadata();
114 $product = $this->getMockBuilder(\
Magento\Catalog\Api\Data\ProductInterface::class)->getMockForAbstractClass();
115 $product->expects($this->any())->method(
'getId')->willReturn(1);
116 $this->connectionMock->expects($this->once())
118 ->with($this->dbSelectMock)
119 ->willReturn($dbCategoryLinks);
120 if (!empty($newCategoryLinks)) {
121 $this->connectionMock->expects($this->once())
123 ->with($this->dbSelectMock)
127 array_column($newCategoryLinks,
'category_id')
132 $actualResult = $this->model->saveCategoryLinks(
$product, $newCategoryLinks);
134 $this->assertEquals($affectedIds, $actualResult);
147 [
'category_id' => 3,
'position' => 10],
148 [
'category_id' => 4,
'position' => 20],
151 [
'category_id' => 3,
'position' => 10],
152 [
'category_id' => 4,
'position' => 20],
158 [
'category_id' => 4,
'position' => 30],
159 [
'category_id' => 5,
'position' => 40],
162 [
'category_id' => 3,
'position' => 10],
163 [
'category_id' => 4,
'position' => 20],
169 [
'category_id' => 6,
'position' => 30],
170 [
'category_id' => 3,
'position' => 40],
173 [
'category_id' => 3,
'position' => 10],
174 [
'category_id' => 4,
'position' => 20],
181 [
'category_id' => 3,
'position' => 10],
182 [
'category_id' => 4,
'position' => 20],
testSaveCategoryLinks($newCategoryLinks, $dbCategoryLinks, $affectedIds)
getCategoryLinksDataProvider()