‪TYPO3CMS  9.5
ApcuBackendTest.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 
20 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
29 class ‪ApcuBackendTest extends UnitTestCase
30 {
31  protected ‪$resetSingletonInstances = true;
32 
36  protected function ‪setUp()
37  {
38  // APCu module is called apcu, but options are prefixed with apc
39  if (!extension_loaded('apcu') || !(bool)ini_get('apc.enabled') || !(bool)ini_get('apc.enable_cli')) {
40  $this->markTestSkipped('APCu extension was not available, or it was disabled for CLI.');
41  }
42  if ((bool)ini_get('apc.slam_defense')) {
43  $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = 0');
44  }
45  }
46 
51  {
52  $backend = new ‪ApcuBackend('Testing');
53  $data = 'Some data';
54  $identifier = $this->getUniqueId('MyIdentifier');
55  $this->expectException(Exception::class);
56  $this->expectExceptionCode(1232986118);
57  $backend->set($identifier, $data);
58  }
59 
64  {
65  $backend = $this->‪setUpBackend();
66  $data = 'Some data';
67  $identifier = $this->getUniqueId('MyIdentifier');
68  $backend->set($identifier, $data);
69  $this->assertTrue($backend->has($identifier));
70  }
71 
76  {
77  $backend = $this->‪setUpBackend();
78  $data = 'Some data';
79  $identifier = $this->getUniqueId('MyIdentifier');
80  $backend->set($identifier, $data);
81  $fetchedData = $backend->get($identifier);
82  $this->assertEquals($data, $fetchedData);
83  }
84 
89  {
90  $backend = $this->‪setUpBackend();
91  $data = 'Some data';
92  $identifier = $this->getUniqueId('MyIdentifier');
93  $backend->set($identifier, $data);
94  $backend->remove($identifier);
95  $this->assertFalse($backend->has($identifier));
96  }
97 
102  {
103  $backend = $this->‪setUpBackend();
104  $data = 'Some data';
105  $identifier = $this->getUniqueId('MyIdentifier');
106  $backend->set($identifier, $data);
107  $otherData = 'some other data';
108  $backend->set($identifier, $otherData);
109  $fetchedData = $backend->get($identifier);
110  $this->assertEquals($otherData, $fetchedData);
111  }
112 
117  {
118  $backend = $this->‪setUpBackend();
119  $data = 'Some data';
120  $identifier = $this->getUniqueId('MyIdentifier');
121  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
122  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
123  $this->assertEquals($identifier, $retrieved[0]);
124  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
125  $this->assertEquals($identifier, $retrieved[0]);
126  }
127 
132  {
133  $backend = $this->‪setUpBackend();
134  $data = 'Some data';
135  $identifier = $this->getUniqueId('MyIdentifier');
136  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
137  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
138  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
139  $this->assertEquals([], $retrieved);
140  }
141 
146  {
147  $backend = $this->‪setUpBackend();
148  $identifier = $this->getUniqueId('NonExistingIdentifier');
149  $this->assertFalse($backend->has($identifier));
150  }
151 
156  {
157  $backend = $this->‪setUpBackend();
158  $identifier = $this->getUniqueId('NonExistingIdentifier');
159  $this->assertFalse($backend->remove($identifier));
160  }
161 
166  {
167  $backend = $this->‪setUpBackend();
168  $data = 'some data' . microtime();
169  $backend->set('BackendAPCUTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
170  $backend->set('BackendAPCUTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
171  $backend->set('BackendAPCUTest3', $data, ['UnitTestTag%test']);
172  $backend->flushByTag('UnitTestTag%special');
173  $this->assertTrue($backend->has('BackendAPCUTest1'));
174  $this->assertFalse($backend->has('BackendAPCUTest2'));
175  $this->assertTrue($backend->has('BackendAPCUTest3'));
176  }
177 
182  {
183  $backend = $this->‪setUpBackend();
184  $data = 'some data' . microtime();
185  $backend->set('BackendAPCUTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
186  $backend->set('BackendAPCUTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
187  $backend->set('BackendAPCUTest3', $data, ['UnitTestTag%test']);
188  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTag%boring']);
189  $this->assertFalse($backend->has('BackendAPCUTest1'), 'BackendAPCTest1');
190  $this->assertFalse($backend->has('BackendAPCUTest2'), 'BackendAPCTest2');
191  $this->assertTrue($backend->has('BackendAPCUTest3'), 'BackendAPCTest3');
192  }
193 
198  {
199  $backend = $this->‪setUpBackend();
200  $data = 'some data' . microtime();
201  $backend->set('BackendAPCUTest1', $data);
202  $backend->set('BackendAPCUTest2', $data);
203  $backend->set('BackendAPCUTest3', $data);
204  $backend->flush();
205  $this->assertFalse($backend->has('BackendAPCUTest1'));
206  $this->assertFalse($backend->has('BackendAPCUTest2'));
207  $this->assertFalse($backend->has('BackendAPCUTest3'));
208  }
209 
214  {
216  $thisCache = $this->createMock(FrontendInterface::class);
217  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
218  $thisBackend = new ‪ApcuBackend('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 ‪ApcuBackend('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(ApcuBackend::class);
282  $backend = new $accessibleClassName('Testing');
283  } else {
284  $backend = new ‪ApcuBackend('Testing');
285  }
286  $backend->setCache($cache);
287  return $backend;
288  }
289 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:2
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: ApcuBackendTest.php:165
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: ApcuBackendTest.php:101
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setUpBackend
‪AccessibleObjectInterface ApcuBackend setUpBackend($accessible=false)
Definition: ApcuBackendTest.php:276
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: ApcuBackendTest.php:213
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: ApcuBackendTest.php:155
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: ApcuBackendTest.php:131
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\largeDataIsStored
‪largeDataIsStored()
Definition: ApcuBackendTest.php:238
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: ApcuBackendTest.php:181
‪TYPO3\CMS\Core\Cache\Backend\ApcuBackend
Definition: ApcuBackend.php:44
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest
Definition: ApcuBackendTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: ApcuBackendTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setUp
‪setUp()
Definition: ApcuBackendTest.php:36
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:2
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: ApcuBackendTest.php:63
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\hasReturnsFalseIfTheEntryDoesNotExist
‪hasReturnsFalseIfTheEntryDoesNotExist()
Definition: ApcuBackendTest.php:145
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: ApcuBackendTest.php:88
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: ApcuBackendTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\findIdentifiersByTagFindsSetEntries
‪findIdentifiersByTagFindsSetEntries()
Definition: ApcuBackendTest.php:116
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: ApcuBackendTest.php:197
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setTagsOnlyOnceToIdentifier
‪setTagsOnlyOnceToIdentifier()
Definition: ApcuBackendTest.php:251
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: ApcuBackendTest.php:75