Skip to content

Commit 1c2aa61

Browse files
ENGCOM-6512: [CatalogInventory] Covering the InvalidatePriceIndexUponConfigChangeObserver for Catalog… #26150
- Merge Pull Request #26150 from eduard13/magento2:stock-invalidate-price-unit-test - Merged commits: 1. df7b502 2. 82fee14
2 parents 4165276 + 82fee14 commit 1c2aa61

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\CatalogInventory\Test\Unit\Observer;
9+
10+
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
11+
use Magento\CatalogInventory\Model\Configuration;
12+
use Magento\CatalogInventory\Observer\InvalidatePriceIndexUponConfigChangeObserver;
13+
use Magento\Framework\Event;
14+
use Magento\Framework\Event\Observer;
15+
use Magento\Framework\Indexer\IndexerInterface;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Class InvalidatePriceIndexUponConfigChangeObserverTest
22+
*
23+
* Testing invalidating product price index onn config changing
24+
*/
25+
class InvalidatePriceIndexUponConfigChangeObserverTest extends TestCase
26+
{
27+
/**
28+
* @var InvalidatePriceIndexUponConfigChangeObserver
29+
*/
30+
private $observer;
31+
32+
/**
33+
* @var Processor|MockObject
34+
*/
35+
private $priceIndexProcessorMock;
36+
37+
/**
38+
* @var Observer|MockObject
39+
*/
40+
private $observerMock;
41+
42+
/**
43+
* @var Event|MockObject
44+
*/
45+
private $eventMock;
46+
47+
/**
48+
* @var IndexerInterface|MockObject
49+
*/
50+
private $indexerMock;
51+
52+
/**
53+
* Set Up
54+
*/
55+
public function setUp()
56+
{
57+
$objectManager = new ObjectManager($this);
58+
$this->priceIndexProcessorMock = $this->createMock(Processor::class);
59+
$this->indexerMock = $this->getMockBuilder(IndexerInterface::class)
60+
->getMockForAbstractClass();
61+
$this->observerMock = $this->createMock(Observer::class);
62+
$this->eventMock = $this->getMockBuilder(Event::class)
63+
->disableOriginalConstructor()
64+
->setMethods(['getChangedPaths'])
65+
->getMock();
66+
67+
$this->observer = $objectManager->getObject(
68+
InvalidatePriceIndexUponConfigChangeObserver::class,
69+
[
70+
'priceIndexProcessor' => $this->priceIndexProcessorMock
71+
]
72+
);
73+
}
74+
75+
/**
76+
* Testing invalidating product price index on catalog inventory config changes
77+
*/
78+
public function testInvalidatingPriceOnChangingOutOfStockConfig()
79+
{
80+
$changedPaths = [Configuration::XML_PATH_SHOW_OUT_OF_STOCK];
81+
82+
$this->eventMock->expects($this->once())
83+
->method('getChangedPaths')
84+
->willReturn($changedPaths);
85+
$this->observerMock->expects($this->once())
86+
->method('getEvent')
87+
->willReturn($this->eventMock);
88+
$this->indexerMock->expects($this->once())
89+
->method('invalidate');
90+
$this->priceIndexProcessorMock->expects($this->once())
91+
->method('getIndexer')
92+
->willReturn($this->indexerMock);
93+
94+
$this->observer->execute($this->observerMock);
95+
}
96+
97+
/**
98+
* Testing invalidating product price index on changing any other config
99+
*/
100+
public function testInvalidatingPriceOnChangingAnyOtherConfig()
101+
{
102+
$changedPaths = [Configuration::XML_PATH_ITEM_AUTO_RETURN];
103+
104+
$this->eventMock->expects($this->once())
105+
->method('getChangedPaths')
106+
->willReturn($changedPaths);
107+
$this->observerMock->expects($this->once())
108+
->method('getEvent')
109+
->willReturn($this->eventMock);
110+
$this->indexerMock->expects($this->never())
111+
->method('invalidate');
112+
$this->priceIndexProcessorMock->expects($this->never())
113+
->method('getIndexer')
114+
->willReturn($this->indexerMock);
115+
116+
$this->observer->execute($this->observerMock);
117+
}
118+
}

0 commit comments

Comments
 (0)