Skip to content

Commit 5332072

Browse files
ENGCOM-2777: [Forwardport] Catalog: Add unit tests for Cron classes #17572
- Merge Pull Request #17572 from jignesh-baldha/magento2:2.3-develop-PR-port-17561 - Merged commits: 1. 50e74cd 2. 077bf2b
2 parents 03ab0fd + 077bf2b commit 5332072

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\Cron;
9+
10+
use Magento\Catalog\Cron\DeleteAbandonedStoreFlatTables;
11+
use Magento\Catalog\Helper\Product\Flat\Indexer;
12+
13+
/**
14+
* @covers \Magento\Catalog\Cron\DeleteAbandonedStoreFlatTables
15+
*/
16+
class AvailabilityCheckerTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* Testable Object
20+
*
21+
* @var DeleteAbandonedStoreFlatTables
22+
*/
23+
private $deleteAbandonedStoreFlatTables;
24+
25+
/**
26+
* @var Indexer|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $indexerMock;
29+
30+
/**
31+
* Set Up
32+
*
33+
* @return void
34+
*/
35+
protected function setUp()
36+
{
37+
$this->indexerMock = $this->createMock(Indexer::class);
38+
$this->deleteAbandonedStoreFlatTables = new DeleteAbandonedStoreFlatTables($this->indexerMock);
39+
}
40+
41+
/**
42+
* Test execute method
43+
*
44+
* @return void
45+
*/
46+
public function testExecute()
47+
{
48+
$this->indexerMock->expects($this->once())->method('deleteAbandonedStoreFlatTables');
49+
$this->deleteAbandonedStoreFlatTables->execute();
50+
}
51+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\Cron;
9+
10+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
11+
use Magento\Catalog\Cron\DeleteOutdatedPriceValues;
12+
use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository;
13+
use Magento\Eav\Model\Entity\Attribute;
14+
use Magento\Eav\Model\Entity\Attribute\Backend\BackendInterface;
15+
use Magento\Framework\App\Config\MutableScopeConfigInterface as ScopeConfig;
16+
use Magento\Framework\App\ResourceConnection;
17+
use Magento\Framework\DB\Adapter\AdapterInterface;
18+
use Magento\Store\Model\Store;
19+
20+
/**
21+
* @covers \Magento\Catalog\Cron\DeleteOutdatedPriceValues
22+
*/
23+
class DeleteOutdatedPriceValuesTest extends \PHPUnit\Framework\TestCase
24+
{
25+
/**
26+
* Testable Object
27+
*
28+
* @var DeleteOutdatedPriceValues
29+
*/
30+
private $deleteOutdatedPriceValues;
31+
32+
/**
33+
* @var AttributeRepository|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $attributeRepositoryMock;
36+
37+
/**
38+
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $resourceConnectionMock;
41+
42+
/**
43+
* @var ScopeConfig|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $scopeConfigMock;
46+
47+
/**
48+
* @var Attribute|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
private $attributeMock;
51+
52+
/**
53+
* @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
54+
*/
55+
private $dbAdapterMock;
56+
57+
/**
58+
* @var BackendInterface|\PHPUnit_Framework_MockObject_MockObject
59+
*/
60+
private $attributeBackendMock;
61+
62+
/**
63+
* Set Up
64+
*
65+
* @return void
66+
*/
67+
protected function setUp()
68+
{
69+
$this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
70+
$this->attributeRepositoryMock = $this->createMock(AttributeRepository::class);
71+
$this->attributeMock = $this->createMock(Attribute::class);
72+
$this->scopeConfigMock = $this->createMock(ScopeConfig::class);
73+
$this->dbAdapterMock = $this->createMock(AdapterInterface::class);
74+
$this->attributeBackendMock = $this->createMock(BackendInterface::class);
75+
$this->deleteOutdatedPriceValues = new DeleteOutdatedPriceValues(
76+
$this->resourceConnectionMock,
77+
$this->attributeRepositoryMock,
78+
$this->scopeConfigMock
79+
);
80+
}
81+
82+
/**
83+
* Test execute method
84+
*
85+
* @return void
86+
*/
87+
public function testExecute()
88+
{
89+
$table = 'catalog_product_entity_decimal';
90+
$attributeId = 15;
91+
$conditions = ['first', 'second'];
92+
93+
$this->scopeConfigMock
94+
->expects($this->once())
95+
->method('getValue')
96+
->with(Store::XML_PATH_PRICE_SCOPE)
97+
->willReturn(Store::XML_PATH_PRICE_SCOPE);
98+
$this->attributeRepositoryMock
99+
->expects($this->once())
100+
->method('get')
101+
->with(ProductAttributeInterface::ENTITY_TYPE_CODE, ProductAttributeInterface::CODE_PRICE)
102+
->willReturn($this->attributeMock);
103+
$this->attributeMock
104+
->expects($this->once())
105+
->method('getId')
106+
->willReturn($attributeId);
107+
$this->attributeMock
108+
->expects($this->once())
109+
->method('getBackend')
110+
->willReturn($this->attributeBackendMock);
111+
$this->attributeBackendMock
112+
->expects($this->once())
113+
->method('getTable')
114+
->willReturn($table);
115+
$this->resourceConnectionMock
116+
->expects($this->once())
117+
->method('getConnection')
118+
->willReturn($this->dbAdapterMock);
119+
$this->dbAdapterMock
120+
->expects($this->exactly(2))
121+
->method('quoteInto')
122+
->willReturnMap([
123+
['attribute_id = ?', $attributeId, null, null, $conditions[0]],
124+
['store_id != ?', Store::DEFAULT_STORE_ID, null, null, $conditions[1]],
125+
]);
126+
$this->dbAdapterMock
127+
->expects($this->once())
128+
->method('delete')
129+
->with($table, $conditions);
130+
131+
$this->deleteOutdatedPriceValues->execute();
132+
}
133+
134+
/**
135+
* Test execute method
136+
* The price scope config option is not equal to global value
137+
*
138+
* @return void
139+
*/
140+
public function testExecutePriceConfigIsNotSetToGlobal()
141+
{
142+
$this->scopeConfigMock
143+
->expects($this->once())
144+
->method('getValue')
145+
->with(Store::XML_PATH_PRICE_SCOPE)
146+
->willReturn(null);
147+
$this->attributeRepositoryMock
148+
->expects($this->never())
149+
->method('get');
150+
$this->dbAdapterMock
151+
->expects($this->never())
152+
->method('delete');
153+
154+
$this->deleteOutdatedPriceValues->execute();
155+
}
156+
}

0 commit comments

Comments
 (0)