Skip to content

Commit

Permalink
ACP2E-1003: Inventory indexer is cleaning all caches in scheduled mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bubasuma committed Jul 18, 2022
1 parent eec0348 commit b6d7223
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
5 changes: 4 additions & 1 deletion InventoryCache/Model/FlushCacheByCacheTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public function execute(string $cacheTag, array $entityIds): void
$cacheContext = $this->cacheContextFactory->create();
$cacheContext->registerEntities($cacheTag, $entityIds);
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $cacheContext]);
$this->appCache->clean($cacheContext->getIdentities());
$tags = $cacheContext->getIdentities();
if ($tags) {
$this->appCache->clean($tags);
}
}
}
}
85 changes: 85 additions & 0 deletions InventoryCache/Test/Unit/Model/FlushCacheByCacheTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCache\Unit\Model;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\EntityManager\EventManager;
use Magento\Framework\Indexer\CacheContext;
use Magento\Framework\Indexer\CacheContextFactory;
use Magento\InventoryCache\Model\FlushCacheByCacheTag;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class FlushCacheByCacheTagTest extends TestCase
{
/**
* @var CacheContextFactory|MockObject
*/
private $cacheContextFactory;

/**
* @var EventManager|MockObject
*/
private $eventManager;

/**
* @var CacheInterface|MockObject
*/
private $appCache;

/**
* @var FlushCacheByCacheTag
*/
private $model;

/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->cacheContextFactory = $this->createMock(CacheContextFactory::class);
$this->eventManager = $this->createMock(EventManager::class);
$this->appCache = $this->createMock(CacheInterface::class);
$this->model = new FlushCacheByCacheTag(
$this->cacheContextFactory,
$this->eventManager,
$this->appCache
);
}

/**
* Checks that cache is not cleaned with empty tags which cleans all caches
*
* @return void
*/
public function testExecuteWithEmptyEntityIds(): void
{
$this->cacheContextFactory->expects($this->never())->method('create');
$this->eventManager->expects($this->never())->method('dispatch');
$this->appCache->expects($this->never())->method('clean');
$this->model->execute('test', []);
}

/**
* Checks that cache is not cleaned with empty tags which cleans all caches
*
* @return void
*/
public function testExecuteWithDeferredCacheContext(): void
{
$cacheContextMock = $this->createMock(CacheContext::class);
$cacheContextMock->method('getIdentities')
->willReturn([]);
$this->cacheContextFactory->expects($this->once())
->method('create')
->willReturn($cacheContextMock);
$this->appCache->expects($this->never())->method('clean');
$this->model->execute('test', ['1', '2']);
}
}

0 comments on commit b6d7223

Please sign in to comment.