Skip to content

Commit b6d7223

Browse files
committed
ACP2E-1003: Inventory indexer is cleaning all caches in scheduled mode
1 parent eec0348 commit b6d7223

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

InventoryCache/Model/FlushCacheByCacheTag.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public function execute(string $cacheTag, array $entityIds): void
5959
$cacheContext = $this->cacheContextFactory->create();
6060
$cacheContext->registerEntities($cacheTag, $entityIds);
6161
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $cacheContext]);
62-
$this->appCache->clean($cacheContext->getIdentities());
62+
$tags = $cacheContext->getIdentities();
63+
if ($tags) {
64+
$this->appCache->clean($tags);
65+
}
6366
}
6467
}
6568
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventoryCache\Unit\Model;
9+
10+
use Magento\Framework\App\CacheInterface;
11+
use Magento\Framework\EntityManager\EventManager;
12+
use Magento\Framework\Indexer\CacheContext;
13+
use Magento\Framework\Indexer\CacheContextFactory;
14+
use Magento\InventoryCache\Model\FlushCacheByCacheTag;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class FlushCacheByCacheTagTest extends TestCase
19+
{
20+
/**
21+
* @var CacheContextFactory|MockObject
22+
*/
23+
private $cacheContextFactory;
24+
25+
/**
26+
* @var EventManager|MockObject
27+
*/
28+
private $eventManager;
29+
30+
/**
31+
* @var CacheInterface|MockObject
32+
*/
33+
private $appCache;
34+
35+
/**
36+
* @var FlushCacheByCacheTag
37+
*/
38+
private $model;
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function setUp(): void
44+
{
45+
parent::setUp();
46+
$this->cacheContextFactory = $this->createMock(CacheContextFactory::class);
47+
$this->eventManager = $this->createMock(EventManager::class);
48+
$this->appCache = $this->createMock(CacheInterface::class);
49+
$this->model = new FlushCacheByCacheTag(
50+
$this->cacheContextFactory,
51+
$this->eventManager,
52+
$this->appCache
53+
);
54+
}
55+
56+
/**
57+
* Checks that cache is not cleaned with empty tags which cleans all caches
58+
*
59+
* @return void
60+
*/
61+
public function testExecuteWithEmptyEntityIds(): void
62+
{
63+
$this->cacheContextFactory->expects($this->never())->method('create');
64+
$this->eventManager->expects($this->never())->method('dispatch');
65+
$this->appCache->expects($this->never())->method('clean');
66+
$this->model->execute('test', []);
67+
}
68+
69+
/**
70+
* Checks that cache is not cleaned with empty tags which cleans all caches
71+
*
72+
* @return void
73+
*/
74+
public function testExecuteWithDeferredCacheContext(): void
75+
{
76+
$cacheContextMock = $this->createMock(CacheContext::class);
77+
$cacheContextMock->method('getIdentities')
78+
->willReturn([]);
79+
$this->cacheContextFactory->expects($this->once())
80+
->method('create')
81+
->willReturn($cacheContextMock);
82+
$this->appCache->expects($this->never())->method('clean');
83+
$this->model->execute('test', ['1', '2']);
84+
}
85+
}

0 commit comments

Comments
 (0)