Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CatalogInventory] Covering the InvalidatePriceIndexUponConfigChangeObserver for Catalog… #26150

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogInventory\Test\Unit\Observer;

use Magento\Catalog\Model\Indexer\Product\Price\Processor;
use Magento\CatalogInventory\Model\Configuration;
use Magento\CatalogInventory\Observer\InvalidatePriceIndexUponConfigChangeObserver;
use Magento\Framework\Event;
use Magento\Framework\Event\Observer;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Class InvalidatePriceIndexUponConfigChangeObserverTest
*
* Testing invalidating product price index onn config changing
*/
class InvalidatePriceIndexUponConfigChangeObserverTest extends TestCase
{
/**
* @var InvalidatePriceIndexUponConfigChangeObserver
*/
private $observer;

/**
* @var Processor|MockObject
*/
private $priceIndexProcessorMock;

/**
* @var Observer|MockObject
*/
private $observerMock;

/**
* @var Event|MockObject
*/
private $eventMock;

/**
* @var IndexerInterface|MockObject
*/
private $indexerMock;

/**
* Set Up
*/
public function setUp()
{
$objectManager = new ObjectManager($this);
$this->priceIndexProcessorMock = $this->createMock(Processor::class);
$this->indexerMock = $this->getMockBuilder(IndexerInterface::class)
->getMockForAbstractClass();
$this->observerMock = $this->createMock(Observer::class);
$this->eventMock = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->setMethods(['getChangedPaths'])
->getMock();

$this->observer = $objectManager->getObject(
InvalidatePriceIndexUponConfigChangeObserver::class,
[
'priceIndexProcessor' => $this->priceIndexProcessorMock
]
);
}

/**
* Testing invalidating product price index on catalog inventory config changes
*/
public function testInvalidatingPriceOnChangingOutOfStockConfig()
{
$changedPaths = [Configuration::XML_PATH_SHOW_OUT_OF_STOCK];

$this->eventMock->expects($this->once())
->method('getChangedPaths')
->willReturn($changedPaths);
$this->observerMock->expects($this->once())
->method('getEvent')
->willReturn($this->eventMock);
Comment on lines +85 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the expecting single execution of getEvent is critical.
When implementation is changed and extra behaviour is added, then this test would fail - even if the functionality covered by tour test has not changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with you, but in the same time, why should we call getEvent twice instead of storing it into a variable (have it used only once) and use it whenever we want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I approved PR, because it's not so easy to decide what's the correct approach.

$this->indexerMock->expects($this->once())
->method('invalidate');
$this->priceIndexProcessorMock->expects($this->once())
->method('getIndexer')
->willReturn($this->indexerMock);

$this->observer->execute($this->observerMock);
}

/**
* Testing invalidating product price index on changing any other config
*/
public function testInvalidatingPriceOnChangingAnyOtherConfig()
{
$changedPaths = [Configuration::XML_PATH_ITEM_AUTO_RETURN];

$this->eventMock->expects($this->once())
->method('getChangedPaths')
->willReturn($changedPaths);
$this->observerMock->expects($this->once())
->method('getEvent')
->willReturn($this->eventMock);
Comment on lines +107 to +109
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the expecting single execution of getEvent is critical.
When implementation is changed and extra behaviour is added, then this test would fail - even if the functionality covered by tour test has not changed.

$this->indexerMock->expects($this->never())
->method('invalidate');
$this->priceIndexProcessorMock->expects($this->never())
->method('getIndexer')
->willReturn($this->indexerMock);

$this->observer->execute($this->observerMock);
}
}