Skip to content

Commit c1a8ead

Browse files
committed
Move additional dependencies from private getters to constructor - Magento_PageCache
1 parent 2268edc commit c1a8ead

File tree

4 files changed

+187
-148
lines changed

4 files changed

+187
-148
lines changed
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
declare(strict_types=1);
7+
78
namespace Magento\PageCache\Observer;
89

9-
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\App\PageCache\Cache;
11+
use Magento\Framework\Event\Observer;
1012
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\PageCache\Model\Cache\Type;
14+
use Magento\PageCache\Model\Config;
1115

16+
/**
17+
* Observer used to flush all caches with built-in full page cache
18+
*/
1219
class FlushAllCache implements ObserverInterface
1320
{
1421
/**
15-
* @var \Magento\Framework\App\PageCache\Cache
22+
* @var Cache
1623
*
1724
* @deprecated 100.1.0
1825
*/
@@ -21,48 +28,42 @@ class FlushAllCache implements ObserverInterface
2128
/**
2229
* Application config object
2330
*
24-
* @var \Magento\PageCache\Model\Config
31+
* @var Config
2532
*/
2633
protected $_config;
2734

2835
/**
29-
* @var \Magento\PageCache\Model\Cache\Type
36+
* @var Type
3037
*/
3138
private $fullPageCache;
3239

3340
/**
34-
* @param \Magento\PageCache\Model\Config $config
35-
* @param \Magento\Framework\App\PageCache\Cache $cache
41+
* @param Config $config
42+
* @param Cache $cache
43+
* @param Type $fullPageCache
3644
*/
37-
public function __construct(\Magento\PageCache\Model\Config $config, \Magento\Framework\App\PageCache\Cache $cache)
38-
{
45+
public function __construct(
46+
Config $config,
47+
Cache $cache,
48+
Type $fullPageCache
49+
) {
3950
$this->_config = $config;
4051
$this->_cache = $cache;
52+
$this->fullPageCache = $fullPageCache;
4153
}
4254

4355
/**
4456
* Flash Built-In cache
45-
* @param \Magento\Framework\Event\Observer $observer
57+
*
58+
* @param Observer $observer
59+
*
4660
* @return void
4761
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4862
*/
49-
public function execute(\Magento\Framework\Event\Observer $observer)
50-
{
51-
if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN) {
52-
$this->getCache()->clean();
53-
}
54-
}
55-
56-
/**
57-
* TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547
58-
*
59-
* @return \Magento\PageCache\Model\Cache\Type
60-
*/
61-
private function getCache()
63+
public function execute(Observer $observer)
6264
{
63-
if (!$this->fullPageCache) {
64-
$this->fullPageCache = ObjectManager::getInstance()->get(\Magento\PageCache\Model\Cache\Type::class);
65+
if ($this->_config->getType() == Config::BUILT_IN) {
66+
$this->fullPageCache->clean();
6567
}
66-
return $this->fullPageCache;
6768
}
6869
}
Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
declare(strict_types=1);
7+
78
namespace Magento\PageCache\Observer;
89

9-
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\App\Cache\Tag\Resolver;
11+
use Magento\Framework\App\PageCache\Cache;
12+
use Magento\Framework\Event\Observer;
1013
use Magento\Framework\Event\ObserverInterface;
14+
use Magento\PageCache\Model\Cache\Type;
15+
use Magento\PageCache\Model\Config;
16+
use Zend_Cache;
1117

18+
/**
19+
* Observer used to cache by tags when using built-in full page cache
20+
*/
1221
class FlushCacheByTags implements ObserverInterface
1322
{
1423
/**
15-
* @var \Magento\Framework\App\PageCache\Cache
24+
* @var Cache
1625
*
1726
* @deprecated 100.1.0
1827
*/
@@ -21,78 +30,59 @@ class FlushCacheByTags implements ObserverInterface
2130
/**
2231
* Application config object
2332
*
24-
* @var \Magento\PageCache\Model\Config
33+
* @var Config
2534
*/
2635
protected $_config;
2736

2837
/**
29-
* @var \Magento\PageCache\Model\Cache\Type
38+
* @var Type
3039
*/
3140
private $fullPageCache;
3241

3342
/**
3443
* Invalidation tags resolver
3544
*
36-
* @var \Magento\Framework\App\Cache\Tag\Resolver
45+
* @var Resolver
3746
*/
3847
private $tagResolver;
3948

4049
/**
41-
* @param \Magento\PageCache\Model\Config $config
42-
* @param \Magento\Framework\App\PageCache\Cache $cache
50+
* @param Config $config
51+
* @param Cache $cache
52+
* @param Type $fullPageCache
53+
* @param Resolver $tagResolver
4354
*/
44-
public function __construct(\Magento\PageCache\Model\Config $config, \Magento\Framework\App\PageCache\Cache $cache)
45-
{
55+
public function __construct(
56+
Config $config,
57+
Cache $cache,
58+
Type $fullPageCache,
59+
Resolver $tagResolver
60+
) {
4661
$this->_config = $config;
4762
$this->_cache = $cache;
63+
$this->fullPageCache = $fullPageCache;
64+
$this->tagResolver = $tagResolver;
4865
}
4966

5067
/**
51-
* If Built-In caching is enabled it collects array of tags
52-
* of incoming object and asks to clean cache.
68+
* If Built-In caching is enabled it collects array of tags of incoming object and asks to clean cache.
69+
*
70+
* @param Observer $observer
5371
*
54-
* @param \Magento\Framework\Event\Observer $observer
5572
* @return void
5673
*/
57-
public function execute(\Magento\Framework\Event\Observer $observer)
74+
public function execute(Observer $observer)
5875
{
59-
if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->_config->isEnabled()) {
76+
if ($this->_config->getType() == Config::BUILT_IN && $this->_config->isEnabled()) {
6077
$object = $observer->getEvent()->getObject();
6178
if (!is_object($object)) {
6279
return;
6380
}
64-
$tags = $this->getTagResolver()->getTags($object);
81+
$tags = $this->tagResolver->getTags($object);
6582

6683
if (!empty($tags)) {
67-
$this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
84+
$this->fullPageCache->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
6885
}
6986
}
7087
}
71-
72-
/**
73-
* TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547
74-
*
75-
*
76-
* @return \Magento\PageCache\Model\Cache\Type
77-
*/
78-
private function getCache()
79-
{
80-
if (!$this->fullPageCache) {
81-
$this->fullPageCache = ObjectManager::getInstance()->get(\Magento\PageCache\Model\Cache\Type::class);
82-
}
83-
return $this->fullPageCache;
84-
}
85-
86-
/**
87-
* @deprecated 100.1.2
88-
* @return \Magento\Framework\App\Cache\Tag\Resolver
89-
*/
90-
private function getTagResolver()
91-
{
92-
if ($this->tagResolver === null) {
93-
$this->tagResolver = \Magento\Framework\App\ObjectManager::getInstance()
94-
->get(\Magento\Framework\App\Cache\Tag\Resolver::class);
95-
}
96-
return $this->tagResolver;
97-
}
9888
}
Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,94 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76

87
namespace Magento\PageCache\Test\Unit\Observer;
98

10-
class FlushAllCacheTest extends \PHPUnit\Framework\TestCase
11-
{
12-
/** @var \Magento\PageCache\Observer\FlushAllCache */
13-
private $_model;
9+
use Magento\Framework\Event\Observer;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\PageCache\Model\Cache\Type;
12+
use Magento\PageCache\Model\Config;
13+
use Magento\PageCache\Observer\FlushAllCache;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
1416

15-
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\PageCache\Model\Config */
16-
private $_configMock;
17+
/**
18+
* Test class for \Magento\PageCache\Observer\FlushAllCache
19+
*/
20+
class FlushAllCacheTest extends TestCase
21+
{
22+
/**
23+
* @var FlushAllCache
24+
*/
25+
private $model;
1726

18-
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\PageCache\Cache */
19-
private $_cacheMock;
27+
/**
28+
* @var Config|MockObject
29+
*/
30+
private $configMock;
2031

21-
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\Observer */
32+
/**
33+
* @var Observer|MockObject
34+
*/
2235
private $observerMock;
2336

24-
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\PageCache\Model\Cache\Type */
37+
/**
38+
* @var Type|MockObject
39+
*/
2540
private $fullPageCacheMock;
2641

2742
/**
28-
* Set up all mocks and data for test
43+
* @inheritDoc
2944
*/
3045
protected function setUp()
3146
{
32-
$this->_configMock = $this->createPartialMock(\Magento\PageCache\Model\Config::class, ['getType', 'isEnabled']);
33-
$this->_cacheMock = $this->createPartialMock(\Magento\Framework\App\PageCache\Cache::class, ['clean']);
34-
$this->fullPageCacheMock = $this->createPartialMock(\Magento\PageCache\Model\Cache\Type::class, ['clean']);
35-
$this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
47+
$this->configMock = $this->createPartialMock(Config::class, ['getType', 'isEnabled']);
48+
$this->fullPageCacheMock = $this->createPartialMock(Type::class, ['clean']);
49+
$this->observerMock = $this->createMock(Observer::class);
3650

37-
$this->_model = new \Magento\PageCache\Observer\FlushAllCache(
38-
$this->_configMock,
39-
$this->_cacheMock
51+
$objectManager = new ObjectManager($this);
52+
$this->model = $objectManager->getObject(
53+
FlushAllCache::class,
54+
[
55+
'config' => $this->configMock,
56+
'fullPageCache' => $this->fullPageCacheMock
57+
]
4058
);
41-
42-
$reflection = new \ReflectionClass(\Magento\PageCache\Observer\FlushAllCache::class);
43-
$reflectionProperty = $reflection->getProperty('fullPageCache');
44-
$reflectionProperty->setAccessible(true);
45-
$reflectionProperty->setValue($this->_model, $this->fullPageCacheMock);
4659
}
4760

4861
/**
4962
* Test case for flushing all the cache
5063
*/
5164
public function testExecute()
5265
{
53-
$this->_configMock->expects(
66+
$this->configMock->expects(
5467
$this->once()
5568
)->method(
5669
'getType'
57-
)->will(
58-
$this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN)
70+
)->willReturn(
71+
Config::BUILT_IN
5972
);
6073

6174
$this->fullPageCacheMock->expects($this->once())->method('clean');
62-
$this->_model->execute($this->observerMock);
75+
$this->model->execute($this->observerMock);
76+
}
77+
78+
/**
79+
* Test case for flushing all the cache with varnish enabled
80+
*/
81+
public function testExecuteWithVarnish()
82+
{
83+
$this->configMock->expects(
84+
$this->once()
85+
)->method(
86+
'getType'
87+
)->willReturn(
88+
Config::VARNISH
89+
);
90+
91+
$this->fullPageCacheMock->expects($this->never())->method('clean');
92+
$this->model->execute($this->observerMock);
6393
}
6494
}

0 commit comments

Comments
 (0)