83dd01e5384aa609054f12ba07678a59b82afcb6
[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 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for the TYPO3\CMS\Core\Cache\CacheManager
29 *
30 * This file is a backport from FLOW3
31 *
32 * @author Ingo Renner <ingo@typo3.org>
33 */
34 class CacheManagerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
35
36 /**
37 * @test
38 * @expectedException \TYPO3\CMS\Core\Cache\Exception\DuplicateIdentifierException
39 */
40 public function managerThrowsExceptionOnCacheRegistrationWithAlreadyExistingIdentifier() {
41 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
42 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
43 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
44 $cache2 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
45 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
46 $manager->registerCache($cache1);
47 $manager->registerCache($cache2);
48 }
49
50 /**
51 * @test
52 */
53 public function managerReturnsThePreviouslyRegisteredCache() {
54 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
55 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
56 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
57 $cache2 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
58 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache2'));
59 $manager->registerCache($cache1);
60 $manager->registerCache($cache2);
61 $this->assertSame($cache2, $manager->getCache('cache2'), 'The cache returned by getCache() was not the same I registered.');
62 }
63
64 /**
65 * @test
66 * @expectedException \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
67 */
68 public function getCacheThrowsExceptionForNonExistingIdentifier() {
69 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
70 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
71 $cache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('someidentifier'));
72 $manager->registerCache($cache);
73 $manager->getCache('someidentifier');
74 $manager->getCache('doesnotexist');
75 }
76
77 /**
78 * @test
79 */
80 public function hasCacheReturnsCorrectResult() {
81 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
82 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
83 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
84 $manager->registerCache($cache1);
85 $this->assertTrue($manager->hasCache('cache1'), 'hasCache() did not return TRUE.');
86 $this->assertFalse($manager->hasCache('cache2'), 'hasCache() did not return FALSE.');
87 }
88
89 /**
90 * @test
91 */
92 public function flushCachesByTagCallsTheFlushByTagMethodOfAllRegisteredCaches() {
93 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
94 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
95 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
96 $cache1->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
97 $manager->registerCache($cache1);
98 $cache2 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
99 $cache2->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
100 $manager->registerCache($cache2);
101 $manager->flushCachesByTag('theTag');
102 }
103
104 /**
105 * @test
106 */
107 public function flushCachesCallsTheFlushMethodOfAllRegisteredCaches() {
108 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
109 $cache1 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
110 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
111 $cache1->expects($this->once())->method('flush');
112 $manager->registerCache($cache1);
113 $cache2 = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
114 $cache2->expects($this->once())->method('flush');
115 $manager->registerCache($cache2);
116 $manager->flushCaches();
117 }
118
119 /**
120 * @test
121 * @expectedException \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
122 */
123 public function flushCachesInGroupThrowsExceptionForNonExistingGroup() {
124 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
125 $manager->flushCachesInGroup('nonExistingGroup');
126 }
127
128 /**
129 * @test
130 * @expectedException \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
131 */
132 public function flushCachesInGroupByTagThrowsExceptionForNonExistingGroup() {
133 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
134 $manager->flushCachesInGroup('nonExistingGroup', 'someTag');
135 }
136
137 /**
138 * @test
139 */
140 public function getCacheCreatesCacheInstanceWithGivenConfiguration() {
141 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
142 $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
143 $cacheObjectName = 'testCache';
144 $backendObjectName = 'testBackend';
145 $backendOptions = array('foo');
146 $configuration = array(
147 $cacheIdentifier => array(
148 'frontend' => $cacheObjectName,
149 'backend' => $backendObjectName,
150 'options' => $backendOptions
151 )
152 );
153 $factory = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheFactory', array('create'), array(), '', FALSE);
154 $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, $backendObjectName, $backendOptions);
155 $manager->injectCacheFactory($factory);
156 $manager->setCacheConfigurations($configuration);
157 $manager->getCache($cacheIdentifier);
158 }
159
160 /**
161 * @test
162 */
163 public function getCacheCreatesCacheInstanceWithFallbackToDefaultFrontend() {
164 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
165 $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
166 $backendObjectName = 'testBackend';
167 $backendOptions = array('foo');
168 $configuration = array(
169 $cacheIdentifier => array(
170 'backend' => $backendObjectName,
171 'options' => $backendOptions
172 )
173 );
174 $factory = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheFactory', array('create'), array(), '', FALSE);
175 $factory->expects($this->once())->method('create')->with($cacheIdentifier, 'TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend', $backendObjectName, $backendOptions);
176 $manager->injectCacheFactory($factory);
177 $manager->setCacheConfigurations($configuration);
178 $manager->getCache($cacheIdentifier);
179 }
180
181 /**
182 * @test
183 */
184 public function getCacheCreatesCacheInstanceWithFallbackToDefaultBackend() {
185 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
186 $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
187 $cacheObjectName = 'testCache';
188 $backendOptions = array('foo');
189 $configuration = array(
190 $cacheIdentifier => array(
191 'frontend' => $cacheObjectName,
192 'options' => $backendOptions
193 )
194 );
195 $factory = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheFactory', array('create'), array(), '', FALSE);
196 $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', $backendOptions);
197 $manager->injectCacheFactory($factory);
198 $manager->setCacheConfigurations($configuration);
199 $manager->getCache($cacheIdentifier);
200 }
201
202 /**
203 * @test
204 */
205 public function getCacheCreatesCacheInstanceWithFallbackToDefaultBackenOptions() {
206 $manager = new \TYPO3\CMS\Core\Cache\CacheManager();
207 $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
208 $cacheObjectName = 'testCache';
209 $backendObjectName = 'testBackend';
210 $configuration = array(
211 $cacheIdentifier => array(
212 'frontend' => $cacheObjectName,
213 'backend' => $backendObjectName
214 )
215 );
216 $factory = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheFactory', array('create'), array(), '', FALSE);
217 $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, $backendObjectName, array());
218 $manager->injectCacheFactory($factory);
219 $manager->setCacheConfigurations($configuration);
220 $manager->getCache($cacheIdentifier);
221 }
222
223 }