‪TYPO3CMS  11.5
ApcuBackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
33 class ‪ApcuBackendTest extends UnitTestCase
34 {
35  protected ‪$resetSingletonInstances = true;
36 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43  // APCu module is called apcu, but options are prefixed with apc
44  if (!extension_loaded('apcu') || !(bool)ini_get('apc.enabled') || !(bool)ini_get('apc.enable_cli')) {
45  self::markTestSkipped('APCu extension was not available, or it was disabled for CLI.');
46  }
47  if ((bool)ini_get('apc.slam_defense')) {
48  self::markTestSkipped('This testcase can only be executed with apc.slam_defense = 0');
49  }
50  }
51 
56  {
57  $backend = new ‪ApcuBackend('Testing');
58  $data = 'Some data';
59  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
60  $this->expectException(Exception::class);
61  $this->expectExceptionCode(1232986118);
62  $backend->set($identifier, $data);
63  }
64 
69  {
70  $backend = $this->‪setUpBackend();
71  $data = 'Some data';
72  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
73  $backend->set($identifier, $data);
74  self::assertTrue($backend->has($identifier));
75  }
76 
80  public function ‪itIsPossibleToSetAndGetEntry(): void
81  {
82  $backend = $this->‪setUpBackend();
83  $data = 'Some data';
84  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
85  $backend->set($identifier, $data);
86  $fetchedData = $backend->get($identifier);
87  self::assertEquals($data, $fetchedData);
88  }
89 
93  public function ‪itIsPossibleToRemoveEntryFromCache(): void
94  {
95  $backend = $this->‪setUpBackend();
96  $data = 'Some data';
97  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
98  $backend->set($identifier, $data);
99  $backend->remove($identifier);
100  self::assertFalse($backend->has($identifier));
101  }
102 
107  {
108  $backend = $this->‪setUpBackend();
109  $data = 'Some data';
110  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
111  $backend->set($identifier, $data);
112  $otherData = 'some other data';
113  $backend->set($identifier, $otherData);
114  $fetchedData = $backend->get($identifier);
115  self::assertEquals($otherData, $fetchedData);
116  }
117 
122  {
123  $backend = $this->‪setUpBackend();
124  $data = 'Some data';
125  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
126  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
127  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
128  self::assertEquals($identifier, $retrieved[0]);
129  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
130  self::assertEquals($identifier, $retrieved[0]);
131  }
132 
136  public function ‪setRemovesTagsFromPreviousSet(): void
137  {
138  $backend = $this->‪setUpBackend();
139  $data = 'Some data';
140  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
141  $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
142  $backend->set($identifier, $data, ['UnitTestTag%tag3']);
143  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
144  self::assertEquals([], $retrieved);
145  }
146 
151  {
152  $backend = $this->‪setUpBackend();
153  $identifier = ‪StringUtility::getUniqueId('NonExistingIdentifier');
154  self::assertFalse($backend->has($identifier));
155  }
156 
161  {
162  $backend = $this->‪setUpBackend();
163  $identifier = ‪StringUtility::getUniqueId('NonExistingIdentifier');
164  self::assertFalse($backend->remove($identifier));
165  }
166 
171  {
172  $backend = $this->‪setUpBackend();
173  $data = 'some data' . microtime();
174  $backend->set('BackendAPCUTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
175  $backend->set('BackendAPCUTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
176  $backend->set('BackendAPCUTest3', $data, ['UnitTestTag%test']);
177  $backend->flushByTag('UnitTestTag%special');
178  self::assertTrue($backend->has('BackendAPCUTest1'));
179  self::assertFalse($backend->has('BackendAPCUTest2'));
180  self::assertTrue($backend->has('BackendAPCUTest3'));
181  }
182 
187  {
188  $backend = $this->‪setUpBackend();
189  $data = 'some data' . microtime();
190  $backend->set('BackendAPCUTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
191  $backend->set('BackendAPCUTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
192  $backend->set('BackendAPCUTest3', $data, ['UnitTestTag%test']);
193  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTag%boring']);
194  self::assertFalse($backend->has('BackendAPCUTest1'), 'BackendAPCTest1');
195  self::assertFalse($backend->has('BackendAPCUTest2'), 'BackendAPCTest2');
196  self::assertTrue($backend->has('BackendAPCUTest3'), 'BackendAPCTest3');
197  }
198 
202  public function ‪flushRemovesAllCacheEntries(): void
203  {
204  $backend = $this->‪setUpBackend();
205  $data = 'some data' . microtime();
206  $backend->set('BackendAPCUTest1', $data);
207  $backend->set('BackendAPCUTest2', $data);
208  $backend->set('BackendAPCUTest3', $data);
209  $backend->flush();
210  self::assertFalse($backend->has('BackendAPCUTest1'));
211  self::assertFalse($backend->has('BackendAPCUTest2'));
212  self::assertFalse($backend->has('BackendAPCUTest3'));
213  }
214 
218  public function ‪flushRemovesOnlyOwnEntries(): void
219  {
220  $thisCache = $this->createMock(FrontendInterface::class);
221  $thisCache->method('getIdentifier')->willReturn('thisCache');
222  $thisBackend = new ‪ApcuBackend('Testing');
223  $thisBackend->setCache($thisCache);
224 
225  $thatCache = $this->createMock(FrontendInterface::class);
226  $thatCache->method('getIdentifier')->willReturn('thatCache');
227  $thatBackend = new ‪ApcuBackend('Testing');
228  $thatBackend->setCache($thatCache);
229  $thisBackend->set('thisEntry', 'Hello');
230  $thatBackend->set('thatEntry', 'World!');
231  $thatBackend->flush();
232  self::assertEquals('Hello', $thisBackend->get('thisEntry'));
233  self::assertFalse($thatBackend->has('thatEntry'));
234  }
235 
241  public function ‪largeDataIsStored(): void
242  {
243  $backend = $this->‪setUpBackend();
244  $data = str_repeat('abcde', 1024 * 1024);
245  $identifier = ‪StringUtility::getUniqueId('tooLargeData');
246  $backend->set($identifier, $data);
247  self::assertTrue($backend->has($identifier));
248  self::assertEquals($backend->get($identifier), $data);
249  }
250 
254  public function ‪setTagsOnlyOnceToIdentifier(): void
255  {
256  $identifier = ‪StringUtility::getUniqueId('MyIdentifier');
257  $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
258 
259  $backend = $this->‪setUpBackend(true);
260  $backend->_call('addIdentifierToTags', $identifier, $tags);
261  self::assertSame(
262  $tags,
263  $backend->_call('findTagsByIdentifier', $identifier)
264  );
265 
266  $backend->_call('addIdentifierToTags', $identifier, $tags);
267  self::assertSame(
268  $tags,
269  $backend->_call('findTagsByIdentifier', $identifier)
270  );
271  }
272 
279  protected function ‪setUpBackend(bool $accessible = false)
280  {
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:18
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: ApcuBackendTest.php:170
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: ApcuBackendTest.php:106
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: ApcuBackendTest.php:218
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: ApcuBackendTest.php:160
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: ApcuBackendTest.php:136
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\largeDataIsStored
‪largeDataIsStored()
Definition: ApcuBackendTest.php:241
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setUpBackend
‪AccessibleObjectInterface ApcuBackend setUpBackend(bool $accessible=false)
Definition: ApcuBackendTest.php:279
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: ApcuBackendTest.php:186
‪TYPO3\CMS\Core\Cache\Backend\ApcuBackend
Definition: ApcuBackend.php:44
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest
Definition: ApcuBackendTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: ApcuBackendTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setUp
‪setUp()
Definition: ApcuBackendTest.php:40
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: ApcuBackendTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\hasReturnsFalseIfTheEntryDoesNotExist
‪hasReturnsFalseIfTheEntryDoesNotExist()
Definition: ApcuBackendTest.php:150
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: ApcuBackendTest.php:93
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: ApcuBackendTest.php:55
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\findIdentifiersByTagFindsSetEntries
‪findIdentifiersByTagFindsSetEntries()
Definition: ApcuBackendTest.php:121
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: ApcuBackendTest.php:202
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\setTagsOnlyOnceToIdentifier
‪setTagsOnlyOnceToIdentifier()
Definition: ApcuBackendTest.php:254
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\ApcuBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: ApcuBackendTest.php:80
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22