[BUGFIX] Failing test in caching framework memcache backend
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / backend / t3lib_cache_backend_memcachedbackendTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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 * Testcase for the cache to memcached backend
27 *
28 * This file is a backport from FLOW3
29 *
30 * @author Ingo Renner <ingo@typo3.org>
31 * @package TYPO3
32 * @subpackage tests
33 * @version $Id$
34 */
35 class t3lib_cache_backend_MemcachedBackendTest extends tx_phpunit_testcase {
36
37 /**
38 * Sets up this testcase
39 *
40 * @return void
41 * @author Christian Jul Jensen <julle@typo3.org>
42 * @author Ingo Renner <ingo@typo3.org>
43 */
44 public function setUp() {
45 if (!extension_loaded('memcache')) {
46 $this->markTestSkipped('memcache extension was not available');
47 }
48
49 try {
50 if (!fsockopen('localhost', 11211)) {
51 $this->markTestSkipped('memcached not reachable');
52 }
53 } catch (Exception $e) {
54 $this->markTestSkipped('memcached not reachable');
55 }
56 }
57
58 /**
59 * @test
60 * @author Christian Jul Jensen <julle@typo3.org>
61 * @author Ingo Renner <ingo@typo3.org>
62 * @expectedException t3lib_cache_Exception
63 */
64 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
65 $backendOptions = array('servers' => array('localhost:11211'));
66 $backend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
67
68 $data = 'Some data';
69 $identifier = uniqid('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 * @expectedException t3lib_cache_Exception
78 */
79 public function constructorThrowsExceptionIfNoMemcacheServerIsConfigured() {
80 $backend = new t3lib_cache_backend_MemcachedBackend();
81 }
82
83 /**
84 * @test
85 * @author Christian Jul Jensen <julle@typo3.org>
86 */
87 public function itIsPossibleToSetAndCheckExistenceInCache() {
88 $backend = $this->setUpBackend();
89 $data = 'Some data';
90 $identifier = uniqid('MyIdentifier');
91 $backend->set($identifier, $data);
92 $inCache = $backend->has($identifier);
93 $this->assertTrue($inCache, 'Memcache failed to set and check entry');
94 }
95
96 /**
97 * @test
98 * @author Christian Jul Jensen <julle@typo3.org>
99 */
100 public function itIsPossibleToSetAndGetEntry() {
101 $backend = $this->setUpBackend();
102 $data = 'Some data';
103 $identifier = uniqid('MyIdentifier');
104 $backend->set($identifier, $data);
105 $fetchedData = $backend->get($identifier);
106 $this->assertEquals($data, $fetchedData, 'Memcache failed to set and retrieve data');
107 }
108
109 /**
110 * @test
111 * @author Christian Jul Jensen <julle@typo3.org>
112 */
113 public function itIsPossibleToRemoveEntryFromCache() {
114 $backend = $this->setUpBackend();
115 $data = 'Some data';
116 $identifier = uniqid('MyIdentifier');
117 $backend->set($identifier, $data);
118 $backend->remove($identifier);
119 $inCache = $backend->has($identifier);
120 $this->assertFalse($inCache, 'Failed to set and remove data from Memcache');
121 }
122
123 /**
124 * @test
125 * @author Christian Jul Jensen <julle@typo3.org>
126 */
127 public function itIsPossibleToOverwriteAnEntryInTheCache() {
128 $backend = $this->setUpBackend();
129 $data = 'Some data';
130 $identifier = uniqid('MyIdentifier');
131 $backend->set($identifier, $data);
132 $otherData = 'some other data';
133 $backend->set($identifier, $otherData);
134 $fetchedData = $backend->get($identifier);
135 $this->assertEquals($otherData, $fetchedData, 'Memcache failed to overwrite and retrieve data');
136 }
137
138 /**
139 * @test
140 * @author Karsten Dambekalns <karsten@typo3.org>
141 */
142 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
143 $backend = $this->setUpBackend();
144
145 $data = 'Some data';
146 $identifier = uniqid('MyIdentifier');
147 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
148
149 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
150 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
151
152 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
153 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
154 }
155
156 /**
157 * @test
158 * @author Karsten Dambekalns <karsten@typo3.org>
159 */
160 public function setRemovesTagsFromPreviousSet() {
161 $backend = $this->setUpBackend();
162
163 $data = 'Some data';
164 $identifier = uniqid('MyIdentifier');
165 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
166 $backend->set($identifier, $data, array('UnitTestTag%tag3'));
167
168 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
169 $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
170 }
171
172 /**
173 * @test
174 * @author Christian Jul Jensen <julle@typo3.org>
175 */
176 public function hasReturnsFalseIfTheEntryDoesntExist() {
177 $backend = $this->setUpBackend();
178 $identifier = uniqid('NonExistingIdentifier');
179 $inCache = $backend->has($identifier);
180 $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
181 }
182
183 /**
184 * @test
185 * @author Christian Jul Jensen <julle@typo3.org>
186 */
187 public function removeReturnsFalseIfTheEntryDoesntExist() {
188 $backend = $this->setUpBackend();
189 $identifier = uniqid('NonExistingIdentifier');
190 $inCache = $backend->remove($identifier);
191 $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
192 }
193
194 /**
195 * @test
196 * @author Robert Lemke <robert@typo3.org>
197 * @author Karsten Dambekalns <karsten@typo3.org>
198 */
199 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
200 $backend = $this->setUpBackend();
201
202 $data = 'some data' . microtime();
203 $backend->set('BackendMemcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
204 $backend->set('BackendMemcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
205 $backend->set('BackendMemcacheTest3', $data, array('UnitTestTag%test'));
206
207 $backend->flushByTag('UnitTestTag%special');
208
209 $this->assertTrue($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
210 $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
211 $this->assertTrue($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
212 }
213
214 /**
215 * @test
216 * @author Karsten Dambekalns <karsten@typo3.org>
217 */
218 public function flushRemovesAllCacheEntries() {
219 $backend = $this->setUpBackend();
220
221 $data = 'some data' . microtime();
222 $backend->set('BackendMemcacheTest1', $data);
223 $backend->set('BackendMemcacheTest2', $data);
224 $backend->set('BackendMemcacheTest3', $data);
225
226 $backend->flush();
227
228 $this->assertFalse($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
229 $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
230 $this->assertFalse($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
231 }
232
233 /**
234 * @test
235 * @author Karsten Dambekalns <karsten@typo3.org>
236 * @author Ingo Renner <ingo@typo3.org>
237 */
238 public function flushRemovesOnlyOwnEntries() {
239 $backendOptions = array('servers' => array('localhost:11211'));
240
241 $thisCache = $this->getMock(
242 't3lib_cache_frontend_AbstractFrontend',
243 array(),
244 array(),
245 '',
246 FALSE
247 );
248 $thisCache->expects($this->any())
249 ->method('getIdentifier')
250 ->will($this->returnValue('thisCache'));
251 $thisBackend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
252 $thisBackend->setCache($thisCache);
253
254 $thatCache = $this->getMock(
255 't3lib_cache_frontend_AbstractFrontend',
256 array(),
257 array(),
258 '',
259 FALSE
260 );
261 $thatCache->expects($this->any())
262 ->method('getIdentifier')
263 ->will($this->returnValue('thatCache'));
264 $thatBackend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
265 $thatBackend->setCache($thatCache);
266
267 $thisBackend->set('thisEntry', 'Hello');
268 $thatBackend->set('thatEntry', 'World!');
269 $thatBackend->flush();
270
271 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
272 $this->assertFalse($thatBackend->has('thatEntry'));
273 }
274
275 /**
276 * Check if we can store ~5 MB of data, this gives some headroom for the
277 * reflection data.
278 *
279 * @test
280 * @author Karsten Dambekalns <karsten@typo3.org>
281 */
282 public function largeDataIsStored() {
283 $backend = $this->setUpBackend();
284
285 $data = str_repeat('abcde', 1024 * 1024);
286 $backend->set('tooLargeData', $data);
287
288 $this->assertTrue($backend->has('tooLargeData'));
289 $this->assertEquals($backend->get('tooLargeData'), $data);
290 }
291
292 /**
293 * Sets up the memcached backend used for testing
294 *
295 * @param array $backendOptions Options for the memcache backend
296 * @return t3lib_cache_backend_MemcachedBackend
297 * @author Christian Jul Jensen <julle@typo3.org>
298 * @author Karsten Dambekalns <karsten@typo3.org>
299 * @author Ingo Renner <ingo@typo3.org>
300 */
301 protected function setUpBackend(array $backendOptions = array()) {
302 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
303 if (empty($backendOptions)) {
304 $backendOptions = array('servers' => array('localhost:11211'));
305 }
306
307 $backend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
308 $backend->setCache($cache);
309
310 return $backend;
311 }
312 }
313
314 ?>