TYPO3 CMS  TYPO3_7-6
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 
23 {
29  {
30  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
31  $cache1 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
32  $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
33  $cache2 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
34  $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
35  $manager->registerCache($cache1);
36  $manager->registerCache($cache2);
37  }
38 
43  {
44  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
45  $cache1 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
46  $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
47  $cache2 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
48  $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache2'));
49  $manager->registerCache($cache1);
50  $manager->registerCache($cache2);
51  $this->assertSame($cache2, $manager->getCache('cache2'), 'The cache returned by getCache() was not the same I registered.');
52  }
53 
59  {
60  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
61  $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
62  $cache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('someidentifier'));
63  $manager->registerCache($cache);
64  $manager->getCache('someidentifier');
65  $manager->getCache('doesnotexist');
66  }
67 
71  public function hasCacheReturnsCorrectResult()
72  {
73  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
74  $cache1 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
75  $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
76  $manager->registerCache($cache1);
77  $this->assertTrue($manager->hasCache('cache1'), 'hasCache() did not return TRUE.');
78  $this->assertFalse($manager->hasCache('cache2'), 'hasCache() did not return FALSE.');
79  }
80 
85  {
86  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
87  $cache1 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
88  $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
89  $cache1->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
90  $manager->registerCache($cache1);
91  $cache2 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
92  $cache2->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
93  $manager->registerCache($cache2);
94  $manager->flushCachesByTag('theTag');
95  }
96 
101  {
102  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
103  $cache1 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
104  $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
105  $cache1->expects($this->once())->method('flush');
106  $manager->registerCache($cache1);
107  $cache2 = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, ['getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'], [], '', false);
108  $cache2->expects($this->once())->method('flush');
109  $manager->registerCache($cache2);
110  $manager->flushCaches();
111  }
112 
118  {
119  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
120  $manager->flushCachesInGroup('nonExistingGroup');
121  }
122 
128  {
129  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
130  $manager->flushCachesInGroup('nonExistingGroup', 'someTag');
131  }
132 
137  {
138  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
139  $cacheIdentifier = $this->getUniqueId('Test');
140  $cacheObjectName = 'testCache';
141  $backendObjectName = 'testBackend';
142  $backendOptions = ['foo'];
143  $configuration = [
144  $cacheIdentifier => [
145  'frontend' => $cacheObjectName,
146  'backend' => $backendObjectName,
147  'options' => $backendOptions
148  ]
149  ];
150  $factory = $this->getMock(\TYPO3\CMS\Core\Cache\CacheFactory::class, ['create'], [], '', false);
151  $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, $backendObjectName, $backendOptions);
152  $manager->injectCacheFactory($factory);
153  $manager->setCacheConfigurations($configuration);
154  $manager->getCache($cacheIdentifier);
155  }
156 
161  {
162  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
163  $cacheIdentifier = $this->getUniqueId('Test');
164  $backendObjectName = 'testBackend';
165  $backendOptions = ['foo'];
166  $configuration = [
167  $cacheIdentifier => [
168  'backend' => $backendObjectName,
169  'options' => $backendOptions
170  ]
171  ];
172  $factory = $this->getMock(\TYPO3\CMS\Core\Cache\CacheFactory::class, ['create'], [], '', false);
173  $factory->expects($this->once())->method('create')->with($cacheIdentifier, \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, $backendObjectName, $backendOptions);
174  $manager->injectCacheFactory($factory);
175  $manager->setCacheConfigurations($configuration);
176  $manager->getCache($cacheIdentifier);
177  }
178 
183  {
184  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
185  $cacheIdentifier = $this->getUniqueId('Test');
186  $cacheObjectName = 'testCache';
187  $backendOptions = ['foo'];
188  $configuration = [
189  $cacheIdentifier => [
190  'frontend' => $cacheObjectName,
191  'options' => $backendOptions
192  ]
193  ];
194  $factory = $this->getMock(\TYPO3\CMS\Core\Cache\CacheFactory::class, ['create'], [], '', false);
195  $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, \TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend::class, $backendOptions);
196  $manager->injectCacheFactory($factory);
197  $manager->setCacheConfigurations($configuration);
198  $manager->getCache($cacheIdentifier);
199  }
200 
205  {
206  $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
207  $cacheIdentifier = $this->getUniqueId('Test');
208  $cacheObjectName = 'testCache';
209  $backendObjectName = 'testBackend';
210  $configuration = [
211  $cacheIdentifier => [
212  'frontend' => $cacheObjectName,
213  'backend' => $backendObjectName
214  ]
215  ];
216  $factory = $this->getMock(\TYPO3\CMS\Core\Cache\CacheFactory::class, ['create'], [], '', false);
217  $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, $backendObjectName, []);
218  $manager->injectCacheFactory($factory);
219  $manager->setCacheConfigurations($configuration);
220  $manager->getCache($cacheIdentifier);
221  }
222 }