[TASK] Re-work/simplify copyright header in PHP files - Part 4
[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 /**
18 * Testcase for the TYPO3\CMS\Core\Cache\CacheManager
19 *
20 * This file is a backport from FLOW3
21 *
22 * @author Ingo Renner <ingo@typo3.org>
23 */
24 class CacheManagerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
25
26 /**
27 * @test
28 * @expectedException \TYPO3\CMS\Core\Cache\Exception\DuplicateIdentifierException
29 */
30 public function managerThrowsExceptionOnCacheRegistrationWithAlreadyExistingIdentifier() {
31 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
32 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
33 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
34 $cache2 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
35 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
36 $manager->registerCache($cache1);
37 $manager->registerCache($cache2);
38 }
39
40 /**
41 * @test
42 */
43 public function managerReturnsThePreviouslyRegisteredCache() {
44 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
45 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
46 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
47 $cache2 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', 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
54 /**
55 * @test
56 * @expectedException \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
57 */
58 public function getCacheThrowsExceptionForNonExistingIdentifier() {
59 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
60 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
61 $cache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('someidentifier'));
62 $manager->registerCache($cache);
63 $manager->getCache('someidentifier');
64 $manager->getCache('doesnotexist');
65 }
66
67 /**
68 * @test
69 */
70 public function hasCacheReturnsCorrectResult() {
71 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
72 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
73 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
74 $manager->registerCache($cache1);
75 $this->assertTrue($manager->hasCache('cache1'), 'hasCache() did not return TRUE.');
76 $this->assertFalse($manager->hasCache('cache2'), 'hasCache() did not return FALSE.');
77 }
78
79 /**
80 * @test
81 */
82 public function flushCachesByTagCallsTheFlushByTagMethodOfAllRegisteredCaches() {
83 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
84 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
85 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
86 $cache1->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
87 $manager->registerCache($cache1);
88 $cache2 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
89 $cache2->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
90 $manager->registerCache($cache2);
91 $manager->flushCachesByTag('theTag');
92 }
93
94 /**
95 * @test
96 */
97 public function flushCachesCallsTheFlushMethodOfAllRegisteredCaches() {
98 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
99 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
100 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
101 $cache1->expects($this->once())->method('flush');
102 $manager->registerCache($cache1);
103 $cache2 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
104 $cache2->expects($this->once())->method('flush');
105 $manager->registerCache($cache2);
106 $manager->flushCaches();
107 }
108
109 /**
110 * @test
111 * @expectedException \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
112 */
113 public function flushCachesInGroupThrowsExceptionForNonExistingGroup() {
114 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
115 $manager->flushCachesInGroup('nonExistingGroup');
116 }
117
118 /**
119 * @test
120 * @expectedException \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
121 */
122 public function flushCachesInGroupByTagThrowsExceptionForNonExistingGroup() {
123 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
124 $manager->flushCachesInGroup('nonExistingGroup', 'someTag');
125 }
126
127 /**
128 * @test
129 */
130 public function getCacheCreatesCacheInstanceWithGivenConfiguration() {
131 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
132 $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
133 $cacheObjectName = 'testCache';
134 $backendObjectName = 'testBackend';
135 $backendOptions = array('foo');
136 $configuration = array(
137 $cacheIdentifier => array(
138 'frontend' => $cacheObjectName,
139 'backend' => $backendObjectName,
140 'options' => $backendOptions
141 )
142 );
143 $factory = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheFactory', array('create'), array(), '', FALSE);
144 $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, $backendObjectName, $backendOptions);
145 $manager->injectCacheFactory($factory);
146 $manager->setCacheConfigurations($configuration);
147 $manager->getCache($cacheIdentifier);
148 }
149
150 /**
151 * @test
152 */
153 public function getCacheCreatesCacheInstanceWithFallbackToDefaultFrontend() {
154 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
155 $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
156 $backendObjectName = 'testBackend';
157 $backendOptions = array('foo');
158 $configuration = array(
159 $cacheIdentifier => array(
160 'backend' => $backendObjectName,
161 'options' => $backendOptions
162 )
163 );
164 $factory = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheFactory', array('create'), array(), '', FALSE);
165 $factory->expects($this->once())->method('create')->with($cacheIdentifier, 'TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend', $backendObjectName, $backendOptions);
166 $manager->injectCacheFactory($factory);
167 $manager->setCacheConfigurations($configuration);
168 $manager->getCache($cacheIdentifier);
169 }
170
171 /**
172 * @test
173 */
174 public function getCacheCreatesCacheInstanceWithFallbackToDefaultBackend() {
175 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
176 $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
177 $cacheObjectName = 'testCache';
178 $backendOptions = array('foo');
179 $configuration = array(
180 $cacheIdentifier => array(
181 'frontend' => $cacheObjectName,
182 'options' => $backendOptions
183 )
184 );
185 $factory = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheFactory', array('create'), array(), '', FALSE);
186 $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', $backendOptions);
187 $manager->injectCacheFactory($factory);
188 $manager->setCacheConfigurations($configuration);
189 $manager->getCache($cacheIdentifier);
190 }
191
192 /**
193 * @test
194 */
195 public function getCacheCreatesCacheInstanceWithFallbackToDefaultBackenOptions() {
196 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
197 $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
198 $cacheObjectName = 'testCache';
199 $backendObjectName = 'testBackend';
200 $configuration = array(
201 $cacheIdentifier => array(
202 'frontend' => $cacheObjectName,
203 'backend' => $backendObjectName
204 )
205 );
206 $factory = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheFactory', array('create'), array(), '', FALSE);
207 $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, $backendObjectName, array());
208 $manager->injectCacheFactory($factory);
209 $manager->setCacheConfigurations($configuration);
210 $manager->getCache($cacheIdentifier);
211 }
212
213 }