Fixed #11206: Cleaned up test "t3lib_extmgm_testcase" (Thanks to Oliver Klee)
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / t3lib_cache_managertestcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Testcase for the Cache Manager
28 *
29 * This file is a backport from FLOW3
30 *
31 * @author Ingo Renner <ingo@typo3.org>
32 * @package TYPO3
33 * @subpackage tests
34 * @version $Id$
35 */
36 class t3lib_cache_ManagerTestCase extends tx_phpunit_testcase {
37
38 /**
39 * @test
40 * @author Robert Lemke <robert@typo3.org>
41 * @author Ingo Renner <ingo@typo3.org>
42 */
43 public function initializeCreatesAndRegistersAllCachesDefinedInTheCachesConfiguration() {
44 $mockCacheFactory = $this->getMock('t3lib_cache_Factory', array(), array(), '', FALSE);
45 $mockCacheFactory->expects($this->at(1))->method('create')->with('cache1', 't3lib_cache_frontend_VariableFrontend', 't3lib_cache_backend_FileBackend', array());
46 $mockCacheFactory->expects($this->at(2))->method('create')->with('cache2', 't3lib_cache_frontend_StringFrontend', 't3lib_cache_backend_NullBackend', array('foo' => 'bar'));
47
48 $cacheConfigurations = array(
49 'cache1' => array(),
50 'cache2' => array(
51 'frontend' => 't3lib_cache_frontend_StringFrontend',
52 'backend' => 't3lib_cache_backend_NullBackend',
53 'backendOptions' => array('foo' => 'bar')
54 ),
55 );
56
57 $manager = new t3lib_cache_Manager();
58 $manager->setCacheConfigurations($cacheConfigurations);
59 $manager->setCacheFactory($mockCacheFactory);
60 $manager->initialize();
61 }
62
63 /**
64 * @test
65 * @author Robert Lemke <robert@typo3.org>
66 * @author Ingo Renner <ingo@typo3.org>
67 * @expectedException t3lib_cache_exception_DuplicateIdentifier
68 */
69 public function managerThrowsExceptionOnCacheRegistrationWithAlreadyExistingIdentifier() {
70 $manager = new t3lib_cache_Manager();
71 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
72
73 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
74 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
75
76 $cache2 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
77 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
78
79 $manager->registerCache($cache1);
80 $manager->registerCache($cache2);
81 }
82
83 /**
84 * @test
85 * @author Robert Lemke <robert@typo3.org>
86 * @author Ingo Renner <ingo@typo3.org>
87 */
88 public function managerReturnsThePreviouslyRegisteredCache() {
89 $manager = new t3lib_cache_Manager();
90 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
91
92 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
93 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
94
95 $cache2 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
96 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache2'));
97
98 $manager->registerCache($cache1);
99 $manager->registerCache($cache2);
100
101 $this->assertSame($cache2, $manager->getCache('cache2'), 'The cache returned by getCache() was not the same I registered.');
102 }
103
104 /**
105 * @test
106 * @author Robert Lemke <robert@typo3.org>
107 * @author Ingo Renner <ingo@typo3.org>
108 * @expectedException t3lib_cache_exception_NoSuchCache
109 */
110 public function getCacheThrowsExceptionForNonExistingIdentifier() {
111 $manager = new t3lib_cache_Manager();
112 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
113 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
114 $cache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('someidentifier'));
115
116 $manager->registerCache($cache);
117 $manager->getCache('someidentifier');
118
119 $manager->getCache('doesnotexist');
120 }
121
122 /**
123 * @test
124 * @author Robert Lemke <robert@typo3.org>
125 * @author Ingo Renner <ingo@typo3.org>
126 */
127 public function hasCacheReturnsCorrectResult() {
128 $manager = new t3lib_cache_Manager();
129 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
130 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
131 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
132 $manager->registerCache($cache1);
133
134 $this->assertTrue($manager->hasCache('cache1'), 'hasCache() did not return TRUE.');
135 $this->assertFalse($manager->hasCache('cache2'), 'hasCache() did not return FALSE.');
136 }
137
138 /**
139 * @test
140 * @author Robert Lemke <robert@typo3.org>
141 * @author Ingo Renner <ingo@typo3.org>
142 */
143 public function flushCachesByTagCallsTheFlushByTagMethodOfAllRegisteredCaches() {
144 $manager = new t3lib_cache_Manager();
145
146 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
147
148 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
149 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
150 $cache1->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
151 $manager->registerCache($cache1);
152
153 $cache2 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
154 $cache2->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
155 $manager->registerCache($cache2);
156
157 $manager->flushCachesByTag('theTag');
158 }
159
160 /**
161 * @test
162 * @author Robert Lemke <robert@typo3.org>
163 * @author Ingo Renner <ingo@typo3.org>
164 */
165 public function flushCachesCallsTheFlushMethodOfAllRegisteredCaches() {
166 $manager = new t3lib_cache_Manager();
167 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
168
169 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
170 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
171 $cache1->expects($this->once())->method('flush');
172 $manager->registerCache($cache1);
173
174 $cache2 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
175 $cache2->expects($this->once())->method('flush');
176 $manager->registerCache($cache2);
177
178 $manager->flushCaches();
179 }
180
181 }
182
183 ?>