‪TYPO3CMS  9.5
CacheManagerTest.php
Go to the documentation of this file.
1 <?php
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 Prophecy\Argument;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
37 class ‪CacheManagerTest extends UnitTestCase
38 {
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 
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 
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 
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 
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 
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 
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 
188  {
189  $this->expectException(NoSuchCacheGroupException::class);
190  $this->expectExceptionCode(1390334120);
191 
192  $manager = new ‪CacheManager();
193  $manager->flushCachesInGroup('nonExistingGroup');
194  }
195 
200  {
201  $this->expectException(NoSuchCacheGroupException::class);
202  $this->expectExceptionCode(1390334120);
203 
204  $manager = new ‪CacheManager();
205  $manager->flushCachesInGroup('nonExistingGroup');
206  }
207 
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 
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 
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 
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 
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 
317  {
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 
343  {
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 
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 
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 
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 
427  {
429  $manager = $this->getAccessibleMock(CacheManager::class, ['dummy'], [], '', false);
430  $cacheIdentifier = 'aTest';
431 
432  $cacheGroups = [
433  'group1' => [$cacheIdentifier],
434  'group2' => [$cacheIdentifier],
435  ];
436  $manager->_set('cacheGroups', $cacheGroups);
437 
438  $frontend = $this->prophesize(FrontendFixture::class);
439 
440  $caches = [
441  $cacheIdentifier => $frontend->reveal()
442  ];
443  $manager->_set('caches', $caches);
444 
445  $frontend->flushByTags(Argument::any())->shouldNotBeCalled();
446 
447  $configuration = [
448  $cacheIdentifier => [
449  'frontend' => $frontend,
450  'backend' => BackendFixture::class,
451  'options' => [],
452  'groups' => ['group1', 'group2']
453  ],
454  ];
455  $manager->setCacheConfigurations($configuration);
456  $manager->flushCachesInGroupByTags('group2', []);
457  }
458 
463  {
465  $manager = $this->getAccessibleMock(CacheManager::class, ['dummy'], [], '', false);
466  $cacheIdentifier = 'aTest';
467 
468  $cacheGroups = [
469  'group1' => [$cacheIdentifier],
470  'group2' => [$cacheIdentifier],
471  ];
472  $manager->_set('cacheGroups', $cacheGroups);
473 
474  $frontend = $this->prophesize(FrontendFixture::class);
475 
476  $caches = [
477  $cacheIdentifier => $frontend->reveal()
478  ];
479  $manager->_set('caches', $caches);
480 
481  $tags = ['tag1', 'tag2'];
482  $frontend->flushByTags($tags)->shouldBeCalled();
483 
484  $configuration = [
485  $cacheIdentifier => [
486  'frontend' => $frontend,
487  'backend' => BackendFixture::class,
488  'options' => [],
489  'groups' => ['group1', 'group2']
490  ],
491  ];
492  $manager->setCacheConfigurations($configuration);
493  $manager->flushCachesInGroupByTags('group2', $tags);
494  }
495 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\flushCachesCallsTheFlushMethodOfAllRegisteredCaches
‪flushCachesCallsTheFlushMethodOfAllRegisteredCaches()
Definition: CacheManagerTest.php:165
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\flushCachesByTagCallsTheFlushByTagMethodOfAllRegisteredCaches
‪flushCachesByTagCallsTheFlushByTagMethodOfAllRegisteredCaches()
Definition: CacheManagerTest.php:121
‪TYPO3
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\flushCachesInGroupByTagThrowsExceptionForNonExistingGroup
‪flushCachesInGroupByTagThrowsExceptionForNonExistingGroup()
Definition: CacheManagerTest.php:199
‪TYPO3\CMS\Core\Cache\Exception\InvalidBackendException
Definition: InvalidBackendException.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendFixture
Definition: FrontendFixture.php:23
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendIdentifierFixture
Definition: FrontendIdentifierFixture.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheCreatesBackendWithGivenConfiguration
‪getCacheCreatesBackendWithGivenConfiguration()
Definition: CacheManagerTest.php:293
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheGivesBackendInstanceToCacheFrontend
‪getCacheGivesBackendInstanceToCacheFrontend()
Definition: CacheManagerTest.php:406
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheCallsInitializeObjectOnBackendInstance
‪getCacheCallsInitializeObjectOnBackendInstance()
Definition: CacheManagerTest.php:272
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\BackendConfigurationOptionFixture
Definition: BackendConfigurationOptionFixture.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\BackendFixture
Definition: BackendFixture.php:24
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheReturnsInstanceOfTheSpecifiedCacheFrontend
‪getCacheReturnsInstanceOfTheSpecifiedCacheFrontend()
Definition: CacheManagerTest.php:368
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheCallsInitializeObjectOnFrontendInstance
‪getCacheCallsInitializeObjectOnFrontendInstance()
Definition: CacheManagerTest.php:251
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\flushCachesInGroupThrowsExceptionForNonExistingGroup
‪flushCachesInGroupThrowsExceptionForNonExistingGroup()
Definition: CacheManagerTest.php:187
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\managerThrowsExceptionOnCacheRegistrationWithAlreadyExistingIdentifier
‪managerThrowsExceptionOnCacheRegistrationWithAlreadyExistingIdentifier()
Definition: CacheManagerTest.php:42
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendBackendInstanceFixture
Definition: FrontendBackendInstanceFixture.php:21
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheCreatesCacheInstanceWithFallbackToDefaultBackend
‪getCacheCreatesCacheInstanceWithFallbackToDefaultBackend()
Definition: CacheManagerTest.php:342
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheThrowsExceptionIfConfiguredBackendDoesNotImplementBackendInterface
‪getCacheThrowsExceptionIfConfiguredBackendDoesNotImplementBackendInterface()
Definition: CacheManagerTest.php:231
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\BackendInitializeObjectFixture
Definition: BackendInitializeObjectFixture.php:21
‪TYPO3\CMS\Core\Cache\Exception\InvalidCacheException
Definition: InvalidCacheException.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\BackendDefaultFixture
Definition: BackendDefaultFixture.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\managerReturnsThePreviouslyRegisteredCache
‪managerReturnsThePreviouslyRegisteredCache()
Definition: CacheManagerTest.php:65
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\flushCachesInGroupByTagsDeletesByTag
‪flushCachesInGroupByTagsDeletesByTag()
Definition: CacheManagerTest.php:462
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendInitializeObjectFixture
Definition: FrontendInitializeObjectFixture.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest
Definition: CacheManagerTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheThrowsExceptionIfConfiguredFrontendDoesNotImplementFrontendInterface
‪getCacheThrowsExceptionIfConfiguredFrontendDoesNotImplementFrontendInterface()
Definition: CacheManagerTest.php:211
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheCreatesCacheInstanceWithFallbackToDefaultFrontend
‪getCacheCreatesCacheInstanceWithFallbackToDefaultFrontend()
Definition: CacheManagerTest.php:316
‪TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
Definition: NoSuchCacheGroupException.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\flushCachesInGroupByTagsWithEmptyTagsArrayDoesNotFlushCaches
‪flushCachesInGroupByTagsWithEmptyTagsArrayDoesNotFlushCaches()
Definition: CacheManagerTest.php:426
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheThrowsExceptionForNonExistingIdentifier
‪getCacheThrowsExceptionForNonExistingIdentifier()
Definition: CacheManagerTest.php:86
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\hasCacheReturnsCorrectResult
‪hasCacheReturnsCorrectResult()
Definition: CacheManagerTest.php:105
‪TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend
Definition: AbstractFrontend.php:26
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\flushCachesByTagsCallsTheFlushByTagsMethodOfAllRegisteredCaches
‪flushCachesByTagsCallsTheFlushByTagsMethodOfAllRegisteredCaches()
Definition: CacheManagerTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Cache\CacheManagerTest\getCacheGivesIdentifierToCacheFrontend
‪getCacheGivesIdentifierToCacheFrontend()
Definition: CacheManagerTest.php:386
‪TYPO3\CMS\Core\Tests\Unit\Cache\Fixtures\FrontendDefaultFixture
Definition: FrontendDefaultFixture.php:21