[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / CacheManagerTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Cache\CacheManager;
18 use TYPO3\CMS\Core\Cache\Exception\InvalidBackendException;
19 use TYPO3\CMS\Core\Cache\Exception\InvalidCacheException;
20 use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException;
21 use TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend;
22 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\BackendConfigurationOptionFixture;
23 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\BackendDefaultFixture;
24 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\BackendFixture;
25 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\BackendInitializeObjectFixture;
26 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendBackendInstanceFixture;
27 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendDefaultFixture;
28 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendFixture;
29 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendIdentifierFixture;
30 use TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendInitializeObjectFixture;
31
32 /**
33 * Testcase for the TYPO3\CMS\Core\Cache\CacheManager
34 *
35 * This file is a backport from FLOW3
36 */
37 class CacheManagerTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
38 {
39 /**
40 * @test
41 */
42 public function managerThrowsExceptionOnCacheRegistrationWithAlreadyExistingIdentifier()
43 {
44 $this->expectException(\TYPO3\CMS\Core\Cache\Exception\DuplicateIdentifierException::class);
45 $this->expectExceptionCode(1203698223);
46
47 $manager = new CacheManager();
48 $cache1 = $this->getMockBuilder(AbstractFrontend::class)
49 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
50 ->disableOriginalConstructor()
51 ->getMock();
52 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
53 $cache2 = $this->getMockBuilder(AbstractFrontend::class)
54 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
55 ->disableOriginalConstructor()
56 ->getMock();
57 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
58 $manager->registerCache($cache1);
59 $manager->registerCache($cache2);
60 }
61
62 /**
63 * @test
64 */
65 public function managerReturnsThePreviouslyRegisteredCache()
66 {
67 $manager = new CacheManager();
68 $cache1 = $this->getMockBuilder(AbstractFrontend::class)
69 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
70 ->disableOriginalConstructor()
71 ->getMock();
72 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
73 $cache2 = $this->getMockBuilder(AbstractFrontend::class)
74 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
75 ->disableOriginalConstructor()
76 ->getMock();
77 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache2'));
78 $manager->registerCache($cache1);
79 $manager->registerCache($cache2);
80 $this->assertSame($cache2, $manager->getCache('cache2'), 'The cache returned by getCache() was not the same I registered.');
81 }
82
83 /**
84 * @test
85 */
86 public function getCacheThrowsExceptionForNonExistingIdentifier()
87 {
88 $this->expectException(\TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException::class);
89 $this->expectExceptionCode(1203699034);
90
91 $manager = new CacheManager();
92 $cache = $this->getMockBuilder(AbstractFrontend::class)
93 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
94 ->disableOriginalConstructor()
95 ->getMock();
96 $cache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('someidentifier'));
97 $manager->registerCache($cache);
98 $manager->getCache('someidentifier');
99 $manager->getCache('doesnotexist');
100 }
101
102 /**
103 * @test
104 */
105 public function hasCacheReturnsCorrectResult()
106 {
107 $manager = new CacheManager();
108 $cache1 = $this->getMockBuilder(AbstractFrontend::class)
109 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
110 ->disableOriginalConstructor()
111 ->getMock();
112 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
113 $manager->registerCache($cache1);
114 $this->assertTrue($manager->hasCache('cache1'), 'hasCache() did not return TRUE.');
115 $this->assertFalse($manager->hasCache('cache2'), 'hasCache() did not return FALSE.');
116 }
117
118 /**
119 * @test
120 */
121 public function flushCachesByTagCallsTheFlushByTagMethodOfAllRegisteredCaches()
122 {
123 $manager = new CacheManager();
124 $cache1 = $this->getMockBuilder(AbstractFrontend::class)
125 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
126 ->disableOriginalConstructor()
127 ->getMock();
128 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
129 $cache1->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
130 $manager->registerCache($cache1);
131 $cache2 = $this->getMockBuilder(AbstractFrontend::class)
132 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
133 ->disableOriginalConstructor()
134 ->getMock();
135 $cache2->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
136 $manager->registerCache($cache2);
137 $manager->flushCachesByTag('theTag');
138 }
139
140 /**
141 * @test
142 */
143 public function flushCachesByTagsCallsTheFlushByTagsMethodOfAllRegisteredCaches()
144 {
145 $manager = new CacheManager();
146 $cache1 = $this->getMockBuilder(AbstractFrontend::class)
147 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTags'])
148 ->disableOriginalConstructor()
149 ->getMock();
150 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
151 $cache1->expects($this->once())->method('flushByTags')->with($this->equalTo(['theTag']));
152 $manager->registerCache($cache1);
153 $cache2 = $this->getMockBuilder(AbstractFrontend::class)
154 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTags'])
155 ->disableOriginalConstructor()
156 ->getMock();
157 $cache2->expects($this->once())->method('flushByTags')->with($this->equalTo(['theTag']));
158 $manager->registerCache($cache2);
159 $manager->flushCachesByTags(['theTag']);
160 }
161
162 /**
163 * @test
164 */
165 public function flushCachesCallsTheFlushMethodOfAllRegisteredCaches()
166 {
167 $manager = new CacheManager();
168 $cache1 = $this->getMockBuilder(AbstractFrontend::class)
169 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
170 ->disableOriginalConstructor()
171 ->getMock();
172 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
173 $cache1->expects($this->once())->method('flush');
174 $manager->registerCache($cache1);
175 $cache2 = $this->getMockBuilder(AbstractFrontend::class)
176 ->setMethods(['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'])
177 ->disableOriginalConstructor()
178 ->getMock();
179 $cache2->expects($this->once())->method('flush');
180 $manager->registerCache($cache2);
181 $manager->flushCaches();
182 }
183
184 /**
185 * @test
186 */
187 public function flushCachesInGroupThrowsExceptionForNonExistingGroup()
188 {
189 $this->expectException(NoSuchCacheGroupException::class);
190 $this->expectExceptionCode(1390334120);
191
192 $manager = new CacheManager();
193 $manager->flushCachesInGroup('nonExistingGroup');
194 }
195
196 /**
197 * @test
198 */
199 public function flushCachesInGroupByTagThrowsExceptionForNonExistingGroup()
200 {
201 $this->expectException(NoSuchCacheGroupException::class);
202 $this->expectExceptionCode(1390334120);
203
204 $manager = new CacheManager();
205 $manager->flushCachesInGroup('nonExistingGroup');
206 }
207
208 /**
209 * @test
210 */
211 public function getCacheThrowsExceptionIfConfiguredFrontendDoesNotImplementFrontendInterface()
212 {
213 $manager = new CacheManager();
214 $cacheIdentifier = 'aCache';
215 $configuration = [
216 $cacheIdentifier => [
217 'frontend' => \stdClass::class,
218 'backend' => BackendFixture::class,
219 'options' => [],
220 ],
221 ];
222 $manager->setCacheConfigurations($configuration);
223 $this->expectException(InvalidCacheException::class);
224 $this->expectExceptionCode(1464550984);
225 $manager->getCache($cacheIdentifier);
226 }
227
228 /**
229 * @test
230 */
231 public function getCacheThrowsExceptionIfConfiguredBackendDoesNotImplementBackendInterface()
232 {
233 $manager = new CacheManager();
234 $cacheIdentifier = 'aCache';
235 $configuration = [
236 $cacheIdentifier => [
237 'frontend' => FrontendFixture::class,
238 'backend' => \stdClass::class,
239 'options' => [],
240 ],
241 ];
242 $manager->setCacheConfigurations($configuration);
243 $this->expectException(InvalidBackendException::class);
244 $this->expectExceptionCode(1464550977);
245 $manager->getCache($cacheIdentifier);
246 }
247
248 /**
249 * @test
250 */
251 public function getCacheCallsInitializeObjectOnFrontendInstance()
252 {
253 $manager = new CacheManager();
254 $cacheIdentifier = 'aCache';
255 $configuration = [
256 $cacheIdentifier => [
257 'backend' => BackendFixture::class,
258 'frontend' => FrontendInitializeObjectFixture::class,
259 'options' => [],
260 ],
261 ];
262 $manager->setCacheConfigurations($configuration);
263 // BackendInitializeObjectFixture throws exception if initializeObject() is called, so expect this
264 $this->expectException(\RuntimeException::class);
265 $this->expectExceptionCode(1464553495);
266 $manager->getCache($cacheIdentifier);
267 }
268
269 /**
270 * @test
271 */
272 public function getCacheCallsInitializeObjectOnBackendInstance()
273 {
274 $manager = new CacheManager();
275 $cacheIdentifier = 'aCache';
276 $configuration = [
277 $cacheIdentifier => [
278 'backend' => BackendInitializeObjectFixture::class,
279 'frontend' => FrontendFixture::class,
280 'options' => [],
281 ],
282 ];
283 $manager->setCacheConfigurations($configuration);
284 // BackendInitializeObjectFixture throws exception if initializeObject() is called, so expect this
285 $this->expectException(\RuntimeException::class);
286 $this->expectExceptionCode(1464552894);
287 $manager->getCache($cacheIdentifier);
288 }
289
290 /**
291 * @test
292 */
293 public function getCacheCreatesBackendWithGivenConfiguration()
294 {
295 $manager = new CacheManager();
296 $cacheIdentifier = 'aCache';
297 $configuration = [
298 $cacheIdentifier => [
299 'backend' => BackendConfigurationOptionFixture::class,
300 'frontend' => FrontendFixture::class,
301 'options' => [
302 'anOption' => 'anOptionValue',
303 ],
304 ],
305 ];
306 $manager->setCacheConfigurations($configuration);
307 // BackendInitializeObjectFixture throws exception if initializeObject() is called, so expect this
308 $this->expectException(\RuntimeException::class);
309 $this->expectExceptionCode(1464555007);
310 $manager->getCache($cacheIdentifier);
311 }
312
313 /**
314 * @test
315 */
316 public function getCacheCreatesCacheInstanceWithFallbackToDefaultFrontend()
317 {
318 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Components\TestingFramework\Core\AccessibleObjectInterface|CacheManager $manager */
319 $manager = $this->getAccessibleMock(CacheManager::class, ['dummy'], [], '', false);
320 $cacheIdentifier = $this->getUniqueId('Test');
321 $configuration = [
322 $cacheIdentifier => [
323 'backend' => BackendFixture::class,
324 'options' => []
325 ]
326 ];
327 $defaultCacheConfiguration = [
328 'frontend' => FrontendDefaultFixture::class,
329 'options' => [],
330 'groups' => [],
331 ];
332 $manager->_set('defaultCacheConfiguration', $defaultCacheConfiguration);
333 $manager->setCacheConfigurations($configuration);
334 $this->expectException(\RuntimeException::class);
335 $this->expectExceptionCode(1476109149);
336 $manager->getCache($cacheIdentifier);
337 }
338
339 /**
340 * @test
341 */
342 public function getCacheCreatesCacheInstanceWithFallbackToDefaultBackend()
343 {
344 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Components\TestingFramework\Core\AccessibleObjectInterface|CacheManager $manager */
345 $manager = $this->getAccessibleMock(CacheManager::class, ['dummy'], [], '', false);
346 $cacheIdentifier = $this->getUniqueId('Test');
347 $configuration = [
348 $cacheIdentifier => [
349 'frontend' => FrontendFixture::class,
350 'options' => []
351 ]
352 ];
353 $defaultCacheConfiguration = [
354 'backend' => BackendDefaultFixture::class,
355 'options' => [],
356 'groups' => [],
357 ];
358 $manager->_set('defaultCacheConfiguration', $defaultCacheConfiguration);
359 $manager->setCacheConfigurations($configuration);
360 $this->expectException(\RuntimeException::class);
361 $this->expectExceptionCode(1464556045);
362 $manager->getCache($cacheIdentifier);
363 }
364
365 /**
366 * @test
367 */
368 public function getCacheReturnsInstanceOfTheSpecifiedCacheFrontend()
369 {
370 $manager = new CacheManager();
371 $cacheIdentifier = 'aCache';
372 $configuration = [
373 $cacheIdentifier => [
374 'backend' => BackendFixture::class,
375 'frontend' => FrontendFixture::class,
376 'options' => [],
377 ],
378 ];
379 $manager->setCacheConfigurations($configuration);
380 $this->assertInstanceOf(FrontendFixture::class, $manager->getCache($cacheIdentifier));
381 }
382
383 /**
384 * @test
385 */
386 public function getCacheGivesIdentifierToCacheFrontend()
387 {
388 $manager = new CacheManager();
389 $cacheIdentifier = 'aCache';
390 $configuration = [
391 $cacheIdentifier => [
392 'backend' => BackendFixture::class,
393 'frontend' => FrontendIdentifierFixture::class,
394 'options' => [],
395 ],
396 ];
397 $manager->setCacheConfigurations($configuration);
398 $this->expectException(\RuntimeException::class);
399 $this->expectExceptionCode(1464555650);
400 $manager->getCache($cacheIdentifier);
401 }
402
403 /**
404 * @test
405 */
406 public function getCacheGivesBackendInstanceToCacheFrontend()
407 {
408 $manager = new CacheManager();
409 $cacheIdentifier = 'aCache';
410 $configuration = [
411 $cacheIdentifier => [
412 'backend' => BackendFixture::class,
413 'frontend' => FrontendBackendInstanceFixture::class,
414 'options' => [],
415 ],
416 ];
417 $manager->setCacheConfigurations($configuration);
418 $this->expectException(\RuntimeException::class);
419 $this->expectExceptionCode(1464557160);
420 $manager->getCache($cacheIdentifier);
421 }
422 }