Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryLinkTest.php
Go to the documentation of this file.
1 <?php
7 
9 use \PHPUnit_Framework_MockObject_MockObject as MockObject;
10 
11 class CategoryLinkTest extends \PHPUnit\Framework\TestCase
12 {
16  private $model;
17 
21  private $resourceMock;
22 
26  private $metadataPoolMock;
27 
31  private $dbSelectMock;
32 
36  private $connectionMock;
37 
38  protected function setUp()
39  {
40  $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
41  ->disableOriginalConstructor()
42  ->getMock();
43  $this->metadataPoolMock = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46 
47  $this->model = new CategoryLink(
48  $this->metadataPoolMock,
49  $this->resourceMock
50  );
51  }
52 
53  private function prepareAdapter()
54  {
55  $this->dbSelectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
56  ->disableOriginalConstructor()
57  ->getMock();
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);
64  }
65 
66  private function prepareMetadata()
67  {
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(
75  [
76  [\Magento\Catalog\Api\Data\CategoryLinkInterface::class, $categoryLinkMetadata],
77  [\Magento\Catalog\Api\Data\CategoryInterface::class, $categoryEntityMetadata],
78  ]
79  );
80  }
81 
82  public function testGetCategoryLinks()
83  {
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(
89  [
90  ['category_id' => 3, 'position' => 10],
91  ['category_id' => 4, 'position' => 20],
92  ]
93  );
94 
95  $this->assertEquals(
96  [
97  ['category_id' => 3, 'position' => 10],
98  ['category_id' => 4, 'position' => 20],
99  ],
100  $this->model->getCategoryLinks($product, [3, 4])
101  );
102  }
103 
110  public function testSaveCategoryLinks($newCategoryLinks, $dbCategoryLinks, $affectedIds)
111  {
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())
117  ->method('fetchAll')
118  ->with($this->dbSelectMock)
119  ->willReturn($dbCategoryLinks);
120  if (!empty($newCategoryLinks)) {
121  $this->connectionMock->expects($this->once())
122  ->method('fetchCol')
123  ->with($this->dbSelectMock)
124  ->willReturn(
125  array_intersect(
126  [3, 4, 5], // valid category_ids
127  array_column($newCategoryLinks, 'category_id')
128  )
129  );
130  }
131 
132  $actualResult = $this->model->saveCategoryLinks($product, $newCategoryLinks);
133  sort($actualResult);
134  $this->assertEquals($affectedIds, $actualResult);
135  }
136 
143  {
144  return [
145  [
146  [
147  ['category_id' => 3, 'position' => 10],
148  ['category_id' => 4, 'position' => 20],
149  ],
150  [
151  ['category_id' => 3, 'position' => 10],
152  ['category_id' => 4, 'position' => 20],
153  ],
154  [], // Nothing to update - data not changed
155  ],
156  [
157  [
158  ['category_id' => 4, 'position' => 30],
159  ['category_id' => 5, 'position' => 40],
160  ],
161  [
162  ['category_id' => 3, 'position' => 10],
163  ['category_id' => 4, 'position' => 20],
164  ],
165  [3, 4, 5], // 4 - updated position, 5 - added, 3 - deleted
166  ],
167  [
168  [
169  ['category_id' => 6, 'position' => 30], //6 - not valid category,
170  ['category_id' => 3, 'position' => 40],
171  ],
172  [
173  ['category_id' => 3, 'position' => 10],
174  ['category_id' => 4, 'position' => 20],
175  ],
176  [3, 4], // 3 - updated position, 4 - deleted
177  ],
178  [
179  [],
180  [
181  ['category_id' => 3, 'position' => 10],
182  ['category_id' => 4, 'position' => 20],
183  ],
184  [3, 4], // 3, 4 - deleted
185  ],
186  ];
187  }
188 }