‪TYPO3CMS  10.4
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 
43  {
44  $this->expectException(Exception::class);
45  $this->expectExceptionCode(1259515600);
46 
47  $backend = new ‪PdoBackend('Testing');
48  $data = 'Some data';
49  $identifier = 'MyIdentifier';
50  $backend->set($identifier, $data);
51  }
52 
57  {
58  $backend = $this->‪setUpBackend();
59  $data = 'Some data';
60  $identifier = 'MyIdentifier';
61  $backend->set($identifier, $data);
62  self::assertTrue($backend->has($identifier));
63  }
64 
68  public function ‪itIsPossibleToSetAndGetEntry()
69  {
70  $backend = $this->‪setUpBackend();
71  $data = 'Some data';
72  $identifier = 'MyIdentifier';
73  $backend->set($identifier, $data);
74  $fetchedData = $backend->get($identifier);
75  self::assertEquals($data, $fetchedData);
76  }
77 
82  {
83  $backend = $this->‪setUpBackend();
84  $data = 'Some data';
85  $identifier = 'MyIdentifier';
86  $backend->set($identifier, $data);
87  $backend->remove($identifier);
88  self::assertFalse($backend->has($identifier));
89  }
90 
95  {
96  $backend = $this->‪setUpBackend();
97  $data = 'Some data';
98  $identifier = 'MyIdentifier';
99  $backend->set($identifier, $data);
100  $otherData = 'some other data';
101  $backend->set($identifier, $otherData);
102  $fetchedData = $backend->get($identifier);
103  self::assertEquals($otherData, $fetchedData);
104  }
105 
110  {
111  $backend = $this->‪setUpBackend();
112  $data = 'Some data';
113  $entryIdentifier = 'MyIdentifier';
114  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
115  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
116  self::assertEquals($entryIdentifier, $retrieved[0]);
117  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
118  self::assertEquals($entryIdentifier, $retrieved[0]);
119  }
120 
124  public function ‪setRemovesTagsFromPreviousSet()
125  {
126  $backend = $this->‪setUpBackend();
127  $data = 'Some data';
128  $entryIdentifier = 'MyIdentifier';
129  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
130  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag3']);
131  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
132  self::assertEquals([], $retrieved);
133  }
134 
139  {
140  $backend = $this->‪setUpBackend();
141  $data1 = 'data1';
142  $entryIdentifier = ‪StringUtility::getUniqueId('test');
143  $backend->set($entryIdentifier, $data1, [], 1);
144  $data2 = 'data2';
145  ‪$GLOBALS['EXEC_TIME'] += 2;
146  $backend->set($entryIdentifier, $data2, [], 10);
147  self::assertEquals($data2, $backend->get($entryIdentifier));
148  }
149 
154  {
155  $backend = $this->‪setUpBackend();
156  $identifier = 'NonExistingIdentifier';
157  self::assertFalse($backend->has($identifier));
158  }
159 
164  {
165  $backend = $this->‪setUpBackend();
166  $identifier = 'NonExistingIdentifier';
167  self::assertFalse($backend->remove($identifier));
168  }
169 
174  {
175  $backend = $this->‪setUpBackend();
176  $data = 'some data' . microtime();
177  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
178  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
179  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
180  $backend->flushByTag('UnitTestTag%special');
181  self::assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
182  self::assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
183  self::assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
184  }
185 
190  {
191  $backend = $this->‪setUpBackend();
192  $data = 'some data' . microtime();
193  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTags%boring']);
194  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
195  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
196  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTags%boring']);
197  self::assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
198  self::assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
199  self::assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
200  }
201 
205  public function ‪flushRemovesAllCacheEntries()
206  {
207  $backend = $this->‪setUpBackend();
208  $data = 'some data' . microtime();
209  $backend->set('PdoBackendTest1', $data);
210  $backend->set('PdoBackendTest2', $data);
211  $backend->set('PdoBackendTest3', $data);
212  $backend->flush();
213  self::assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
214  self::assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
215  self::assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
216  }
217 
221  public function ‪flushRemovesOnlyOwnEntries()
222  {
223  $thisCache = $this->createMock(FrontendInterface::class);
224  $thisCache->expects(self::any())->method('getIdentifier')->willReturn('thisCache');
225  $thisBackend = $this->‪setUpBackend();
226  $thisBackend->setCache($thisCache);
227  $thatCache = $this->createMock(FrontendInterface::class);
228  $thatCache->expects(self::any())->method('getIdentifier')->willReturn('thatCache');
229  $thatBackend = $this->‪setUpBackend();
230  $thatBackend->setCache($thatCache);
231  $thisBackend->set('thisEntry', 'Hello');
232  $thatBackend->set('thatEntry', 'World!');
233  $thatBackend->flush();
234  self::assertEquals('Hello', $thisBackend->get('thisEntry'));
235  self::assertFalse($thatBackend->has('thatEntry'));
236  }
237 
242  {
243  $backend = $this->‪setUpBackend();
244  $data = 'some data' . microtime();
245  $entryIdentifier = 'BackendPDORemovalTest';
246  $backend->set($entryIdentifier, $data, [], 1);
247  self::assertTrue($backend->has($entryIdentifier));
248  ‪$GLOBALS['EXEC_TIME'] += 2;
249  $backend->collectGarbage();
250  self::assertFalse($backend->has($entryIdentifier));
251  }
252 
257  {
258  $backend = $this->‪setUpBackend();
259  $data = 'some data' . microtime();
260  $entryIdentifier = 'BackendPDORemovalTest';
261  $backend->set($entryIdentifier . 'A', $data, [], null);
262  $backend->set($entryIdentifier . 'B', $data, [], 10);
263  $backend->set($entryIdentifier . 'C', $data, [], 1);
264  $backend->set($entryIdentifier . 'D', $data, [], 1);
265  self::assertTrue($backend->has($entryIdentifier . 'A'));
266  self::assertTrue($backend->has($entryIdentifier . 'B'));
267  self::assertTrue($backend->has($entryIdentifier . 'C'));
268  self::assertTrue($backend->has($entryIdentifier . 'D'));
269  ‪$GLOBALS['EXEC_TIME'] += 2;
270  $backend->collectGarbage();
271  self::assertTrue($backend->has($entryIdentifier . 'A'));
272  self::assertTrue($backend->has($entryIdentifier . 'B'));
273  self::assertFalse($backend->has($entryIdentifier . 'C'));
274  self::assertFalse($backend->has($entryIdentifier . 'D'));
275  }
276 
282  protected function ‪setUpBackend()
283  {
284  $mockCache = $this->createMock(FrontendInterface::class);
285  $mockCache->expects(self::any())->method('getIdentifier')->willReturn('TestCache');
286  $backend = GeneralUtility::makeInstance(PdoBackend::class, 'Testing');
287  $backend->setCache($mockCache);
288  $backend->setDataSourceName('sqlite::memory:');
289  $backend->initializeObject();
290  return $backend;
291  }
292 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\collectGarbageReallyRemovesAllExpiredCacheEntries
‪collectGarbageReallyRemovesAllExpiredCacheEntries()
Definition: PdoBackendTest.php:255
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\findIdentifiersByTagFindsSetEntries
‪findIdentifiersByTagFindsSetEntries()
Definition: PdoBackendTest.php:108
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: PdoBackendTest.php:67
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: PdoBackendTest.php:93
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: PdoBackendTest.php:55
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: PdoBackendTest.php:220
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: PdoBackendTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: PdoBackendTest.php:162
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: PdoBackendTest.php:172
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: PdoBackendTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest
Definition: PdoBackendTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PdoBackendTest.php:36
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Cache\Backend\PdoBackend
Definition: PdoBackend.php:28
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: PdoBackendTest.php:80
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\setUpBackend
‪TYPO3 CMS Core Cache Backend PdoBackend setUpBackend()
Definition: PdoBackendTest.php:281
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\hasReturnsFalseIfTheEntryDoesntExist
‪hasReturnsFalseIfTheEntryDoesntExist()
Definition: PdoBackendTest.php:152
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: PdoBackendTest.php:188
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\setOverwritesExistingEntryThatExceededItsLifetimeWithNewData
‪setOverwritesExistingEntryThatExceededItsLifetimeWithNewData()
Definition: PdoBackendTest.php:137
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: PdoBackendTest.php:204
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\collectGarbageReallyRemovesAnExpiredCacheEntry
‪collectGarbageReallyRemovesAnExpiredCacheEntry()
Definition: PdoBackendTest.php:240