3c729461197d33494fb05ca2a6938e46adc5949e
[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 * @author Ingo Renner <ingo@typo3.org>
87 * @expectedException t3lib_cache_Exception
88 */
89 public function setThrowsExceptionIfConfiguredServersAreUnreachable() {
90 $backend = $this->setUpBackend(array('servers' => array('julle.did.this:1234')));
91 $data = 'Somedata';
92 $identifier = uniqid('MyIdentifier');
93 $backend->set($identifier, $data);
94 }
95
96 /**
97 * @test
98 * @author Christian Jul Jensen <julle@typo3.org>
99 */
100 public function itIsPossibleToSetAndCheckExistenceInCache() {
101 $backend = $this->setUpBackend();
102 $data = 'Some data';
103 $identifier = uniqid('MyIdentifier');
104 $backend->set($identifier, $data);
105 $inCache = $backend->has($identifier);
106 $this->assertTrue($inCache, 'Memcache failed to set and check entry');
107 }
108
109 /**
110 * @test
111 * @author Christian Jul Jensen <julle@typo3.org>
112 */
113 public function itIsPossibleToSetAndGetEntry() {
114 $backend = $this->setUpBackend();
115 $data = 'Some data';
116 $identifier = uniqid('MyIdentifier');
117 $backend->set($identifier, $data);
118 $fetchedData = $backend->get($identifier);
119 $this->assertEquals($data, $fetchedData, 'Memcache failed to set and retrieve data');
120 }
121
122 /**
123 * @test
124 * @author Christian Jul Jensen <julle@typo3.org>
125 */
126 public function itIsPossibleToRemoveEntryFromCache() {
127 $backend = $this->setUpBackend();
128 $data = 'Some data';
129 $identifier = uniqid('MyIdentifier');
130 $backend->set($identifier, $data);
131 $backend->remove($identifier);
132 $inCache = $backend->has($identifier);
133 $this->assertFalse($inCache, 'Failed to set and remove data from Memcache');
134 }
135
136 /**
137 * @test
138 * @author Christian Jul Jensen <julle@typo3.org>
139 */
140 public function itIsPossibleToOverwriteAnEntryInTheCache() {
141 $backend = $this->setUpBackend();
142 $data = 'Some data';
143 $identifier = uniqid('MyIdentifier');
144 $backend->set($identifier, $data);
145 $otherData = 'some other data';
146 $backend->set($identifier, $otherData);
147 $fetchedData = $backend->get($identifier);
148 $this->assertEquals($otherData, $fetchedData, 'Memcache failed to overwrite and retrieve data');
149 }
150
151 /**
152 * @test
153 * @author Karsten Dambekalns <karsten@typo3.org>
154 */
155 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
156 $backend = $this->setUpBackend();
157
158 $data = 'Some data';
159 $identifier = uniqid('MyIdentifier');
160 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
161
162 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
163 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
164
165 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
166 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
167 }
168
169 /**
170 * @test
171 * @author Karsten Dambekalns <karsten@typo3.org>
172 */
173 public function setRemovesTagsFromPreviousSet() {
174 $backend = $this->setUpBackend();
175
176 $data = 'Some data';
177 $identifier = uniqid('MyIdentifier');
178 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
179 $backend->set($identifier, $data, array('UnitTestTag%tag3'));
180
181 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
182 $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
183 }
184
185 /**
186 * @test
187 * @author Christian Jul Jensen <julle@typo3.org>
188 */
189 public function hasReturnsFalseIfTheEntryDoesntExist() {
190 $backend = $this->setUpBackend();
191 $identifier = uniqid('NonExistingIdentifier');
192 $inCache = $backend->has($identifier);
193 $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
194 }
195
196 /**
197 * @test
198 * @author Christian Jul Jensen <julle@typo3.org>
199 */
200 public function removeReturnsFalseIfTheEntryDoesntExist() {
201 $backend = $this->setUpBackend();
202 $identifier = uniqid('NonExistingIdentifier');
203 $inCache = $backend->remove($identifier);
204 $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
205 }
206
207 /**
208 * @test
209 * @author Robert Lemke <robert@typo3.org>
210 * @author Karsten Dambekalns <karsten@typo3.org>
211 */
212 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
213 $backend = $this->setUpBackend();
214
215 $data = 'some data' . microtime();
216 $backend->set('BackendMemcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
217 $backend->set('BackendMemcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
218 $backend->set('BackendMemcacheTest3', $data, array('UnitTestTag%test'));
219
220 $backend->flushByTag('UnitTestTag%special');
221
222 $this->assertTrue($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
223 $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
224 $this->assertTrue($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
225 }
226
227 /**
228 * @test
229 * @author Karsten Dambekalns <karsten@typo3.org>
230 */
231 public function flushRemovesAllCacheEntries() {
232 $backend = $this->setUpBackend();
233
234 $data = 'some data' . microtime();
235 $backend->set('BackendMemcacheTest1', $data);
236 $backend->set('BackendMemcacheTest2', $data);
237 $backend->set('BackendMemcacheTest3', $data);
238
239 $backend->flush();
240
241 $this->assertFalse($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
242 $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
243 $this->assertFalse($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
244 }
245
246 /**
247 * @test
248 * @author Karsten Dambekalns <karsten@typo3.org>
249 * @author Ingo Renner <ingo@typo3.org>
250 */
251 public function flushRemovesOnlyOwnEntries() {
252 $backendOptions = array('servers' => array('localhost:11211'));
253
254 $thisCache = $this->getMock(
255 't3lib_cache_frontend_AbstractFrontend',
256 array(),
257 array(),
258 '',
259 FALSE
260 );
261 $thisCache->expects($this->any())
262 ->method('getIdentifier')
263 ->will($this->returnValue('thisCache'));
264 $thisBackend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
265 $thisBackend->setCache($thisCache);
266
267 $thatCache = $this->getMock(
268 't3lib_cache_frontend_AbstractFrontend',
269 array(),
270 array(),
271 '',
272 FALSE
273 );
274 $thatCache->expects($this->any())
275 ->method('getIdentifier')
276 ->will($this->returnValue('thatCache'));
277 $thatBackend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
278 $thatBackend->setCache($thatCache);
279
280 $thisBackend->set('thisEntry', 'Hello');
281 $thatBackend->set('thatEntry', 'World!');
282 $thatBackend->flush();
283
284 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
285 $this->assertFalse($thatBackend->has('thatEntry'));
286 }
287
288 /**
289 * Check if we can store ~5 MB of data, this gives some headroom for the
290 * reflection data.
291 *
292 * @test
293 * @author Karsten Dambekalns <karsten@typo3.org>
294 */
295 public function largeDataIsStored() {
296 $backend = $this->setUpBackend();
297
298 $data = str_repeat('abcde', 1024 * 1024);
299 $backend->set('tooLargeData', $data);
300
301 $this->assertTrue($backend->has('tooLargeData'));
302 $this->assertEquals($backend->get('tooLargeData'), $data);
303 }
304
305 /**
306 * Sets up the memcached backend used for testing
307 *
308 * @param array $backendOptions Options for the memcache backend
309 * @return t3lib_cache_backend_MemcachedBackend
310 * @author Christian Jul Jensen <julle@typo3.org>
311 * @author Karsten Dambekalns <karsten@typo3.org>
312 * @author Ingo Renner <ingo@typo3.org>
313 */
314 protected function setUpBackend(array $backendOptions = array()) {
315 $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
316 if (empty($backendOptions)) {
317 $backendOptions = array('servers' => array('localhost:11211'));
318 }
319
320 $backend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
321 $backend->setCache($cache);
322
323 return $backend;
324 }
325 }
326
327 ?>