Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / backend / t3lib_cache_backend_transientmemorybackendtestcase.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 // TODO implement autoloading so that we only require stuff we really need
27 require_once(PATH_t3lib . 'class.t3lib_cache.php');
28
29 require_once(PATH_t3lib . 'cache/backend/interfaces/interface.t3lib_cache_backend_backend.php');
30 require_once(PATH_t3lib . 'cache/frontend/interfaces/interface.t3lib_cache_frontend_frontend.php');
31
32 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_abstractbackend.php');
33 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php');
34 require_once(PATH_t3lib . 'cache/class.t3lib_cache_exception.php');
35 require_once(PATH_t3lib . 'cache/class.t3lib_cache_factory.php');
36 require_once(PATH_t3lib . 'cache/class.t3lib_cache_manager.php');
37 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_variablefrontend.php');
38
39 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_classalreadyloaded.php');
40 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_duplicateidentifier.php');
41 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invalidbackend.php');
42 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invalidcache.php');
43 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invaliddata.php');
44 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_nosuchcache.php');
45
46 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_transientmemorybackend.php');
47
48 /**
49 * Testcase for the TransientMemory cache backend
50 *
51 * This file is a backport from FLOW3
52 *
53 * @author Ingo Renner <ingo@typo3.org>
54 * @package TYPO3
55 * @subpackage tests
56 * @version $Id$
57 */
58 class t3lib_cache_backend_TransientMemoryBackendTestCase extends tx_phpunit_testcase {
59
60 /**
61 * @expectedException t3lib_cache_Exception
62 * @test
63 * @author Robert Lemke <robert@typo3.org>
64 */
65 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
66 $backend = new t3lib_cache_backend_TransientMemoryBackend();
67
68 $data = 'Some data';
69 $identifier = 'MyIdentifier';
70 $backend->set($identifier, $data);
71 }
72
73 /**
74 * @test
75 * @author Robert Lemke <robert@typo3.org>
76 * @author Ingo Renner <ingo@typo3.org>
77 */
78 public function itIsPossibleToSetAndCheckExistenceInCache() {
79 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
80 $backend = new t3lib_cache_backend_TransientMemoryBackend();
81 $backend->setCache($cache);
82
83 $data = 'Some data';
84 $identifier = 'MyIdentifier';
85 $backend->set($identifier, $data);
86 $inCache = $backend->has($identifier);
87 $this->assertTrue($inCache);
88 }
89
90 /**
91 * @test
92 * @author Robert Lemke <robert@typo3.org>
93 * @author Ingo Renner <ingo@typo3.org>
94 */
95 public function itIsPossibleToSetAndGetEntry() {
96 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
97 $backend = new t3lib_cache_backend_TransientMemoryBackend();
98 $backend->setCache($cache);
99
100 $data = 'Some data';
101 $identifier = 'MyIdentifier';
102 $backend->set($identifier, $data);
103 $fetchedData = $backend->get($identifier);
104 $this->assertEquals($data, $fetchedData);
105 }
106
107 /**
108 * @test
109 * @author Robert Lemke <robert@typo3.org>
110 * @author Ingo Renner <ingo@typo3.org>
111 */
112 public function itIsPossibleToRemoveEntryFromCache() {
113 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
114 $backend = new t3lib_cache_backend_TransientMemoryBackend();
115 $backend->setCache($cache);
116
117 $data = 'Some data';
118 $identifier = 'MyIdentifier';
119 $backend->set($identifier, $data);
120 $backend->remove($identifier);
121 $inCache = $backend->has($identifier);
122 $this->assertFalse($inCache);
123 }
124
125 /**
126 * @test
127 * @author Robert Lemke <robert@typo3.org>
128 * @author Ingo Renner <ingo@typo3.org>
129 */
130 public function itIsPossibleToOverwriteAnEntryInTheCache() {
131 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
132 $backend = new t3lib_cache_backend_TransientMemoryBackend();
133 $backend->setCache($cache);
134
135 $data = 'Some data';
136 $identifier = 'MyIdentifier';
137 $backend->set($identifier, $data);
138 $otherData = 'some other data';
139 $backend->set($identifier, $otherData);
140 $fetchedData = $backend->get($identifier);
141 $this->assertEquals($otherData, $fetchedData);
142 }
143
144 /**
145 * @test
146 * @author Robert Lemke <robert@typo3.org>
147 * @author Ingo Renner <ingo@typo3.org>
148 */
149 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
150 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
151 $backend = new t3lib_cache_backend_TransientMemoryBackend();
152 $backend->setCache($cache);
153
154 $data = 'Some data';
155 $entryIdentifier = 'MyIdentifier';
156 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
157
158 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
159 $this->assertEquals($entryIdentifier, $retrieved[0]);
160
161 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
162 $this->assertEquals($entryIdentifier, $retrieved[0]);
163 }
164
165 /**
166 * @test
167 * @author Robert Lemke <robert@typo3.org>
168 * @author Ingo Renner <ingo@typo3.org>
169 */
170 public function hasReturnsFalseIfTheEntryDoesntExist() {
171 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
172 $backend = new t3lib_cache_backend_TransientMemoryBackend();
173 $backend->setCache($cache);
174
175 $identifier = 'NonExistingIdentifier';
176 $inCache = $backend->has($identifier);
177 $this->assertFalse($inCache);
178 }
179
180 /**
181 * @test
182 * @author Robert Lemke <robert@typo3.org>
183 * @author Ingo Renner <ingo@typo3.org>
184 */
185 public function removeReturnsFalseIfTheEntryDoesntExist() {
186 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
187 $backend = new t3lib_cache_backend_TransientMemoryBackend();
188 $backend->setCache($cache);
189
190 $identifier = 'NonExistingIdentifier';
191 $inCache = $backend->remove($identifier);
192 $this->assertFalse($inCache);
193 }
194
195 /**
196 * @test
197 * @author Robert Lemke <robert@typo3.org>
198 * @author Karsten Dambekalns <karsten@typo3.org>
199 * @author Ingo Renner <ingo@typo3.org>
200 */
201 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
202 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
203 $backend = new t3lib_cache_backend_TransientMemoryBackend();
204 $backend->setCache($cache);
205
206 $data = 'some data' . microtime();
207 $backend->set('TransientMemoryBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
208 $backend->set('TransientMemoryBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
209 $backend->set('TransientMemoryBackendTest3', $data, array('UnitTestTag%test'));
210
211 $backend->flushByTag('UnitTestTag%special');
212
213 $this->assertTrue($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
214 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
215 $this->assertTrue($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
216 }
217
218 /**
219 * @test
220 * @author Karsten Dambekalns <karsten@typo3.org>
221 * @author Ingo Renner <ingo@typo3.org>
222 */
223 public function flushRemovesAllCacheEntries() {
224 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
225 $backend = new t3lib_cache_backend_TransientMemoryBackend();
226 $backend->setCache($cache);
227
228 $data = 'some data' . microtime();
229 $backend->set('TransientMemoryBackendTest1', $data);
230 $backend->set('TransientMemoryBackendTest2', $data);
231 $backend->set('TransientMemoryBackendTest3', $data);
232
233 $backend->flush();
234
235 $this->assertFalse($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
236 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
237 $this->assertFalse($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
238 }
239 }
240
241 ?>