Skip to content

Commit df7b502

Browse files
committed
Covering the InvalidatePriceIndexUponConfigChangeObserver for CatalogInventory by Unit Test
1 parent 9400c1e commit df7b502

File tree

1 file changed

+113
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)