[TASK] Add leading backslashes to InvalidArgumentException
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / MemcachedBackendTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2011 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 cache to memcached backend
29 *
30 * This file is a backport from FLOW3
31 *
32 * @author Ingo Renner <ingo@typo3.org>
33 * @package TYPO3
34 * @subpackage tests
35 */
36 class MemcachedBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
37
38 /**
39 * Sets up this testcase
40 *
41 * @return void
42 */
43 public function setUp() {
44 if (!extension_loaded('memcache')) {
45 $this->markTestSkipped('memcache extension was not available');
46 }
47 try {
48 if (!fsockopen('localhost', 11211)) {
49 $this->markTestSkipped('memcached not reachable');
50 }
51 } catch (\Exception $e) {
52 $this->markTestSkipped('memcached not reachable');
53 }
54 }
55
56 /**
57 * @test
58 * @expectedException \TYPO3\CMS\Core\Cache\Exception
59 */
60 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
61 $backendOptions = array('servers' => array('localhost:11211'));
62 $backend = new \TYPO3\CMS\Core\Cache\Backend\MemcachedBackend('Testing', $backendOptions);
63 $backend->initializeObject();
64 $data = 'Some data';
65 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
66 $backend->set($identifier, $data);
67 }
68
69 /**
70 * @test
71 * @expectedException \TYPO3\CMS\Core\Cache\Exception
72 */
73 public function initializeObjectThrowsExceptionIfNoMemcacheServerIsConfigured() {
74 $backend = new \TYPO3\CMS\Core\Cache\Backend\MemcachedBackend('Testing');
75 $backend->initializeObject();
76 }
77
78 /**
79 * @test
80 */
81 public function itIsPossibleToSetAndCheckExistenceInCache() {
82 $backend = $this->setUpBackend();
83 $data = 'Some data';
84 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
85 $backend->set($identifier, $data);
86 $inCache = $backend->has($identifier);
87 $this->assertTrue($inCache, 'Memcache failed to set and check entry');
88 }
89
90 /**
91 * @test
92 */
93 public function itIsPossibleToSetAndGetEntry() {
94 $backend = $this->setUpBackend();
95 $data = 'Some data';
96 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
97 $backend->set($identifier, $data);
98 $fetchedData = $backend->get($identifier);
99 $this->assertEquals($data, $fetchedData, 'Memcache failed to set and retrieve data');
100 }
101
102 /**
103 * @test
104 */
105 public function itIsPossibleToRemoveEntryFromCache() {
106 $backend = $this->setUpBackend();
107 $data = 'Some data';
108 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
109 $backend->set($identifier, $data);
110 $backend->remove($identifier);
111 $inCache = $backend->has($identifier);
112 $this->assertFalse($inCache, 'Failed to set and remove data from Memcache');
113 }
114
115 /**
116 * @test
117 */
118 public function itIsPossibleToOverwriteAnEntryInTheCache() {
119 $backend = $this->setUpBackend();
120 $data = 'Some data';
121 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
122 $backend->set($identifier, $data);
123 $otherData = 'some other data';
124 $backend->set($identifier, $otherData);
125 $fetchedData = $backend->get($identifier);
126 $this->assertEquals($otherData, $fetchedData, 'Memcache failed to overwrite and retrieve data');
127 }
128
129 /**
130 * @test
131 */
132 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
133 $backend = $this->setUpBackend();
134 $data = 'Some data';
135 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
136 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
137 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
138 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
139 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
140 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
141 }
142
143 /**
144 * @test
145 */
146 public function setRemovesTagsFromPreviousSet() {
147 $backend = $this->setUpBackend();
148 $data = 'Some data';
149 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
150 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
151 $backend->set($identifier, $data, array('UnitTestTag%tag3'));
152 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
153 $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
154 }
155
156 /**
157 * @test
158 */
159 public function hasReturnsFalseIfTheEntryDoesntExist() {
160 $backend = $this->setUpBackend();
161 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
162 $inCache = $backend->has($identifier);
163 $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
164 }
165
166 /**
167 * @test
168 */
169 public function removeReturnsFalseIfTheEntryDoesntExist() {
170 $backend = $this->setUpBackend();
171 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
172 $inCache = $backend->remove($identifier);
173 $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
174 }
175
176 /**
177 * @test
178 */
179 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
180 $backend = $this->setUpBackend();
181 $data = 'some data' . microtime();
182 $backend->set('BackendMemcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
183 $backend->set('BackendMemcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
184 $backend->set('BackendMemcacheTest3', $data, array('UnitTestTag%test'));
185 $backend->flushByTag('UnitTestTag%special');
186 $this->assertTrue($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
187 $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
188 $this->assertTrue($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
189 }
190
191 /**
192 * @test
193 */
194 public function flushRemovesAllCacheEntries() {
195 $backend = $this->setUpBackend();
196 $data = 'some data' . microtime();
197 $backend->set('BackendMemcacheTest1', $data);
198 $backend->set('BackendMemcacheTest2', $data);
199 $backend->set('BackendMemcacheTest3', $data);
200 $backend->flush();
201 $this->assertFalse($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
202 $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
203 $this->assertFalse($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
204 }
205
206 /**
207 * @test
208 */
209 public function flushRemovesOnlyOwnEntries() {
210 $backendOptions = array('servers' => array('localhost:11211'));
211 $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
212 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
213 $thisBackend = new \TYPO3\CMS\Core\Cache\Backend\MemcachedBackend('Testing', $backendOptions);
214 $thisBackend->setCache($thisCache);
215 $thisBackend->initializeObject();
216 $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
217 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
218 $thatBackend = new \TYPO3\CMS\Core\Cache\Backend\MemcachedBackend('Testing', $backendOptions);
219 $thatBackend->setCache($thatCache);
220 $thatBackend->initializeObject();
221 $thisBackend->set('thisEntry', 'Hello');
222 $thatBackend->set('thatEntry', 'World!');
223 $thatBackend->flush();
224 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
225 $this->assertFalse($thatBackend->has('thatEntry'));
226 }
227
228 /**
229 * Check if we can store ~5 MB of data, this gives some headroom for the
230 * reflection data.
231 *
232 * @test
233 */
234 public function largeDataIsStored() {
235 $backend = $this->setUpBackend();
236 $data = str_repeat('abcde', 1024 * 1024);
237 $backend->set('tooLargeData', $data);
238 $this->assertTrue($backend->has('tooLargeData'));
239 $this->assertEquals($backend->get('tooLargeData'), $data);
240 }
241
242 /**
243 * Sets up the memcached backend used for testing
244 *
245 * @param array $backendOptions Options for the memcache backend
246 * @return \TYPO3\CMS\Core\Cache\Backend\MemcachedBackend
247 */
248 protected function setUpBackend(array $backendOptions = array()) {
249 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
250 if ($backendOptions == array()) {
251 $backendOptions = array('servers' => array('localhost:11211'));
252 }
253 $backend = new \TYPO3\CMS\Core\Cache\Backend\MemcachedBackend('Testing', $backendOptions);
254 $backend->setCache($cache);
255 $backend->initializeObject();
256 return $backend;
257 }
258
259 }
260
261 ?>