[TASK] Raise PHP version requirement to 5.3.7
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / ApcBackendTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 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 APC cache backend.
29 *
30 * NOTE: If you want to execute these tests you need to enable apc in
31 * cli context (apc.enable_cli = 1)
32 *
33 * This file is a backport from FLOW3
34 *
35 * @author Ingo Renner <ingo@typo3.org>
36 */
37 class ApcBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
38
39 /**
40 * Sets up this testcase
41 *
42 * @return void
43 */
44 public function setUp() {
45 // Currently APCu identifies itself both as "apcu" and "apc" (for compatibility) although it doesn't provide the APC-opcache functionality
46 if (!extension_loaded('apc')) {
47 $this->markTestSkipped('APC/APCu extension was not available');
48 }
49 if (ini_get('apc.slam_defense') == 1) {
50 $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = Off');
51 }
52 }
53
54 /**
55 * @test
56 * @expectedException \TYPO3\CMS\Core\Cache\Exception
57 */
58 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
59 $backend = new \TYPO3\CMS\Core\Cache\Backend\ApcBackend('Testing');
60 $data = 'Some data';
61 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
62 $backend->set($identifier, $data);
63 }
64
65 /**
66 * @test
67 */
68 public function itIsPossibleToSetAndCheckExistenceInCache() {
69 $backend = $this->setUpBackend();
70 $data = 'Some data';
71 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
72 $backend->set($identifier, $data);
73 $inCache = $backend->has($identifier);
74 $this->assertTrue($inCache, 'APC backend failed to set and check entry');
75 }
76
77 /**
78 * @test
79 */
80 public function itIsPossibleToSetAndGetEntry() {
81 $backend = $this->setUpBackend();
82 $data = 'Some data';
83 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
84 $backend->set($identifier, $data);
85 $fetchedData = $backend->get($identifier);
86 $this->assertEquals($data, $fetchedData, 'APC backend failed to set and retrieve data');
87 }
88
89 /**
90 * @test
91 */
92 public function itIsPossibleToRemoveEntryFromCache() {
93 $backend = $this->setUpBackend();
94 $data = 'Some data';
95 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
96 $backend->set($identifier, $data);
97 $backend->remove($identifier);
98 $inCache = $backend->has($identifier);
99 $this->assertFalse($inCache, 'Failed to set and remove data from APC backend');
100 }
101
102 /**
103 * @test
104 */
105 public function itIsPossibleToOverwriteAnEntryInTheCache() {
106 $backend = $this->setUpBackend();
107 $data = 'Some data';
108 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
109 $backend->set($identifier, $data);
110 $otherData = 'some other data';
111 $backend->set($identifier, $otherData);
112 $fetchedData = $backend->get($identifier);
113 $this->assertEquals($otherData, $fetchedData, 'APC backend failed to overwrite and retrieve data');
114 }
115
116 /**
117 * @test
118 */
119 public function findIdentifiersByTagFindsSetEntries() {
120 $backend = $this->setUpBackend();
121 $data = 'Some data';
122 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
123 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
124 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
125 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
126 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
127 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
128 }
129
130 /**
131 * @test
132 */
133 public function setRemovesTagsFromPreviousSet() {
134 $backend = $this->setUpBackend();
135 $data = 'Some data';
136 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
137 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
138 $backend->set($identifier, $data, array('UnitTestTag%tag3'));
139 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
140 $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
141 }
142
143 /**
144 * @test
145 */
146 public function setCacheIsSettingIdentifierPrefixWithCacheIdentifier() {
147 $cacheMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
148 $cacheMock->expects($this->any())->method('getIdentifier')->will($this->returnValue(
149 'testidentifier'
150 ));
151
152 /** @var $backendMock \TYPO3\CMS\Core\Cache\Backend\ApcBackend */
153 $backendMock = $this->getMock(
154 'TYPO3\\CMS\\Core\\Cache\\Backend\\ApcBackend',
155 array('setIdentifierPrefix','getCurrentUserData','getPathSite'),
156 array('testcontext')
157 );
158
159 $backendMock->expects($this->once())->method('getCurrentUserData')->will(
160 $this->returnValue(array('name' => 'testname'))
161 );
162
163 $backendMock->expects($this->once())->method('getPathSite')->will(
164 $this->returnValue('testpath')
165 );
166
167 $expectedIdentifier = 'TYPO3_' . \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testpath' . 'testname' . 'testcontext' . 'testidentifier', 12);
168 $backendMock->expects($this->once())->method('setIdentifierPrefix')->with($expectedIdentifier);
169 $backendMock->setCache($cacheMock);
170 }
171
172 /**
173 * @test
174 */
175 public function hasReturnsFalseIfTheEntryDoesntExist() {
176 $backend = $this->setUpBackend();
177 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
178 $inCache = $backend->has($identifier);
179 $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
180 }
181
182 /**
183 * @test
184 */
185 public function removeReturnsFalseIfTheEntryDoesntExist() {
186 $backend = $this->setUpBackend();
187 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
188 $inCache = $backend->remove($identifier);
189 $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
190 }
191
192 /**
193 * @test
194 */
195 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
196 if (version_compare(phpversion(), '5.4.0', '<')) {
197 $this->markTestSkipped('This test is not reliable with PHP version below 5.4.0');
198 }
199 $backend = $this->setUpBackend();
200 $data = 'some data' . microtime();
201 $backend->set('BackendAPCTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
202 $backend->set('BackendAPCTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
203 $backend->set('BackendAPCTest3', $data, array('UnitTestTag%test'));
204 $backend->flushByTag('UnitTestTag%special');
205 $this->assertTrue($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
206 $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
207 $this->assertTrue($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
208 }
209
210 /**
211 * @test
212 */
213 public function flushRemovesAllCacheEntries() {
214 $backend = $this->setUpBackend();
215 $data = 'some data' . microtime();
216 $backend->set('BackendAPCTest1', $data);
217 $backend->set('BackendAPCTest2', $data);
218 $backend->set('BackendAPCTest3', $data);
219 $backend->flush();
220 $this->assertFalse($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
221 $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
222 $this->assertFalse($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
223 }
224
225 /**
226 * @test
227 */
228 public function flushRemovesOnlyOwnEntries() {
229 $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
230 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
231 $thisBackend = new \TYPO3\CMS\Core\Cache\Backend\ApcBackend('Testing');
232 $thisBackend->setCache($thisCache);
233 $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
234 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
235 $thatBackend = new \TYPO3\CMS\Core\Cache\Backend\ApcBackend('Testing');
236 $thatBackend->setCache($thatCache);
237 $thisBackend->set('thisEntry', 'Hello');
238 $thatBackend->set('thatEntry', 'World!');
239 $thatBackend->flush();
240 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
241 $this->assertFalse($thatBackend->has('thatEntry'));
242 }
243
244 /**
245 * Check if we can store ~5 MB of data
246 *
247 * @test
248 */
249 public function largeDataIsStored() {
250 $backend = $this->setUpBackend();
251 $data = str_repeat('abcde', 1024 * 1024);
252 $identifier = 'tooLargeData' . md5(uniqid(mt_rand(), TRUE));
253 $backend->set($identifier, $data);
254 $this->assertTrue($backend->has($identifier));
255 $this->assertEquals($backend->get($identifier), $data);
256 }
257
258 /**
259 * Sets up the APC backend used for testing
260 *
261 * @return \TYPO3\CMS\Core\Cache\Backend\ApcBackend
262 */
263 protected function setUpBackend() {
264 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
265 $backend = new \TYPO3\CMS\Core\Cache\Backend\ApcBackend('Testing');
266 $backend->setCache($cache);
267 return $backend;
268 }
269
270 }
271
272 ?>