Skip to content

Commit

Permalink
fix clean_cache plugin flush mode
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Apr 15, 2019
1 parent de50d12 commit fcea053
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 60 deletions.
63 changes: 31 additions & 32 deletions lib/internal/Magento/Framework/App/Cache/FlushCacheByTags.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\App\Cache;

use Magento\Framework\App\Cache\Tag\Resolver;
use Magento\Framework\App\Cache\Type\FrontendPool;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\AbstractResource;

/**
* Automatic cache cleaner plugin
*/
class FlushCacheByTags
{
/**
* @var Type\FrontendPool
* @var FrontendPool
*/
private $cachePool;

Expand All @@ -27,23 +33,21 @@ class FlushCacheByTags
private $cacheState;

/**
* @var Tag\Resolver
* @var Resolver
*/
private $tagResolver;

/**
* FlushCacheByTags constructor.
*
* @param Type\FrontendPool $cachePool
* @param FrontendPool $cachePool
* @param StateInterface $cacheState
* @param array $cacheList
* @param Tag\Resolver $tagResolver
* @param string[] $cacheList
* @param Resolver $tagResolver
*/
public function __construct(
\Magento\Framework\App\Cache\Type\FrontendPool $cachePool,
\Magento\Framework\App\Cache\StateInterface $cacheState,
FrontendPool $cachePool,
StateInterface $cacheState,
array $cacheList,
\Magento\Framework\App\Cache\Tag\Resolver $tagResolver
Resolver $tagResolver
) {
$this->cachePool = $cachePool;
$this->cacheState = $cacheState;
Expand All @@ -54,17 +58,14 @@ public function __construct(
/**
* Clean cache on save object
*
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $subject
* @param AbstractResource $subject
* @param \Closure $proceed
* @param \Magento\Framework\Model\AbstractModel $object
* @return \Magento\Framework\Model\ResourceModel\AbstractResource
* @param AbstractModel $object
* @return AbstractResource
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundSave(
\Magento\Framework\Model\ResourceModel\AbstractResource $subject,
\Closure $proceed,
\Magento\Framework\Model\AbstractModel $object
) {
public function aroundSave(AbstractResource $subject, \Closure $proceed, AbstractModel $object): AbstractResource
{
$result = $proceed($object);
$tags = $this->tagResolver->getTags($object);
$this->cleanCacheByTags($tags);
Expand All @@ -75,39 +76,37 @@ public function aroundSave(
/**
* Clean cache on delete object
*
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $subject
* @param AbstractResource $subject
* @param \Closure $proceed
* @param \Magento\Framework\Model\AbstractModel $object
* @return \Magento\Framework\Model\ResourceModel\AbstractResource
* @param AbstractModel $object
* @return AbstractResource
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundDelete(
\Magento\Framework\Model\ResourceModel\AbstractResource $subject,
\Closure $proceed,
\Magento\Framework\Model\AbstractModel $object
) {
public function aroundDelete(AbstractResource $subject, \Closure $proceed, AbstractModel $object): AbstractResource
{
$tags = $this->tagResolver->getTags($object);
$result = $proceed($object);
$this->cleanCacheByTags($tags);

return $result;
}

/**
* Clean cache by tags
*
* @param string[] $tags
* @param string[] $tags
* @return void
*/
private function cleanCacheByTags($tags)
private function cleanCacheByTags(array $tags): void
{
if (empty($tags)) {
if (!$tags) {
return;
}
foreach ($this->cacheList as $cacheType) {
if ($this->cacheState->isEnabled($cacheType)) {
$this->cachePool->get($cacheType)->clean(
\Zend_Cache::CLEANING_MODE_MATCHING_TAG,
array_unique($tags)
\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
\array_unique($tags)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\App\Test\Unit\Cache;

use Magento\Framework\App\Cache\FlushCacheByTags;
use Magento\Framework\App\Cache\StateInterface;
use Magento\Framework\App\Cache\Tag\Resolver;
use Magento\Framework\App\Cache\Type\FrontendPool;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\AbstractResource;

/**
* Unit tests for the \Magento\Framework\App\Cache\FlushCacheByTags class.
*/
class FlushCacheByTagsTest extends \PHPUnit\Framework\TestCase
{
/**
Expand All @@ -28,72 +39,68 @@ class FlushCacheByTagsTest extends \PHPUnit\Framework\TestCase
*/
private $plugin;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->cacheState = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\StateInterface::class);
$this->frontendPool = $this->createMock(\Magento\Framework\App\Cache\Type\FrontendPool::class);
$this->tagResolver = $this->createMock(\Magento\Framework\App\Cache\Tag\Resolver::class);
$this->cacheState = $this->getMockForAbstractClass(StateInterface::class);
$this->frontendPool = $this->createMock(FrontendPool::class);
$this->tagResolver = $this->createMock(Resolver::class);

$this->plugin = new \Magento\Framework\App\Cache\FlushCacheByTags(
$this->plugin = new FlushCacheByTags(
$this->frontendPool,
$this->cacheState,
['test'],
$this->tagResolver
);
}

public function testAroundSave()
/**
* @return void
*/
public function testAroundSave(): void
{
$resource = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
$resource = $this->getMockBuilder(AbstractResource::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$model = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
$model = $this->getMockBuilder(AbstractModel::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->tagResolver->expects($this->atLeastOnce())->method('getTags')->with($model)->willReturn([]);

$result = $this->plugin->aroundSave(
$resource,
function () use ($resource) {
return $resource;
},
$model
);

$this->assertSame($resource, $result);
}

public function testAroundDelete()
/**
* @return void
*/
public function testAroundDelete(): void
{
$resource = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
$resource = $this->getMockBuilder(AbstractResource::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$model = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
$model = $this->getMockBuilder(AbstractModel::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->tagResolver->expects($this->atLeastOnce())->method('getTags')->with($model)->willReturn([]);

$result = $this->plugin->aroundDelete(
$resource,
function () use ($resource) {
return $resource;
},
$model
);
$this->assertSame($resource, $result);
}

public function testAroundSaveWithInterface()
{
$resource = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)

->disableOriginalConstructor()
->getMockForAbstractClass();
$model = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$result = $this->plugin->aroundSave(
$resource,
function () use ($resource) {
return $resource;
},
$model
);
$this->assertSame($resource, $result);
}
}

0 comments on commit fcea053

Please sign in to comment.