‪TYPO3CMS  10.4
ApcuBackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
31 class ‪ApcuBackendTest extends UnitTestCase
32 {
33  protected ‪$resetSingletonInstances = true;
34 
38  protected function ‪setUp(): void
39  {
40  // APCu module is called apcu, but options are prefixed with apc
41  if (!extension_loaded('apcu') || !(bool)ini_get('apc.enabled') || !(bool)ini_get('apc.enable_cli')) {
42  self::markTestSkipped('APCu extension was not available, or it was disabled for CLI.');
43  }
44  if ((bool)ini_get('apc.slam_defense')) {
45  self::markTestSkipped('This testcase can only be executed with apc.slam_defense = 0');
46  }
47  }
48 
53  {
54  $backend = new ‪ApcuBackend('Testing');
55  $data = 'Some data';
56  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
57  $this->expectException(Exception::class);
58  $this->expectExceptionCode(1232986118);
59  $backend->set($identifier, $data);
60  }
61 
66  {
67  $backend = $this->‪setUpBackend();
68  $data = 'Some data';
69  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
70  $backend->set($identifier, $data);
71  self::assertTrue($backend->has($identifier));
72  }
73 
78  {
79  $backend = $this->‪setUpBackend();
80  $data = 'Some data';
81  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
82  $backend->set($identifier, $data);
83  $fetchedData = $backend->get($identifier);
84  self::assertEquals($data, $fetchedData);
85  }
86 
91  {
92  $backend = $this->‪setUpBackend();
93  $data = 'Some data';
94  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
95  $backend->set($identifier, $data);
96  $backend->remove($identifier);
97  self::assertFalse($backend->has($identifier));
98  }
99 
104  {
105  $backend = $this->‪setUpBackend();
106  $data = 'Some data';
107  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
108  $backend->set($identifier, $data);
109  $otherData = 'some other data';
110  $backend->set($identifier, $otherData);
111  $fetchedData = $backend->get($identifier);
112  self::assertEquals($otherData, $fetchedData);
113  }
114 
119  {
120  $backend = $this->‪setUpBackend();
121  $data = 'Some data';
122  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
123  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
124  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
125  self::assertEquals($identifier, $retrieved[0]);
126  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
127  self::assertEquals($identifier, $retrieved[0]);
128  }
129 
134  {
135  $backend = $this->‪setUpBackend();
136  $data = 'Some data';
137  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
138  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
139  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
140  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
141  self::assertEquals([], $retrieved);
142  }
143 
148  {
149  $backend = $this->‪setUpBackend();
150  $identifier = ‪StringUtility::getUniqueId('NonExistingIdentifier');
151  self::assertFalse($backend->has($identifier));
152  }
153 
158  {
159  $backend = $this->‪setUpBackend();
160  $identifier = ‪StringUtility::getUniqueId('NonExistingIdentifier');
161  self::assertFalse($backend->remove($identifier));
162  }
163 
168  {
169  $backend = $this->‪setUpBackend();
170  $data = 'some data' . microtime();
171  $backend->set('BackendAPCUTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
172  $backend->set('BackendAPCUTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
173  $backend->set('BackendAPCUTest3', $data, ['UnitTestTag%test']);
174  $backend->flushByTag('UnitTestTag%special');
175  self::assertTrue($backend->has('BackendAPCUTest1'));
176  self::assertFalse($backend->has('BackendAPCUTest2'));
177  self::assertTrue($backend->has('BackendAPCUTest3'));
178  }
179 
184  {
185  $backend = $this->‪setUpBackend();
186  $data = 'some data' . microtime();
187  $backend->set('BackendAPCUTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
188  $backend->set('BackendAPCUTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
189  $backend->set('BackendAPCUTest3', $data, ['UnitTestTag%test']);
190  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTag%boring']);
191  self::assertFalse($backend->has('BackendAPCUTest1'), 'BackendAPCTest1');
192  self::assertFalse($backend->has('BackendAPCUTest2'), 'BackendAPCTest2');
193  self::assertTrue($backend->has('BackendAPCUTest3'), 'BackendAPCTest3');
194  }
195 
200  {
201  $backend = $this->‪setUpBackend();
202  $data = 'some data' . microtime();
203  $backend->set('BackendAPCUTest1', $data);
204  $backend->set('BackendAPCUTest2', $data);
205  $backend->set('BackendAPCUTest3', $data);
206  $backend->flush();
207  self::assertFalse($backend->has('BackendAPCUTest1'));
208  self::assertFalse($backend->has('BackendAPCUTest2'));
209  self::assertFalse($backend->has('BackendAPCUTest3'));
210  }
211 
216  {
218  $thisCache = $this->createMock(FrontendInterface::class);
219  $thisCache->expects(self::any())->method('getIdentifier')->willReturn('thisCache');
220  $thisBackend = new ‪ApcuBackend('Testing');
221  $thisBackend->setCache($thisCache);
222 
224  $thatCache = $this->createMock(FrontendInterface::class);
225  $thatCache->expects(self::any())->method('getIdentifier')->willReturn('thatCache');
226  $thatBackend = new ‪ApcuBackend('Testing');
227  $thatBackend->setCache($thatCache);
228  $thisBackend->set('thisEntry', 'Hello');
229  $thatBackend->set('thatEntry', 'World!');
230  $thatBackend->flush();
231  self::assertEquals('Hello', $thisBackend->get('thisEntry'));
232  self::assertFalse($thatBackend->has('thatEntry'));
233  }
234 
240  public function ‪largeDataIsStored()
241  {
242  $backend = $this->‪setUpBackend();
243  $data = str_repeat('abcde', 1024 * 1024);
244  $identifier = ‪StringUtility::getUniqueId('tooLargeData');
245  $backend->set($identifier, $data);
246  self::assertTrue($backend->has($identifier));
247  self::assertEquals($backend->get($identifier), $data);
248  }
249 
254  {
255  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
256  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
257 
258  $backend = $this->‪setUpBackend(true);
259  $backend->_call('addIdentifierToTags', $identifier, $tags);
260  self::assertSame(
261  $tags,
262  $backend->_call('findTagsByIdentifier', $identifier)
263  );
264 
265  $backend->_call('addIdentifierToTags', $identifier, $tags);
266  self::assertSame(
267  $tags,
268  $backend->_call('findTagsByIdentifier', $identifier)
269  );
270  }
271 
278  protected function ‪setUpBackend($accessible = false)
279  {
281  $cache = $this->createMock(FrontendInterface::class);
282  if ($accessible) {
283  $backend = $this->getAccessibleMock(ApcuBackend::class, ['dummy'], ['Testing']);
284  } else {
285  $backend = new ‪ApcuBackend('Testing');
286  }
287  $backend->setCache($cache);
288  return $backend;
289  }
290 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: ApcuBackendTest.php:167
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: ApcuBackendTest.php:103
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setUpBackend
‪AccessibleObjectInterface ApcuBackend setUpBackend($accessible=false)
Definition: ApcuBackendTest.php:278
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: ApcuBackendTest.php:215
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: ApcuBackendTest.php:157
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: ApcuBackendTest.php:133
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\largeDataIsStored
‪largeDataIsStored()
Definition: ApcuBackendTest.php:240
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: ApcuBackendTest.php:183
‪TYPO3\CMS\Core\Cache\Backend\ApcuBackend
Definition: ApcuBackend.php:45
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest
Definition: ApcuBackendTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: ApcuBackendTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setUp
‪setUp()
Definition: ApcuBackendTest.php:38
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: ApcuBackendTest.php:65
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\hasReturnsFalseIfTheEntryDoesNotExist
‪hasReturnsFalseIfTheEntryDoesNotExist()
Definition: ApcuBackendTest.php:147
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: ApcuBackendTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: ApcuBackendTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\findIdentifiersByTagFindsSetEntries
‪findIdentifiersByTagFindsSetEntries()
Definition: ApcuBackendTest.php:118
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: ApcuBackendTest.php:199
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setTagsOnlyOnceToIdentifier
‪setTagsOnlyOnceToIdentifier()
Definition: ApcuBackendTest.php:253
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: ApcuBackendTest.php:77
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22