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