Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CacheTest.php
Go to the documentation of this file.
1 <?php
7 
8 class CacheTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_model;
14 
18  protected $_cacheTypeMocks;
19 
24 
25  protected function setUp()
26  {
27  $this->_initCacheTypeMocks();
28 
29  $this->_cacheFrontendMock = $this->getMockForAbstractClass(
30  \Magento\Framework\Cache\FrontendInterface::class,
31  [],
32  '',
33  true,
34  true,
35  true,
36  ['clean']
37  );
38 
39  $frontendPoolMock = $this->createMock(\Magento\Framework\App\Cache\Frontend\Pool::class);
40  $frontendPoolMock->expects($this->any())->method('valid')->will($this->onConsecutiveCalls(true, false));
41 
42  $frontendPoolMock->expects(
43  $this->any()
44  )->method(
45  'current'
46  )->will(
47  $this->returnValue($this->_cacheFrontendMock)
48  );
49  $frontendPoolMock->expects(
50  $this->any()
51  )->method(
52  'get'
53  )->with(
54  \Magento\Framework\App\Cache\Frontend\Pool::DEFAULT_FRONTEND_ID
55  )->will(
56  $this->returnValue($this->_cacheFrontendMock)
57  );
58 
59  $this->_model = new \Magento\Framework\App\Cache($frontendPoolMock);
60  }
61 
65  protected function _initCacheTypeMocks()
66  {
67  $cacheTypes = [
68  \Magento\Framework\Cache\Frontend\Decorator\TagScope::class,
69  \Magento\Framework\Cache\Frontend\Decorator\Bare::class,
70  ];
71  foreach ($cacheTypes as $type) {
72  $this->_cacheTypeMocks[$type] = $this->getMockBuilder($type)
73  ->setMethods(['clean'])
74  ->setConstructorArgs(
75  [
76  $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class), '
77  FIXTURE_TAG'
78  ]
79  )
80  ->getMock();
81  }
82  }
83 
90  public function getTypeMock($type)
91  {
92  return $this->_cacheTypeMocks[$type];
93  }
94 
95  protected function tearDown()
96  {
97  $this->_cacheTypeMocks = [];
98  $this->_cacheFrontendMock = null;
99  $this->_model = null;
100  }
101 
102  public function testConstructor()
103  {
104  $this->assertSame($this->_cacheFrontendMock, $this->_model->getFrontend());
105  }
106 
107  public function testGetFrontend()
108  {
109  $frontend = $this->_model->getFrontend();
110  $this->assertSame($this->_cacheFrontendMock, $frontend);
111  }
112 
113  public function testLoad()
114  {
115  $this->_cacheFrontendMock->expects(
116  $this->once()
117  )->method(
118  'load'
119  )->with(
120  'test_id'
121  )->will(
122  $this->returnValue('test_data')
123  );
124  $this->assertEquals('test_data', $this->_model->load('test_id'));
125  }
126 
136  public function testSave($inputData, $inputId, $inputTags, $expectedData, $expectedId, $expectedTags)
137  {
138  $this->_cacheFrontendMock->expects(
139  $this->once()
140  )->method(
141  'save'
142  )->with(
143  $this->identicalTo($expectedData),
144  $expectedId,
145  $expectedTags
146  );
147  $this->_model->save($inputData, $inputId, $inputTags);
148  }
149 
153  public function saveDataProvider()
154  {
156  return [
157  'default tags' => ['test_data', 'test_id', [], 'test_data', 'test_id', []],
158  'config tags' => [
159  'test_data',
160  'test_id',
161  [$configTag],
162  'test_data',
163  'test_id',
164  [$configTag],
165  ],
166  'lowercase tags' => [
167  'test_data',
168  'test_id',
169  ['test_tag'],
170  'test_data',
171  'test_id',
172  ['test_tag'],
173  ],
174  'non-string data' => [1234567890, 'test_id', [], '1234567890', 'test_id', []]
175  ];
176  }
177 
182  public function testRemove($result)
183  {
184  $this->_cacheFrontendMock->expects(
185  $this->once()
186  )->method(
187  'remove'
188  )->with(
189  'test_id'
190  )->will(
191  $this->returnValue($result)
192  );
193  $this->assertEquals($result, $this->_model->remove('test_id'));
194  }
195 
199  public function successFailureDataProvider()
200  {
201  return ['success' => [true], 'failure' => [false]];
202  }
203 
204  public function testCleanByTags()
205  {
206  $expectedTags = ['test_tag'];
207  $this->_cacheFrontendMock->expects(
208  $this->once()
209  )->method(
210  'clean'
211  )->with(
213  $expectedTags
214  )->will(
215  $this->returnValue(true)
216  );
217  $this->assertTrue($this->_model->clean($expectedTags));
218  }
219 
220  public function testCleanByEmptyTags()
221  {
222  $this->_cacheFrontendMock->expects(
223  $this->once()
224  )->method(
225  'clean'
226  )->with(
228  )->will(
229  $this->returnValue(true)
230  );
231  $this->assertTrue($this->_model->clean());
232  }
233 }
testSave($inputData, $inputId, $inputTags, $expectedData, $expectedId, $expectedTags)
Definition: CacheTest.php:136
$type
Definition: item.phtml:13
const CLEANING_MODE_ALL
Definition: Cache.php:72
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76