Skip to content

Commit 182e2e9

Browse files
author
Stanislav Idolov
authored
ENGCOM-2772: Catalog: Add unit tests for Cron classes #17561
2 parents 73c22ec + c385580 commit 182e2e9

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-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: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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->expects($this->once())->method('getValue')->with(Store::XML_PATH_PRICE_SCOPE)
94+
->willReturn(Store::XML_PATH_PRICE_SCOPE);
95+
$this->attributeRepositoryMock->expects($this->once())->method('get')
96+
->with(ProductAttributeInterface::ENTITY_TYPE_CODE, ProductAttributeInterface::CODE_PRICE)
97+
->willReturn($this->attributeMock);
98+
$this->attributeMock->expects($this->once())->method('getId')->willReturn($attributeId);
99+
$this->attributeMock->expects($this->once())->method('getBackend')->willReturn($this->attributeBackendMock);
100+
$this->attributeBackendMock->expects($this->once())->method('getTable')->willReturn($table);
101+
$this->resourceConnectionMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
102+
$this->dbAdapterMock->expects($this->exactly(2))->method('quoteInto')->willReturnMap([
103+
['attribute_id = ?', $attributeId, null, null, $conditions[0]],
104+
['store_id != ?', Store::DEFAULT_STORE_ID, null, null, $conditions[1]],
105+
]);
106+
$this->dbAdapterMock->expects($this->once())->method('delete')->with($table, $conditions);
107+
$this->deleteOutdatedPriceValues->execute();
108+
}
109+
110+
/**
111+
* Test execute method
112+
* The price scope config option is not equal to global value
113+
*
114+
* @return void
115+
*/
116+
public function testExecutePriceConfigIsNotSetToGlobal()
117+
{
118+
$this->scopeConfigMock->expects($this->once())->method('getValue')->with(Store::XML_PATH_PRICE_SCOPE)
119+
->willReturn(null);
120+
$this->attributeRepositoryMock->expects($this->never())->method('get');
121+
$this->dbAdapterMock->expects($this->never())->method('delete');
122+
123+
$this->deleteOutdatedPriceValues->execute();
124+
}
125+
}

0 commit comments

Comments
 (0)