‪TYPO3CMS  9.5
ApcBackendTest.php
Go to the documentation of this file.
1 <?php
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 
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20 
27 class ‪ApcBackendTest extends UnitTestCase
28 {
32  protected function ‪setUp()
33  {
34  // Currently APCu identifies itself both as "apcu" and "apc" (for compatibility) although it doesn't provide the APC-opcache functionality
35  if (!extension_loaded('apc') || ini_get('apc.enabled') == 0 || ini_get('apc.enable_cli') == 0) {
36  $this->markTestSkipped('APC/APCu extension was not available, or it was disabled for CLI.');
37  }
38  if (ini_get('apc.slam_defense') == 1) {
39  $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = Off');
40  }
41  }
42 
47  {
48  $backend = new ‪ApcBackend('Testing');
49  $data = 'Some data';
50  $identifier = $this->getUniqueId('MyIdentifier');
51  $this->expectException(\‪TYPO3\CMS\Core\Cache\Exception::class);
52  $this->expectExceptionCode(1232986818);
53  $backend->set($identifier, $data);
54  }
55 
60  {
61  $backend = $this->‪setUpBackend();
62  $data = 'Some data';
63  $identifier = $this->getUniqueId('MyIdentifier');
64  $backend->set($identifier, $data);
65  $inCache = $backend->has($identifier);
66  $this->assertTrue($inCache, 'APC backend failed to set and check entry');
67  }
68 
73  {
74  $backend = $this->‪setUpBackend();
75  $data = 'Some data';
76  $identifier = $this->getUniqueId('MyIdentifier');
77  $backend->set($identifier, $data);
78  $fetchedData = $backend->get($identifier);
79  $this->assertEquals($data, $fetchedData, 'APC backend failed to set and retrieve data');
80  }
81 
86  {
87  $backend = $this->‪setUpBackend();
88  $data = 'Some data';
89  $identifier = $this->getUniqueId('MyIdentifier');
90  $backend->set($identifier, $data);
91  $backend->remove($identifier);
92  $inCache = $backend->has($identifier);
93  $this->assertFalse($inCache, 'Failed to set and remove data from APC backend');
94  }
95 
100  {
101  $backend = $this->‪setUpBackend();
102  $data = 'Some data';
103  $identifier = $this->getUniqueId('MyIdentifier');
104  $backend->set($identifier, $data);
105  $otherData = 'some other data';
106  $backend->set($identifier, $otherData);
107  $fetchedData = $backend->get($identifier);
108  $this->assertEquals($otherData, $fetchedData, 'APC backend failed to overwrite and retrieve data');
109  }
110 
115  {
116  $backend = $this->‪setUpBackend();
117  $data = 'Some data';
118  $identifier = $this->getUniqueId('MyIdentifier');
119  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
120  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
121  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
122  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
123  $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
124  }
125 
130  {
131  $backend = $this->‪setUpBackend();
132  $data = 'Some data';
133  $identifier = $this->getUniqueId('MyIdentifier');
134  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
135  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
136  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
137  $this->assertEquals([], $retrieved, 'Found entry which should no longer exist.');
138  }
139 
144  {
145  $backend = $this->‪setUpBackend();
146  $identifier = $this->getUniqueId('NonExistingIdentifier');
147  $inCache = $backend->has($identifier);
148  $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
149  }
150 
155  {
156  $backend = $this->‪setUpBackend();
157  $identifier = $this->getUniqueId('NonExistingIdentifier');
158  $inCache = $backend->remove($identifier);
159  $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
160  }
161 
166  {
167  $backend = $this->‪setUpBackend();
168  $data = 'some data' . microtime();
169  $backend->set('BackendAPCTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
170  $backend->set('BackendAPCTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
171  $backend->set('BackendAPCTest3', $data, ['UnitTestTag%test']);
172  $backend->flushByTag('UnitTestTag%special');
173  $this->assertTrue($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
174  $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
175  $this->assertTrue($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
176  }
177 
182  {
183  $backend = $this->‪setUpBackend();
184  $data = 'some data' . microtime();
185  $backend->set('BackendAPCTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
186  $backend->set('BackendAPCTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
187  $backend->set('BackendAPCTest3', $data, ['UnitTestTag%test']);
188  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTag%boring']);
189  $this->assertFalse($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
190  $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
191  $this->assertTrue($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
192  }
193 
198  {
199  $backend = $this->‪setUpBackend();
200  $data = 'some data' . microtime();
201  $backend->set('BackendAPCTest1', $data);
202  $backend->set('BackendAPCTest2', $data);
203  $backend->set('BackendAPCTest3', $data);
204  $backend->flush();
205  $this->assertFalse($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
206  $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
207  $this->assertFalse($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
208  }
209 
214  {
216  $thisCache = $this->createMock(FrontendInterface::class);
217  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
218  $thisBackend = new ‪ApcBackend('Testing');
219  $thisBackend->setCache($thisCache);
220 
222  $thatCache = $this->createMock(FrontendInterface::class);
223  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
224  $thatBackend = new ‪ApcBackend('Testing');
225  $thatBackend->setCache($thatCache);
226  $thisBackend->set('thisEntry', 'Hello');
227  $thatBackend->set('thatEntry', 'World!');
228  $thatBackend->flush();
229  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
230  $this->assertFalse($thatBackend->has('thatEntry'));
231  }
232 
238  public function ‪largeDataIsStored()
239  {
240  $backend = $this->‪setUpBackend();
241  $data = str_repeat('abcde', 1024 * 1024);
242  $identifier = $this->getUniqueId('tooLargeData');
243  $backend->set($identifier, $data);
244  $this->assertTrue($backend->has($identifier));
245  $this->assertEquals($backend->get($identifier), $data);
246  }
247 
252  {
253  $identifier = $this->getUniqueId('MyIdentifier');
254  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
255 
256  $backend = $this->‪setUpBackend(true);
257  $backend->_call('addIdentifierToTags', $identifier, $tags);
258  $this->assertSame(
259  $tags,
260  $backend->_call('findTagsByIdentifier', $identifier)
261  );
262 
263  $backend->_call('addIdentifierToTags', $identifier, $tags);
264  $this->assertSame(
265  $tags,
266  $backend->_call('findTagsByIdentifier', $identifier)
267  );
268  }
269 
276  protected function ‪setUpBackend($accessible = false)
277  {
279  $cache = $this->createMock(FrontendInterface::class);
280  if ($accessible) {
281  $accessibleClassName = $this->buildAccessibleProxy(ApcBackend::class);
282  $backend = new $accessibleClassName('Testing');
283  } else {
284  $backend = new ‪ApcBackend('Testing');
285  }
286  $backend->setCache($cache);
287  return $backend;
288  }
289 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: ApcBackendTest.php:72
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\hasReturnsFalseIfTheEntryDoesNotExist
‪hasReturnsFalseIfTheEntryDoesNotExist()
Definition: ApcBackendTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:2
‪TYPO3
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest
Definition: ApcBackendTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: ApcBackendTest.php:165
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: ApcBackendTest.php:154
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\findIdentifiersByTagFindsSetEntries
‪findIdentifiersByTagFindsSetEntries()
Definition: ApcBackendTest.php:114
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: ApcBackendTest.php:59
‪TYPO3\CMS\Core\Cache\Backend\ApcBackend
Definition: ApcBackend.php:44
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: ApcBackendTest.php:197
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\setUp
‪setUp()
Definition: ApcBackendTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: ApcBackendTest.php:181
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\largeDataIsStored
‪largeDataIsStored()
Definition: ApcBackendTest.php:238
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: ApcBackendTest.php:99
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: ApcBackendTest.php:46
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: ApcBackendTest.php:129
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\setUpBackend
‪TYPO3 TestingFramework Core AccessibleObjectInterface ApcBackend setUpBackend($accessible=false)
Definition: ApcBackendTest.php:276
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\setTagsOnlyOnceToIdentifier
‪setTagsOnlyOnceToIdentifier()
Definition: ApcBackendTest.php:251
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: ApcBackendTest.php:213
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: ApcBackendTest.php:85