[TASK] Use name-resolution instead of strings where possible: 17
[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 use TYPO3\CMS\Core\Cache\Backend\ApcBackend;
19 /**
20 * Testcase for the APC cache backend.
21 *
22 * NOTE: If you want to execute these tests you need to enable apc in
23 * cli context (apc.enable_cli = 1)
24 *
25 * This file is a backport from FLOW3
26 *
27 * @author Ingo Renner <ingo@typo3.org>
28 */
29 class ApcBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
30
31 /**
32 * Sets up this testcase
33 *
34 * @return void
35 */
36 public function setUp() {
37 // Currently APCu identifies itself both as "apcu" and "apc" (for compatibility) although it doesn't provide the APC-opcache functionality
38 if (!extension_loaded('apc') || ini_get('apc.enabled') == 0 || ini_get('apc.enable_cli') == 0) {
39 $this->markTestSkipped('APC/APCu extension was not available, or it was disabled for CLI.');
40 }
41 if (ini_get('apc.slam_defense') == 1) {
42 $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = Off');
43 }
44 }
45
46 /**
47 * @test
48 * @expectedException \TYPO3\CMS\Core\Cache\Exception
49 */
50 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
51 $backend = new ApcBackend('Testing');
52 $data = 'Some data';
53 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
54 $backend->set($identifier, $data);
55 }
56
57 /**
58 * @test
59 */
60 public function itIsPossibleToSetAndCheckExistenceInCache() {
61 $backend = $this->setUpBackend();
62 $data = 'Some data';
63 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
64 $backend->set($identifier, $data);
65 $inCache = $backend->has($identifier);
66 $this->assertTrue($inCache, 'APC backend failed to set and check entry');
67 }
68
69 /**
70 * @test
71 */
72 public function itIsPossibleToSetAndGetEntry() {
73 $backend = $this->setUpBackend();
74 $data = 'Some data';
75 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
76 $backend->set($identifier, $data);
77 $fetchedData = $backend->get($identifier);
78 $this->assertEquals($data, $fetchedData, 'APC backend failed to set and retrieve data');
79 }
80
81 /**
82 * @test
83 */
84 public function itIsPossibleToRemoveEntryFromCache() {
85 $backend = $this->setUpBackend();
86 $data = 'Some data';
87 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
88 $backend->set($identifier, $data);
89 $backend->remove($identifier);
90 $inCache = $backend->has($identifier);
91 $this->assertFalse($inCache, 'Failed to set and remove data from APC backend');
92 }
93
94 /**
95 * @test
96 */
97 public function itIsPossibleToOverwriteAnEntryInTheCache() {
98 $backend = $this->setUpBackend();
99 $data = 'Some data';
100 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
101 $backend->set($identifier, $data);
102 $otherData = 'some other data';
103 $backend->set($identifier, $otherData);
104 $fetchedData = $backend->get($identifier);
105 $this->assertEquals($otherData, $fetchedData, 'APC backend failed to overwrite and retrieve data');
106 }
107
108 /**
109 * @test
110 */
111 public function findIdentifiersByTagFindsSetEntries() {
112 $backend = $this->setUpBackend();
113 $data = 'Some data';
114 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
115 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
116 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
117 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
118 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
119 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
120 }
121
122 /**
123 * @test
124 */
125 public function setRemovesTagsFromPreviousSet() {
126 $backend = $this->setUpBackend();
127 $data = 'Some data';
128 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
129 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
130 $backend->set($identifier, $data, array('UnitTestTag%tag3'));
131 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
132 $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
133 }
134
135 /**
136 * @test
137 */
138 public function setCacheIsSettingIdentifierPrefixWithCacheIdentifier() {
139 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cacheMock */
140 $cacheMock = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
141 $cacheMock->expects($this->any())->method('getIdentifier')->will($this->returnValue(
142 'testidentifier'
143 ));
144
145 /** @var $backendMock \PHPUnit_Framework_MockObject_MockObject|ApcBackend */
146 $backendMock = $this->getMock(
147 ApcBackend::class,
148 array('setIdentifierPrefix','getCurrentUserData','getPathSite'),
149 array('testcontext')
150 );
151
152 $backendMock->expects($this->once())->method('getCurrentUserData')->will(
153 $this->returnValue(array('name' => 'testname'))
154 );
155
156 $backendMock->expects($this->once())->method('getPathSite')->will(
157 $this->returnValue('testpath')
158 );
159
160 $expectedIdentifier = 'TYPO3_' . \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testpath' . 'testname' . 'testcontext' . 'testidentifier', 12);
161 $backendMock->expects($this->once())->method('setIdentifierPrefix')->with($expectedIdentifier);
162 $backendMock->setCache($cacheMock);
163 }
164
165 /**
166 * @test
167 */
168 public function hasReturnsFalseIfTheEntryDoesNotExist() {
169 $backend = $this->setUpBackend();
170 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
171 $inCache = $backend->has($identifier);
172 $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
173 }
174
175 /**
176 * @test
177 */
178 public function removeReturnsFalseIfTheEntryDoesntExist() {
179 $backend = $this->setUpBackend();
180 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
181 $inCache = $backend->remove($identifier);
182 $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
183 }
184
185 /**
186 * @test
187 */
188 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
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 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $thisCache */
220 $thisCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
221 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
222 $thisBackend = new ApcBackend('Testing');
223 $thisBackend->setCache($thisCache);
224
225 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $thatCache */
226 $thatCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
227 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
228 $thatBackend = new ApcBackend('Testing');
229 $thatBackend->setCache($thatCache);
230 $thisBackend->set('thisEntry', 'Hello');
231 $thatBackend->set('thatEntry', 'World!');
232 $thatBackend->flush();
233 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
234 $this->assertFalse($thatBackend->has('thatEntry'));
235 }
236
237 /**
238 * Check if we can store ~5 MB of data
239 *
240 * @test
241 */
242 public function largeDataIsStored() {
243 $backend = $this->setUpBackend();
244 $data = str_repeat('abcde', 1024 * 1024);
245 $identifier = 'tooLargeData' . md5(uniqid(mt_rand(), TRUE));
246 $backend->set($identifier, $data);
247 $this->assertTrue($backend->has($identifier));
248 $this->assertEquals($backend->get($identifier), $data);
249 }
250
251 /**
252 * @test
253 */
254 public function setTagsOnlyOnceToIdentifier() {
255 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
256 $tags = array('UnitTestTag%test', 'UnitTestTag%boring');
257
258 $backend = $this->setUpBackend(TRUE);
259 $backend->_call('addIdentifierToTags', $identifier, $tags);
260 $this->assertSame(
261 $tags,
262 $backend->_call('findTagsByIdentifier', $identifier)
263 );
264
265 $backend->_call('addIdentifierToTags', $identifier, $tags);
266 $this->assertSame(
267 $tags,
268 $backend->_call('findTagsByIdentifier', $identifier)
269 );
270 }
271
272 /**
273 * Sets up the APC backend used for testing
274 *
275 * @param bool $accessible TRUE if backend should be encapsulated in accessible proxy otherwise FALSE.
276 * @return \TYPO3\CMS\Core\Tests\AccessibleObjectInterface|ApcBackend
277 */
278 protected function setUpBackend($accessible = FALSE) {
279 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache */
280 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
281 if ($accessible) {
282 $accessibleClassName = $this->buildAccessibleProxy(ApcBackend::class);
283 $backend = new $accessibleClassName('Testing');
284 } else {
285 $backend = new ApcBackend('Testing');
286 }
287 $backend->setCache($cache);
288 return $backend;
289 }
290
291 }