Skip to content

Commit 19b4bf1

Browse files
committed
ACP2E-3376: added unit tests
1 parent a8c459f commit 19b4bf1

File tree

1 file changed

+172
-0
lines changed
  • app/code/Magento/Catalog/Test/Unit/Model/Product/ResourceModel/Product/Price

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\Model\Product\ResourceModel\Product\Price;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Model\ResourceModel\Attribute;
12+
use Magento\Catalog\Model\ResourceModel\Product\Price\BasePrice;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\Framework\DB\Select;
15+
use Magento\Framework\EntityManager\EntityMetadataInterface;
16+
use Magento\Framework\EntityManager\MetadataPool;
17+
use Magento\Framework\Exception\CouldNotSaveException;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class BasePriceTest extends TestCase
22+
{
23+
/**
24+
* @var Attribute|MockObject
25+
*/
26+
private $attributeResource;
27+
28+
/**
29+
* @var MetadataPool|MockObject
30+
*/
31+
private $metadataPool;
32+
33+
/**
34+
* @var EntityMetadataInterface|MockObject
35+
*/
36+
private $entityMetadata;
37+
38+
/**
39+
* @var BasePrice
40+
*/
41+
private $model;
42+
43+
protected function setUp(): void
44+
{
45+
$this->attributeResource = $this->createMock(Attribute::class);
46+
$this->metadataPool = $this->createMock(MetadataPool::class);
47+
$this->entityMetadata = $this->createMock(EntityMetadataInterface::class);
48+
49+
$this->model = new BasePrice(
50+
$this->attributeResource,
51+
$this->metadataPool,
52+
5
53+
);
54+
}
55+
56+
public function testUpdate()
57+
{
58+
$priceBunches = [
59+
['store_id' => 0, 'row_id' => 1, 'value' => 15, 'attribute_id' => 5],
60+
['store_id' => 0, 'row_id' => 2, 'value' => 20, 'attribute_id' => 5],
61+
['store_id' => 1, 'row_id' => 1, 'value' => 15, 'attribute_id' => 5],
62+
['store_id' => 1, 'row_id' => 2, 'value' => 20, 'attribute_id' => 5]
63+
];
64+
65+
$connection = $this->createMock(AdapterInterface::class);
66+
$select = $this->createMock(Select::class);
67+
$this->attributeResource->expects($this->atLeastOnce())
68+
->method('getConnection')
69+
->willReturn($connection);
70+
71+
$this->attributeResource->expects($this->atLeastOnce())
72+
->method('getTable')
73+
->with('catalog_product_entity_decimal')
74+
->willReturn('catalog_product_entity_decimal');
75+
76+
$select->expects($this->once())->method('from')->with('catalog_product_entity_decimal')->willReturnSelf();
77+
$select->expects($this->atLeastOnce())->method('where')->willReturnSelf();
78+
79+
$connection->expects($this->once())
80+
->method('beginTransaction');
81+
82+
$connection->expects($this->once())
83+
->method('commit');
84+
85+
$connection->expects($this->atLeastOnce())
86+
->method('update');
87+
88+
$connection->expects($this->atLeastOnce())
89+
->method('insertMultiple')
90+
->willReturn(1);
91+
92+
$connection->expects($this->once())
93+
->method('select')
94+
->willReturn($select);
95+
96+
$connection->expects($this->once())
97+
->method('fetchAll')
98+
->willReturn([
99+
['value_id' => 1, 'store_id' => 1, 'row_id' => 1, 'attribute_id' => 5],
100+
['value_id' => 2, 'store_id' => 2, 'row_id' => 2, 'attribute_id' => 5]
101+
]);
102+
103+
$this->metadataPool->expects($this->atLeastOnce())
104+
->method('getMetadata')
105+
->with(ProductInterface::class)
106+
->willReturn($this->entityMetadata);
107+
108+
$this->entityMetadata->expects($this->atLeastOnce())
109+
->method('getLinkField')
110+
->willReturn('row_id');
111+
112+
$this->model->update($priceBunches);
113+
}
114+
115+
public function testUpdateThrowsException()
116+
{
117+
$this->expectException(CouldNotSaveException::class);
118+
119+
$priceBunches = [
120+
['store_id' => 1, 'row_id' => 1, 'value' => 15, 'attribute_id' => 5]
121+
];
122+
123+
$connection = $this->createMock(AdapterInterface::class);
124+
$select = $this->createMock(Select::class);
125+
$this->attributeResource->expects($this->atLeastOnce())
126+
->method('getConnection')
127+
->willReturn($connection);
128+
129+
$this->attributeResource->expects($this->atLeastOnce())
130+
->method('getTable')
131+
->with('catalog_product_entity_decimal')
132+
->willReturn('catalog_product_entity_decimal');
133+
134+
$select->expects($this->once())->method('from')->with('catalog_product_entity_decimal')->willReturnSelf();
135+
$select->expects($this->atLeastOnce())->method('where')->willReturnSelf();
136+
137+
$connection->expects($this->once())
138+
->method('beginTransaction');
139+
140+
$connection->expects($this->once())
141+
->method('commit');
142+
143+
$connection->expects($this->atLeastOnce())
144+
->method('update');
145+
146+
$connection->expects($this->once())
147+
->method('select')
148+
->willReturn($select);
149+
150+
$connection->expects($this->once())
151+
->method('fetchAll')
152+
->willReturn([
153+
['value_id' => 1, 'store_id' => 1, 'row_id' => 1, 'attribute_id' => 5],
154+
['value_id' => 2, 'store_id' => 2, 'row_id' => 2, 'attribute_id' => 5]
155+
]);
156+
157+
$this->metadataPool->expects($this->atLeastOnce())
158+
->method('getMetadata')
159+
->with(ProductInterface::class)
160+
->willReturn($this->entityMetadata);
161+
162+
$this->entityMetadata->expects($this->atLeastOnce())
163+
->method('getLinkField')
164+
->willReturn('row_id');
165+
166+
$connection->expects($this->once())
167+
->method('commit')
168+
->willThrowException(new \Exception());
169+
170+
$this->model->update($priceBunches);
171+
}
172+
}

0 commit comments

Comments
 (0)