Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PricePersistenceTest.php
Go to the documentation of this file.
1 <?php
8 
12 class PricePersistenceTest extends \PHPUnit\Framework\TestCase
13 {
17  private $attributeResource;
18 
22  private $attributeRepository;
23 
27  private $productAttribute;
28 
32  private $productIdLocator;
33 
37  private $connection;
38 
42  private $metadataPool;
43 
47  private $model;
48 
54  protected function setUp()
55  {
56  $this->attributeResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Attribute::class)
57  ->disableOriginalConstructor()->getMock();
58  $this->attributeRepository = $this->getMockBuilder(
59  \Magento\Catalog\Api\ProductAttributeRepositoryInterface::class
60  )
61  ->disableOriginalConstructor()
62  ->getMockForAbstractClass();
63  $this->productIdLocator = $this->getMockBuilder(\Magento\Catalog\Model\ProductIdLocatorInterface::class)
64  ->disableOriginalConstructor()->getMockForAbstractClass();
65  $this->metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
66  ->disableOriginalConstructor()
67  ->setMethods(['getLinkField', 'getMetadata'])
68  ->getMock();
69  $this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
70  ->disableOriginalConstructor()->getMockForAbstractClass();
71  $this->productAttribute = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductAttributeInterface::class)
72  ->disableOriginalConstructor()->getMockForAbstractClass();
73 
74  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
75  $this->model = $objectManager->getObject(
76  \Magento\Catalog\Model\Product\Price\PricePersistence::class,
77  [
78  'attributeResource' => $this->attributeResource,
79  'attributeRepository' => $this->attributeRepository,
80  'productIdLocator' => $this->productIdLocator,
81  'metadataPool' => $this->metadataPool,
82  ]
83  );
84  }
85 
91  public function testGet()
92  {
93  $attributeId = 5;
94  $skus = ['sku_1', 'sku_2'];
95  $idsBySku = [
96  'sku_1' => [
97  1 => \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
98  ],
99  'sku_2' => [
100  2 => \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL
101  ]
102  ];
103  $select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
104  ->disableOriginalConstructor()->getMock();
105  $this->productIdLocator
106  ->expects($this->once())
107  ->method('retrieveProductIdsBySkus')->with($skus)
108  ->willReturn($idsBySku);
109  $this->attributeResource->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->connection);
110  $this->connection->expects($this->once())->method('select')->willReturn($select);
111  $this->attributeResource
112  ->expects($this->once())
113  ->method('getTable')
114  ->with('catalog_product_entity_decimal')
115  ->willReturn('catalog_product_entity_decimal');
116  $select->expects($this->once())->method('from')->with('catalog_product_entity_decimal')->willReturnSelf();
117  $this->attributeRepository->expects($this->once())->method('get')->willReturn($this->productAttribute);
118  $this->productAttribute->expects($this->once())->method('getAttributeId')->willReturn($attributeId);
119  $select
120  ->expects($this->atLeastOnce())
121  ->method('where')
122  ->withConsecutive(['row_id IN (?)', [1, 2]], ['attribute_id = ?', $attributeId])
123  ->willReturnSelf();
124  $this->metadataPool->expects($this->atLeastOnce())->method('getMetadata')->willReturnSelf();
125  $this->metadataPool->expects($this->atLeastOnce())->method('getLinkField')->willReturn('row_id');
126  $this->model->get($skus);
127  }
128 
134  public function testUpdate()
135  {
136  $attributeId = 5;
137  $prices = [
138  [
139  'store_id' => 1,
140  'row_id' => 1,
141  'value' => 15
142  ]
143  ];
144  $this->attributeRepository->expects($this->once())->method('get')->willReturn($this->productAttribute);
145  $this->productAttribute->expects($this->once())->method('getAttributeId')->willReturn($attributeId);
146  $this->attributeResource->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->connection);
147  $this->connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
148  $this->attributeResource
149  ->expects($this->once())
150  ->method('getTable')
151  ->with('catalog_product_entity_decimal')
152  ->willReturn('catalog_product_entity_decimal');
153  $this->connection
154  ->expects($this->once())
155  ->method('insertOnDuplicate')
156  ->with(
157  'catalog_product_entity_decimal',
158  [
159  [
160  'store_id' => 1,
161  'row_id' => 1,
162  'value' => 15,
163  'attribute_id' => 5,
164  ]
165  ],
166  ['value']
167  )
168  ->willReturnSelf();
169  $this->connection->expects($this->once())->method('commit')->willReturnSelf();
170  $this->model->update($prices);
171  }
172 
179  public function testUpdateWithException()
180  {
181  $attributeId = 5;
182  $prices = [
183  [
184  'store_id' => 1,
185  'row_id' => 1,
186  'value' => 15
187  ]
188  ];
189  $this->attributeRepository->expects($this->once())->method('get')->willReturn($this->productAttribute);
190  $this->productAttribute->expects($this->once())->method('getAttributeId')->willReturn($attributeId);
191  $this->attributeResource->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->connection);
192  $this->connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
193  $this->attributeResource
194  ->expects($this->once())
195  ->method('getTable')
196  ->with('catalog_product_entity_decimal')
197  ->willReturn('catalog_product_entity_decimal');
198  $this->connection
199  ->expects($this->once())
200  ->method('insertOnDuplicate')
201  ->with(
202  'catalog_product_entity_decimal',
203  [
204  [
205  'store_id' => 1,
206  'row_id' => 1,
207  'value' => 15,
208  'attribute_id' => 5,
209  ]
210  ],
211  ['value']
212  )
213  ->willReturnSelf();
214  $this->connection->expects($this->once())->method('commit')->willThrowException(new \Exception());
215  $this->connection->expects($this->once())->method('rollback')->willReturnSelf();
216  $this->model->update($prices);
217  }
218 
224  public function testDelete()
225  {
226  $attributeId = 5;
227  $skus = ['sku_1', 'sku_2'];
228  $idsBySku = [
229  'sku_1' => [
230  1 => \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
231  ],
232  'sku_2' => [
233  2 => \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL
234  ]
235  ];
236  $this->productIdLocator
237  ->expects($this->once())
238  ->method('retrieveProductIdsBySkus')->with($skus)
239  ->willReturn($idsBySku);
240  $this->attributeRepository->expects($this->once())->method('get')->willReturn($this->productAttribute);
241  $this->productAttribute->expects($this->once())->method('getAttributeId')->willReturn($attributeId);
242  $this->attributeResource->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->connection);
243  $this->connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
244  $this->attributeResource
245  ->expects($this->once())
246  ->method('getTable')
247  ->with('catalog_product_entity_decimal')
248  ->willReturn('catalog_product_entity_decimal');
249  $this->connection
250  ->expects($this->once())
251  ->method('delete')
252  ->with(
253  'catalog_product_entity_decimal',
254  [
255  'attribute_id = ?' => $attributeId,
256  'row_id IN (?)' => [1, 2]
257  ]
258  )
259  ->willReturnSelf();
260  $this->connection->expects($this->once())->method('commit')->willReturnSelf();
261  $this->metadataPool->expects($this->atLeastOnce())->method('getMetadata')->willReturnSelf();
262  $this->metadataPool->expects($this->atLeastOnce())->method('getLinkField')->willReturn('row_id');
263  $this->model->delete($skus);
264  }
265 
272  public function testDeleteWithException()
273  {
274  $attributeId = 5;
275  $skus = ['sku_1', 'sku_2'];
276  $idsBySku = [
277  'sku_1' => [
278  1 => \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
279  ],
280  'sku_2' => [
281  2 => \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL
282  ]
283  ];
284  $this->productIdLocator
285  ->expects($this->once())
286  ->method('retrieveProductIdsBySkus')->with($skus)
287  ->willReturn($idsBySku);
288  $this->attributeRepository->expects($this->once())->method('get')->willReturn($this->productAttribute);
289  $this->productAttribute->expects($this->once())->method('getAttributeId')->willReturn($attributeId);
290  $this->attributeResource->expects($this->atLeastOnce(2))->method('getConnection')
291  ->willReturn($this->connection);
292  $this->connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
293  $this->attributeResource
294  ->expects($this->once())
295  ->method('getTable')
296  ->with('catalog_product_entity_decimal')
297  ->willReturn('catalog_product_entity_decimal');
298  $this->connection
299  ->expects($this->once())
300  ->method('delete')
301  ->with(
302  'catalog_product_entity_decimal',
303  [
304  'attribute_id = ?' => $attributeId,
305  'row_id IN (?)' => [1, 2]
306  ]
307  )
308  ->willReturnSelf();
309  $this->connection->expects($this->once())->method('commit')->willThrowException(new \Exception());
310  $this->connection->expects($this->once())->method('rollBack')->willReturnSelf();
311  $this->metadataPool->expects($this->atLeastOnce())->method('getMetadata')->willReturnSelf();
312  $this->metadataPool->expects($this->atLeastOnce())->method('getLinkField')->willReturn('row_id');
313  $this->model->delete($skus);
314  }
315 
324  public function testRetrieveSkuById($expectedResult, $id, array $skus)
325  {
326  $this->productIdLocator
327  ->expects($this->once())
328  ->method('retrieveProductIdsBySkus')
329  ->willReturn($skus);
330 
331  $this->assertEquals($expectedResult, $this->model->retrieveSkuById($id, $skus));
332  }
333 
339  public function dataProviderRetrieveSkuById()
340  {
341  return [
342  [
343  null,
344  2,
345  ['sku_1' => [1 => 1]]
346  ],
347  [
348  'sku_1',
349  1,
350  ['sku_1' => [1 => 1]]
351  ],
352  [
353  null,
354  1,
355  ['sku_1' => [2 => 1]]
356  ],
357  ];
358  }
359 }
$objectManager
Definition: bootstrap.php:17
$id
Definition: fieldset.phtml:14
foreach($websiteCodes as $websiteCode) $skus