From df7b502c8fbb11b294a119abc232db1c422466eb Mon Sep 17 00:00:00 2001 From: eduard13 Date: Fri, 20 Dec 2019 12:23:38 +0200 Subject: [PATCH 1/2] Covering the InvalidatePriceIndexUponConfigChangeObserver for CatalogInventory by Unit Test --- ...PriceIndexUponConfigChangeObserverTest.php | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 app/code/Magento/CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php new file mode 100644 index 0000000000000..9762b59c3e883 --- /dev/null +++ b/app/code/Magento/CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php @@ -0,0 +1,113 @@ +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 = new InvalidatePriceIndexUponConfigChangeObserver( + $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); + $this->indexerMock->expects($this->never()) + ->method('invalidate'); + $this->priceIndexProcessorMock->expects($this->never()) + ->method('getIndexer') + ->willReturn($this->indexerMock); + + $this->observer->execute($this->observerMock); + } +} From 82fee14d8ae788c0da5eb1f23d5f335376b90613 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Fri, 20 Dec 2019 12:34:02 +0200 Subject: [PATCH 2/2] Adding ObjectManager --- .../InvalidatePriceIndexUponConfigChangeObserverTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php index 9762b59c3e883..1dd7df8952473 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Observer/InvalidatePriceIndexUponConfigChangeObserverTest.php @@ -13,6 +13,7 @@ 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; @@ -53,6 +54,7 @@ class InvalidatePriceIndexUponConfigChangeObserverTest extends TestCase */ public function setUp() { + $objectManager = new ObjectManager($this); $this->priceIndexProcessorMock = $this->createMock(Processor::class); $this->indexerMock = $this->getMockBuilder(IndexerInterface::class) ->getMockForAbstractClass(); @@ -62,8 +64,11 @@ public function setUp() ->setMethods(['getChangedPaths']) ->getMock(); - $this->observer = new InvalidatePriceIndexUponConfigChangeObserver( - $this->priceIndexProcessorMock + $this->observer = $objectManager->getObject( + InvalidatePriceIndexUponConfigChangeObserver::class, + [ + 'priceIndexProcessor' => $this->priceIndexProcessorMock + ] ); }