diff --git a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php new file mode 100644 index 000000000000..b3261b885928 --- /dev/null +++ b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php @@ -0,0 +1,56 @@ +productRepository->getById($stockItem->getProductId()); + $this->fullProductIndexer->executeRow($product->getId()); + $sourceItems = $this->getSourceItemsBySku->execute($product->getSku()); + $sourceItemIds = []; + + foreach ($sourceItems as $sourceItem) { + $sourceItemIds[] = $sourceItem->getId(); + } + $this->inventoryIndexer->executeList($sourceItemIds); + return $stockItem; + } +} diff --git a/InventoryCatalog/Test/Unit/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPluginTest.php b/InventoryCatalog/Test/Unit/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPluginTest.php new file mode 100644 index 000000000000..03b7f06b977d --- /dev/null +++ b/InventoryCatalog/Test/Unit/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPluginTest.php @@ -0,0 +1,81 @@ +fullProductIndexer = $this->createMock(FullProductIndexer::class); + $this->inventoryIndexer = $this->createMock(InventoryIndexer::class); + $this->productRepository = $this->createMock(ProductRepositoryInterface::class); + $this->getSourceItemsBySku = $this->createMock(GetSourceItemsBySku::class); + + $this->plugin = new StockItemRepositoryPlugin( + $this->fullProductIndexer, + $this->inventoryIndexer, + $this->productRepository, + $this->getSourceItemsBySku + ); + } + + public function testAfterSave(): void + { + $productId = 123; + $sku = 'test-sku'; + $sourceItemId = 456; + + $stockItem = $this->createMock(StockItemInterface::class); + $stockItem->method('getProductId')->willReturn($productId); + + $product = $this->createMock(\Magento\Catalog\Api\Data\ProductInterface::class); + $product->method('getId')->willReturn($productId); + $product->method('getSku')->willReturn($sku); + $this->productRepository->method('getById')->with($productId)->willReturn($product); + + $sourceItem = $this->createMock(SourceItem::class); + $sourceItem->method('getId')->willReturn($sourceItemId); + $this->getSourceItemsBySku->method('execute')->with($sku)->willReturn([$sourceItem]); + + $this->fullProductIndexer->expects($this->once())->method('executeRow')->with($productId); + $this->inventoryIndexer->expects($this->once())->method('executeList')->with([$sourceItemId]); + + $result = $this->plugin->afterSave( + $this->createMock(StockItemRepository::class), + $stockItem + ); + + $this->assertSame($stockItem, $result); + } +} diff --git a/InventoryCatalog/etc/webapi_rest/di.xml b/InventoryCatalog/etc/webapi_rest/di.xml index 223b6edb2d02..4118e19caf38 100644 --- a/InventoryCatalog/etc/webapi_rest/di.xml +++ b/InventoryCatalog/etc/webapi_rest/di.xml @@ -1,12 +1,15 @@ + + + diff --git a/InventoryCatalog/etc/webapi_soap/di.xml b/InventoryCatalog/etc/webapi_soap/di.xml index 223b6edb2d02..4118e19caf38 100644 --- a/InventoryCatalog/etc/webapi_soap/di.xml +++ b/InventoryCatalog/etc/webapi_soap/di.xml @@ -1,12 +1,15 @@ + + +