Skip to content

Commit

Permalink
Merge pull request #399 from magento-l3/PR-L3-08112023
Browse files Browse the repository at this point in the history
Pr l3 08112023
  • Loading branch information
2 parents 95df453 + 423d564 commit 412ba39
Show file tree
Hide file tree
Showing 20 changed files with 953 additions and 478 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,66 @@
*/
declare(strict_types=1);

namespace Magento\InventoryBundleProductIndexer\Plugin\InventoryIndexer\Indexer\SourceItem\Strategy\Sync;
namespace Magento\InventoryBundleProductIndexer\Indexer;

use Magento\Framework\Exception\StateException;
use Magento\InventoryBundleProductIndexer\Indexer\SourceItem\SourceItemIndexer as BundleProductsSourceItemIndexer;
use Magento\InventoryIndexer\Indexer\SourceItem\Strategy\Sync;
use Magento\InventoryIndexer\Indexer\SourceItem\CompositeProductProcessorInterface;

/**
* Reindex bundle product source items.
*/
class SourceItemIndexerPlugin
class SourceItemIndexerProcessor implements CompositeProductProcessorInterface
{
/**
* @var BundleProductsSourceItemIndexer
*/
private $bundleProductsSourceItemIndexer;

/**
* Processor sort order
*
* @var int
*/
private $sortOrder;

/**
* @param BundleProductsSourceItemIndexer $configurableProductsSourceItemIndexer
* @param array $sortOrder
*/
public function __construct(
BundleProductsSourceItemIndexer $configurableProductsSourceItemIndexer
BundleProductsSourceItemIndexer $configurableProductsSourceItemIndexer,
int $sortOrder = 5
) {
$this->bundleProductsSourceItemIndexer = $configurableProductsSourceItemIndexer;
$this->sortOrder = $sortOrder;
}

/**
* Reindex source items list for bundle products.
*
* @param Sync $subject
* @param callable $proceed
* @param array $sourceItemIds
* @param array $saleableStatusesBeforeSync
* @param array $saleableStatusesAfterSync
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws StateException
*/
public function aroundExecuteList(
Sync $subject,
callable $proceed,
array $sourceItemIds
) {
$proceed($sourceItemIds);
public function process(
array $sourceItemIds,
array $saleableStatusesBeforeSync,
array $saleableStatusesAfterSync
): void {
$this->bundleProductsSourceItemIndexer->executeList($sourceItemIds);
}

/**
* @inheritdoc
*
* @return int
*/
public function getSortOrder(): int
{
return $this->sortOrder;
}
}
15 changes: 12 additions & 3 deletions InventoryBundleProductIndexer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
<plugin name="bundle_product_index_full" type="Magento\InventoryBundleProductIndexer\Plugin\InventoryIndexer\Indexer\Stock\Strategy\Sync\ReindexFullPlugin"/>
<plugin name="bundle_product_index_list" type="Magento\InventoryBundleProductIndexer\Plugin\InventoryIndexer\Indexer\Stock\Strategy\Sync\ReindexListPlugin"/>
</type>
<type name="Magento\InventoryIndexer\Indexer\SourceItem\Strategy\Sync">
<plugin name="bundle_product_index" type="Magento\InventoryBundleProductIndexer\Plugin\InventoryIndexer\Indexer\SourceItem\Strategy\Sync\SourceItemIndexerPlugin" sortOrder="20"/>
</type>
<type name="Magento\InventoryBundleProductIndexer\Indexer\Stock\StockIndexer">
<arguments>
<argument name="indexHandler" xsi:type="object">Magento\InventoryIndexer\Indexer\IndexHandler</argument>
Expand All @@ -33,4 +30,16 @@
<type name="Magento\Bundle\Api\ProductLinkManagementAddChildrenInterface">
<plugin name="reindex_source_items_after_bulk_add_bundle_selection" type="Magento\InventoryBundleProductIndexer\Plugin\Bundle\Model\LinkManagement\ReindexSourceItemsAfterBulkAddBundleSelectionPlugin"/>
</type>
<type name="Magento\InventoryBundleProductIndexer\Indexer\SourceItemIndexerProcessor">
<arguments>
<argument name="sortOrder" xsi:type="number">5</argument>
</arguments>
</type>
<type name="Magento\InventoryIndexer\Indexer\SourceItem\Strategy\Sync">
<arguments>
<argument name="saleabilityChangesProcessorsPool" xsi:type="array">
<item name="sourceItemIndexerProcessor" xsi:type="object">Magento\InventoryBundleProductIndexer\Indexer\SourceItemIndexerProcessor</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
*/
declare(strict_types=1);

namespace Magento\InventoryCache\Plugin\InventoryIndexer\Indexer\SourceItem\Strategy\Sync;
namespace Magento\InventoryCache\Model;

use Magento\Framework\Indexer\IndexerRegistry;
use Magento\InventoryCache\Model\FlushCacheByCategoryIds;
use Magento\InventoryCache\Model\FlushCacheByProductIds;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use Magento\InventoryIndexer\Indexer\SourceItem\CompositeProductProcessorInterface;
use Magento\InventoryIndexer\Model\GetProductsIdsToProcess;
use Magento\InventoryIndexer\Indexer\SourceItem\Strategy\Sync;
use Magento\InventoryIndexer\Indexer\SourceItem\GetSalableStatuses;
use Magento\InventoryIndexer\Model\ResourceModel\GetCategoryIdsByProductIds;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;

/**
* Clean cache for corresponding products after source item reindex.
*/
class CacheFlush
class CacheFlushProcessor implements CompositeProductProcessorInterface
{
/**
* Processor sort order
*
* @var int
*/
private $sortOrder;

/**
* @var FlushCacheByProductIds
*/
Expand All @@ -31,11 +32,6 @@ class CacheFlush
*/
private $getCategoryIdsByProductIds;

/**
* @var GetSalableStatuses
*/
private $getSalableStatuses;

/**
* @var FlushCacheByCategoryIds
*/
Expand All @@ -44,62 +40,73 @@ class CacheFlush
/**
* @var GetProductsIdsToProcess
*/
private $getProductsIdsToProcess;
private GetProductsIdsToProcess $getProductsIdsToProcess;

/**
* @var IndexerRegistry
*/
private $indexerRegistry;
private IndexerRegistry $indexerRegistry;

/**
* @param FlushCacheByProductIds $flushCacheByIds
* @param GetCategoryIdsByProductIds $getCategoryIdsByProductIds
* @param FlushCacheByCategoryIds $flushCategoryByCategoryIds
* @param GetSalableStatuses $getSalableStatuses
* @param GetProductsIdsToProcess $getProductsIdsToProcess
* @param IndexerRegistry $indexerRegistry
* @param int $sortOrder
*/
public function __construct(
FlushCacheByProductIds $flushCacheByIds,
GetCategoryIdsByProductIds $getCategoryIdsByProductIds,
FlushCacheByCategoryIds $flushCategoryByCategoryIds,
GetSalableStatuses $getSalableStatuses,
GetProductsIdsToProcess $getProductsIdsToProcess,
IndexerRegistry $indexerRegistry
IndexerRegistry $indexerRegistry,
int $sortOrder = 30
) {
$this->flushCacheByIds = $flushCacheByIds;
$this->getCategoryIdsByProductIds = $getCategoryIdsByProductIds;
$this->flushCategoryByCategoryIds = $flushCategoryByCategoryIds;
$this->getSalableStatuses = $getSalableStatuses;
$this->getProductsIdsToProcess = $getProductsIdsToProcess;
$this->indexerRegistry = $indexerRegistry;
$this->sortOrder = $sortOrder;
}

/**
* Clean cache for specific products after source items reindex.
*
* @param Sync $subject
* @param callable $proceed
* @param array $sourceItemIds
* @param array $saleableStatusesBeforeSync
* @param array $saleableStatusesAfterSync
* @return void
* @throws \Exception in case catalog product entity type hasn't been initialized.
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundExecuteList(Sync $subject, callable $proceed, array $sourceItemIds) : void
{
$beforeSalableList = $this->getSalableStatuses->execute($sourceItemIds);
$proceed($sourceItemIds);
$afterSalableList = $this->getSalableStatuses->execute($sourceItemIds);
public function process(
array $sourceItemIds,
array $saleableStatusesBeforeSync,
array $saleableStatusesAfterSync
): void {
$forceDefaultProcessing = !$this->indexerRegistry->get(InventoryIndexer::INDEXER_ID)->isScheduled();

$productsIdsToFlush = $this->getProductsIdsToProcess->execute(
$beforeSalableList,
$afterSalableList,
$saleableStatusesBeforeSync,
$saleableStatusesAfterSync,
$forceDefaultProcessing
);

if (!empty($productsIdsToFlush)) {
$categoryIds = $this->getCategoryIdsByProductIds->execute($productsIdsToFlush);
$this->flushCacheByIds->execute($productsIdsToFlush);
$this->flushCategoryByCategoryIds->execute($categoryIds);
}
}

/**
* @inheritdoc
*
* @return int
*/
public function getSortOrder(): int
{
return $this->sortOrder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
*/
declare(strict_types=1);

namespace Magento\InventoryCache\Test\Unit\Plugin\InventoryIndexer\Indexer\SourceItem\Strategy\Sync;
namespace Magento\InventoryCache\Test\Unit\Model;

use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\InventoryCache\Plugin\InventoryIndexer\Indexer\SourceItem\Strategy\Sync\CacheFlush;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\InventoryCache\Model\CacheFlushProcessor;
use Magento\InventoryCache\Model\FlushCacheByCategoryIds;
use Magento\InventoryCache\Model\FlushCacheByProductIds;
use Magento\InventoryIndexer\Model\ResourceModel\GetCategoryIdsByProductIds;
use Magento\InventoryIndexer\Model\GetProductsIdsToProcess;
use Magento\InventoryIndexer\Indexer\SourceItem\GetSalableStatuses;
use Magento\InventoryIndexer\Indexer\SourceItem\Strategy\Sync;
use PHPUnit\Framework\TestCase;
use Magento\InventoryIndexer\Model\ResourceModel\GetCategoryIdsByProductIds;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class CacheFlushTest extends TestCase
class CacheFlushProcessorTest extends TestCase
{
/**
* @var CacheFlush
*/
private $cacheFlush;
private $cacheFlushProcessor;

/**
* @var FlushCacheByProductIds|MockObject
Expand All @@ -41,26 +39,11 @@ class CacheFlushTest extends TestCase
*/
private $flushCategoryByCategoryIds;

/**
* @var GetSalableStatuses|MockObject
*/
private $getSalableStatuses;

/**
* @var GetProductsIdsToProcess|MockObject
*/
private $getProductsIdsToProcess;

/**
* @var Sync|MockObject
*/
private $sync;

/**
* @var \Callable|MockObject
*/
private $proceedMock;

/**
* @var IndexerRegistry|MockObject
*/
Expand All @@ -71,16 +54,8 @@ class CacheFlushTest extends TestCase
*/
private $indexer;

/**
* @var bool
*/
private $isProceedMockCalled = false;

protected function setUp(): void
{
$this->proceedMock = function () {
$this->isProceedMockCalled = true;
};
$this->flushCacheByIds = $this->getMockBuilder(FlushCacheByProductIds::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -90,40 +65,33 @@ protected function setUp(): void
$this->flushCategoryByCategoryIds = $this->getMockBuilder(FlushCacheByCategoryIds::class)
->disableOriginalConstructor()
->getMock();
$this->getSalableStatuses = $this->getMockBuilder(GetSalableStatuses::class)
->disableOriginalConstructor()
->getMock();
$this->getProductsIdsToProcess = $this->getMockBuilder(GetProductsIdsToProcess::class)
->disableOriginalConstructor()
->getMock();
$this->sync = $this->getMockBuilder(Sync::class)
->disableOriginalConstructor()
->getMock();
$this->indexer = $this->getMockBuilder(IndexerInterface::class)
->getMockForAbstractClass();
$this->indexerRegistry = $this->getMockBuilder(IndexerRegistry::class)
->disableOriginalConstructor()
->getMock();
$this->cacheFlush = new CacheFlush(
$this->cacheFlushProcessor = new CacheFlushProcessor(
$this->flushCacheByIds,
$this->getCategoryIdsByProductIds,
$this->flushCategoryByCategoryIds,
$this->getSalableStatuses,
$this->getProductsIdsToProcess,
$this->indexerRegistry
);
}

/**
* @dataProvider executeListDataProvider
* @dataProvider processDataProvider
* @param array $sourceItemIds
* @param array $beforeSalableList
* @param array $afterSalableList
* @param array $changedProductIds,
* @param int $numberOfCacheCleans,
* @return void
*/
public function testAroundExecuteList(
public function testProcess(
array $sourceItemIds,
array $beforeSalableList,
array $afterSalableList,
Expand All @@ -133,16 +101,9 @@ public function testAroundExecuteList(
$this->indexerRegistry->expects($this->once())
->method('get')
->willReturn($this->indexer);
$this->indexer->expects($this->any())
$this->indexer->expects($this->once())
->method('isScheduled')
->willReturn(true);
$this->getSalableStatuses->expects($this->exactly(2))
->method('execute')
->with($sourceItemIds)
->willReturnOnConsecutiveCalls(
$beforeSalableList,
$afterSalableList
);
$this->getProductsIdsToProcess->expects($this->once())
->method('execute')
->with($beforeSalableList, $afterSalableList)
Expand All @@ -158,15 +119,13 @@ public function testAroundExecuteList(
$this->flushCategoryByCategoryIds->expects($this->exactly($numberOfCacheCleans))
->method('execute');

$this->cacheFlush->aroundExecuteList($this->sync, $this->proceedMock, $sourceItemIds);
$this->assertTrue($this->isProceedMockCalled);
$this->cacheFlushProcessor->process($sourceItemIds, $beforeSalableList, $afterSalableList);
}

/**
* Data provider for testAroundExecuteList
* @return array
*/
public function executeListDataProvider(): array
public function processDataProvider(): array
{
return [
[[1], ['sku1' => [1 => true]], ['sku1' => [1 => true]], [], 0],
Expand Down
Loading

0 comments on commit 412ba39

Please sign in to comment.