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