‪TYPO3CMS  11.5
PdoBackendTest.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 
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
32 class ‪PdoBackendTest extends UnitTestCase
33 {
37  protected ‪$resetSingletonInstances = true;
38 
39  protected function ‪setUp(): void
40  {
41  parent::setUp();
42  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'secret-encryption-key-test';
43  }
44 
45  protected function ‪tearDown(): void
46  {
47  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']);
48  parent::tearDown();
49  }
50 
54  public function ‪setThrowsExceptionIfNoFrontEndHasBeenSet(): void
55  {
56  $this->expectException(Exception::class);
57  $this->expectExceptionCode(1259515600);
58 
59  $backend = new ‪PdoBackend('Testing');
60  $data = 'Some data';
61  $identifier = 'MyIdentifier';
62  $backend->set($identifier, $data);
63  }
64 
68  public function ‪itIsPossibleToSetAndCheckExistenceInCache(): void
69  {
70  $backend = $this->‪setUpBackend();
71  $data = 'Some data';
72  $identifier = '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 = '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 = 'MyIdentifier';
98  $backend->set($identifier, $data);
99  $backend->remove($identifier);
100  self::assertFalse($backend->has($identifier));
101  }
102 
106  public function ‪itIsPossibleToOverwriteAnEntryInTheCache(): void
107  {
108  $backend = $this->‪setUpBackend();
109  $data = 'Some data';
110  $identifier = '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 
121  public function ‪findIdentifiersByTagFindsSetEntries(): void
122  {
123  $backend = $this->‪setUpBackend();
124  $data = 'Some data';
125  $entryIdentifier = 'MyIdentifier';
126  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
127  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
128  self::assertEquals($entryIdentifier, $retrieved[0]);
129  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
130  self::assertEquals($entryIdentifier, $retrieved[0]);
131  }
132 
136  public function ‪setRemovesTagsFromPreviousSet(): void
137  {
138  $backend = $this->‪setUpBackend();
139  $data = 'Some data';
140  $entryIdentifier = 'MyIdentifier';
141  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
142  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag3']);
143  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
144  self::assertEquals([], $retrieved);
145  }
146 
151  {
152  $backend = $this->‪setUpBackend();
153  $data1 = 'data1';
154  $entryIdentifier = ‪StringUtility::getUniqueId('test');
155  $backend->set($entryIdentifier, $data1, [], 1);
156  $data2 = 'data2';
157  ‪$GLOBALS['EXEC_TIME'] += 2;
158  $backend->set($entryIdentifier, $data2, [], 10);
159  self::assertEquals($data2, $backend->get($entryIdentifier));
160  }
161 
165  public function ‪hasReturnsFalseIfTheEntryDoesntExist(): void
166  {
167  $backend = $this->‪setUpBackend();
168  $identifier = 'NonExistingIdentifier';
169  self::assertFalse($backend->has($identifier));
170  }
171 
175  public function ‪removeReturnsFalseIfTheEntryDoesntExist(): void
176  {
177  $backend = $this->‪setUpBackend();
178  $identifier = 'NonExistingIdentifier';
179  self::assertFalse($backend->remove($identifier));
180  }
181 
186  {
187  $backend = $this->‪setUpBackend();
188  $data = 'some data' . microtime();
189  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
190  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
191  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
192  $backend->flushByTag('UnitTestTag%special');
193  self::assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
194  self::assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
195  self::assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
196  }
197 
202  {
203  $backend = $this->‪setUpBackend();
204  $data = 'some data' . microtime();
205  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTags%boring']);
206  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
207  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
208  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTags%boring']);
209  self::assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
210  self::assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
211  self::assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
212  }
213 
217  public function ‪flushRemovesAllCacheEntries(): void
218  {
219  $backend = $this->‪setUpBackend();
220  $data = 'some data' . microtime();
221  $backend->set('PdoBackendTest1', $data);
222  $backend->set('PdoBackendTest2', $data);
223  $backend->set('PdoBackendTest3', $data);
224  $backend->flush();
225  self::assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
226  self::assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
227  self::assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
228  }
229 
233  public function ‪flushRemovesOnlyOwnEntries(): void
234  {
235  $thisCache = $this->createMock(FrontendInterface::class);
236  $thisCache->method('getIdentifier')->willReturn('thisCache');
237  $thisBackend = $this->‪setUpBackend();
238  $thisBackend->setCache($thisCache);
239  $thatCache = $this->createMock(FrontendInterface::class);
240  $thatCache->method('getIdentifier')->willReturn('thatCache');
241  $thatBackend = $this->‪setUpBackend();
242  $thatBackend->setCache($thatCache);
243  $thisBackend->set('thisEntry', 'Hello');
244  $thatBackend->set('thatEntry', 'World!');
245  $thatBackend->flush();
246  self::assertEquals('Hello', $thisBackend->get('thisEntry'));
247  self::assertFalse($thatBackend->has('thatEntry'));
248  }
249 
254  {
255  $backend = $this->‪setUpBackend();
256  $data = 'some data' . microtime();
257  $entryIdentifier = 'BackendPDORemovalTest';
258  $backend->set($entryIdentifier, $data, [], 1);
259  self::assertTrue($backend->has($entryIdentifier));
260  ‪$GLOBALS['EXEC_TIME'] += 2;
261  $backend->collectGarbage();
262  self::assertFalse($backend->has($entryIdentifier));
263  }
264 
269  {
270  $backend = $this->‪setUpBackend();
271  $data = 'some data' . microtime();
272  $entryIdentifier = 'BackendPDORemovalTest';
273  $backend->set($entryIdentifier . 'A', $data, [], null);
274  $backend->set($entryIdentifier . 'B', $data, [], 10);
275  $backend->set($entryIdentifier . 'C', $data, [], 1);
276  $backend->set($entryIdentifier . 'D', $data, [], 1);
277  self::assertTrue($backend->has($entryIdentifier . 'A'));
278  self::assertTrue($backend->has($entryIdentifier . 'B'));
279  self::assertTrue($backend->has($entryIdentifier . 'C'));
280  self::assertTrue($backend->has($entryIdentifier . 'D'));
281  ‪$GLOBALS['EXEC_TIME'] += 2;
282  $backend->collectGarbage();
283  self::assertTrue($backend->has($entryIdentifier . 'A'));
284  self::assertTrue($backend->has($entryIdentifier . 'B'));
285  self::assertFalse($backend->has($entryIdentifier . 'C'));
286  self::assertFalse($backend->has($entryIdentifier . 'D'));
287  }
288 
294  protected function ‪setUpBackend(): PdoBackend
295  {
296  $mockCache = $this->createMock(FrontendInterface::class);
297  $mockCache->method('getIdentifier')->willReturn('TestCache');
298  $backend = GeneralUtility::makeInstance(PdoBackend::class, 'Testing');
299  $backend->setCache($mockCache);
300  $backend->setDataSourceName('sqlite::memory:');
301  $backend->initializeObject();
302  return $backend;
303  }
304 }
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\findIdentifiersByTagFindsSetEntries
‪findIdentifiersByTagFindsSetEntries()
Definition: PdoBackendTest.php:120
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: PdoBackendTest.php:200
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: PdoBackendTest.php:216
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: PdoBackendTest.php:53
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\setOverwritesExistingEntryThatExceededItsLifetimeWithNewData
‪setOverwritesExistingEntryThatExceededItsLifetimeWithNewData()
Definition: PdoBackendTest.php:149
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: PdoBackendTest.php:92
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\setUpBackend
‪TYPO3 CMS Core Cache Backend PdoBackend setUpBackend()
Definition: PdoBackendTest.php:293
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\tearDown
‪tearDown()
Definition: PdoBackendTest.php:44
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest
Definition: PdoBackendTest.php:33
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\collectGarbageReallyRemovesAllExpiredCacheEntries
‪collectGarbageReallyRemovesAllExpiredCacheEntries()
Definition: PdoBackendTest.php:267
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: PdoBackendTest.php:79
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: PdoBackendTest.php:67
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend
Definition: PdoBackendTest.php:18
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: PdoBackendTest.php:105
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\hasReturnsFalseIfTheEntryDoesntExist
‪hasReturnsFalseIfTheEntryDoesntExist()
Definition: PdoBackendTest.php:164
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Cache\Backend\PdoBackend
Definition: PdoBackend.php:31
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PdoBackendTest.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: PdoBackendTest.php:174
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\setUp
‪setUp()
Definition: PdoBackendTest.php:38
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: PdoBackendTest.php:184
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: PdoBackendTest.php:135
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: PdoBackendTest.php:232
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Backend\PdoBackendTest\collectGarbageReallyRemovesAnExpiredCacheEntry
‪collectGarbageReallyRemovesAnExpiredCacheEntry()
Definition: PdoBackendTest.php:252