1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App\Test\Unit\Cache;
use \Magento\Framework\App\Cache\Manager;
class ManagerTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\TypeListInterface
*/
private $cacheTypeList;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\StateInterface
*/
private $cacheState;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Console\Response
*/
private $response;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\Type\FrontendPool
*/
private $frontendPool;
/**
* @var Manager
*/
private $model;
protected function setUp()
{
$this->cacheTypeList = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\TypeListInterface::class);
$this->cacheState = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\StateInterface::class);
$this->response = $this->createMock(\Magento\Framework\App\Console\Response::class);
$this->frontendPool = $this->createMock(\Magento\Framework\App\Cache\Type\FrontendPool::class);
$this->model = new Manager($this->cacheTypeList, $this->cacheState, $this->frontendPool);
}
public function testEmptyRequest()
{
$this->cacheState->expects($this->never())->method('setEnabled');
$this->cacheState->expects($this->never())->method('persist');
$this->frontendPool->expects($this->never())->method('get');
$this->model->setEnabled([], true);
}
/**
* Test setting all cache types to true
*
* In this fixture, there are 2 of 3 cache types disabled, but will be enabled
* so persist() should be invoked once, then clean() for each of those which changed
*/
public function testSetEnabledTrueAll()
{
$caches = ['foo', 'bar', 'baz'];
$cacheStatusMap = [['foo', true], ['bar', false], ['baz', false]];
$this->cacheState->expects($this->exactly(3))
->method('isEnabled')
->will($this->returnValueMap($cacheStatusMap));
$this->cacheState->expects($this->exactly(2))
->method('setEnabled')
->will($this->returnValueMap([['bar', true], ['baz', true]]));
$this->cacheState->expects($this->once())->method('persist');
$this->assertEquals(['bar', 'baz'], $this->model->setEnabled($caches, true));
}
/**
* Test setting all cache types to true
*
* Fixture is the same as in previous test, but here the intent to disable all of them.
* Since only one of them is enabled, the setter should be invoked only once.
* Also the operation of deactivating cache does not need cleanup (it is relevant when you enable it).
*/
public function testSetEnabledFalseAll()
{
$caches = ['foo', 'bar', 'baz'];
$cacheStatusMap = [['foo', true], ['bar', false], ['baz', false]];
$this->cacheState->expects($this->exactly(3))
->method('isEnabled')
->will($this->returnValueMap($cacheStatusMap));
$this->cacheState->expects($this->once())->method('setEnabled')->with('foo', false);
$this->cacheState->expects($this->once())->method('persist');
$this->frontendPool->expects($this->never())->method('get');
$this->assertEquals(['foo'], $this->model->setEnabled($caches, false));
}
/**
* Test flushing all cache types
*
* Emulates situation when some cache frontends reuse the same backend
* Asserts that the flush is invoked only once per affected storage
*/
public function testFlushAll()
{
$cacheTypes = ['foo', 'bar', 'baz'];
$frontendFoo = $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class);
$frontendBar = $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class);
$frontendBaz = $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class);
$this->frontendPool->expects($this->exactly(3))->method('get')->will($this->returnValueMap([
['foo', $frontendFoo],
['bar', $frontendBar],
['baz', $frontendBaz],
]));
$backendOne = $this->getMockForAbstractClass(\Zend_Cache_Backend_Interface::class);
$backendTwo = $this->getMockForAbstractClass(\Zend_Cache_Backend_Interface::class);
$frontendFoo->expects($this->once())->method('getBackend')->willReturn($backendOne);
$frontendBar->expects($this->once())->method('getBackend')->willReturn($backendOne);
$frontendBaz->expects($this->once())->method('getBackend')->willReturn($backendTwo);
$backendOne->expects($this->once())->method('clean');
$backendTwo->expects($this->once())->method('clean');
$this->model->flush($cacheTypes);
}
public function testGetStatus()
{
$types = [
['id' => 'foo', 'status' => true],
['id' => 'bar', 'status' => false],
];
$this->cacheTypeList->expects($this->once())->method('getTypes')->willReturn($types);
$this->assertSame(['foo' => true, 'bar' => false], $this->model->getStatus());
}
public function testGetAvailableTypes()
{
$types = [
['id' => 'foo', 'status' => true],
['id' => 'bar', 'status' => false],
];
$this->cacheTypeList->expects($this->once())->method('getTypes')->willReturn($types);
$this->assertSame(['foo', 'bar'], $this->model->getAvailableTypes());
}
}