-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
magento-engcom-team
merged 2 commits into
magento:2.4-develop
from
eduard13:stock-invalidate-price-unit-test
Dec 24, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
.../CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
$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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
$this->indexerMock->expects($this->never()) | ||
->method('invalidate'); | ||
$this->priceIndexProcessorMock->expects($this->never()) | ||
->method('getIndexer') | ||
->willReturn($this->indexerMock); | ||
|
||
$this->observer->execute($this->observerMock); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.