Skip to content

Commit

Permalink
Merge pull request #518 from adobe-commerce-tier-4/Tier4-Kings-PR-11-…
Browse files Browse the repository at this point in the history
…22-2024

[Support Tier-4-Kings glo02433] 11.22.2024 Regular delivery of bugfixes and improvements
  • Loading branch information
dhorytskyi authored Dec 9, 2024
2 parents 132d98c + 28dc022 commit e6fe0aa
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Indexer\Product\Full as FullProductIndexer;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Inventory\Model\SourceItem\Command\GetSourceItemsBySku;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;

class StockItemRepositoryPlugin
{

/**
* @param FullProductIndexer $fullProductIndexer
* @param InventoryIndexer $inventoryIndexer
* @param ProductRepositoryInterface $productRepository
* @param GetSourceItemsBySku $getSourceItemsBySku
*/
public function __construct(
private FullProductIndexer $fullProductIndexer,
private InventoryIndexer $inventoryIndexer,
private ProductRepositoryInterface $productRepository,
private getSourceItemsBySku $getSourceItemsBySku
) {
}

/**
* Complex reindex after product stock item has been saved.
*
* @param StockItemRepository $subject
* @param StockItemInterface $stockItem
* @return StockItemInterface
* @throws NoSuchEntityException
*/
public function afterSave(StockItemRepository $subject, StockItemInterface $stockItem): StockItemInterface
{
$product = $this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Test\Unit\Plugin\CatalogInventory\Model\Stock\StockItemRepository;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Indexer\Product\Full as FullProductIndexer;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use Magento\Inventory\Model\SourceItem;
use Magento\Inventory\Model\SourceItem\Command\GetSourceItemsBySku;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository\StockItemRepositoryPlugin;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class StockItemRepositoryPluginTest extends TestCase
{
/** @var FullProductIndexer|MockObject */
private $fullProductIndexer;

/** @var InventoryIndexer|MockObject */
private $inventoryIndexer;

/** @var ProductRepositoryInterface|MockObject */
private $productRepository;

/** @var GetSourceItemsBySku|MockObject */
private $getSourceItemsBySku;

/** @var StockItemRepositoryPlugin */
private $plugin;

protected function setUp(): void
{
$this->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);
}
}
7 changes: 5 additions & 2 deletions InventoryCatalog/etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2020 Adobe
* All Rights Reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\ResourceModel\Product">
<plugin name="create_source_items" type="Magento\InventoryCatalog\Plugin\Catalog\Model\ResourceModel\Product\CreateSourceItemsPlugin"/>
</type>
<type name="Magento\CatalogInventory\Model\Stock\StockItemRepository">
<plugin name="reindex_after_save_product_stock_item" type="Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository\StockItemRepositoryPlugin"/>
</type>
</config>
7 changes: 5 additions & 2 deletions InventoryCatalog/etc/webapi_soap/di.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2020 Adobe
* All Rights Reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\ResourceModel\Product">
<plugin name="create_source_items" type="Magento\InventoryCatalog\Plugin\Catalog\Model\ResourceModel\Product\CreateSourceItemsPlugin"/>
</type>
<type name="Magento\CatalogInventory\Model\Stock\StockItemRepository">
<plugin name="reindex_after_save_product_stock_item" type="Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository\StockItemRepositoryPlugin"/>
</type>
</config>

0 comments on commit e6fe0aa

Please sign in to comment.