‪TYPO3CMS  9.5
PdoBackendTest.php
Go to the documentation of this file.
1 <?php
2 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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
29 class ‪PdoBackendTest extends UnitTestCase
30 {
34  protected ‪$resetSingletonInstances = true;
35 
40  {
41  $this->expectException(Exception::class);
42  $this->expectExceptionCode(1259515600);
43 
44  $backend = new ‪PdoBackend('Testing');
45  $data = 'Some data';
46  $identifier = 'MyIdentifier';
47  $backend->set($identifier, $data);
48  }
49 
54  {
55  $backend = $this->‪setUpBackend();
56  $data = 'Some data';
57  $identifier = 'MyIdentifier';
58  $backend->set($identifier, $data);
59  $this->assertTrue($backend->has($identifier));
60  }
61 
65  public function ‪itIsPossibleToSetAndGetEntry()
66  {
67  $backend = $this->‪setUpBackend();
68  $data = 'Some data';
69  $identifier = 'MyIdentifier';
70  $backend->set($identifier, $data);
71  $fetchedData = $backend->get($identifier);
72  $this->assertEquals($data, $fetchedData);
73  }
74 
79  {
80  $backend = $this->‪setUpBackend();
81  $data = 'Some data';
82  $identifier = 'MyIdentifier';
83  $backend->set($identifier, $data);
84  $backend->remove($identifier);
85  $this->assertFalse($backend->has($identifier));
86  }
87 
92  {
93  $backend = $this->‪setUpBackend();
94  $data = 'Some data';
95  $identifier = 'MyIdentifier';
96  $backend->set($identifier, $data);
97  $otherData = 'some other data';
98  $backend->set($identifier, $otherData);
99  $fetchedData = $backend->get($identifier);
100  $this->assertEquals($otherData, $fetchedData);
101  }
102 
107  {
108  $backend = $this->‪setUpBackend();
109  $data = 'Some data';
110  $entryIdentifier = 'MyIdentifier';
111  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
112  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
113  $this->assertEquals($entryIdentifier, $retrieved[0]);
114  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
115  $this->assertEquals($entryIdentifier, $retrieved[0]);
116  }
117 
121  public function ‪setRemovesTagsFromPreviousSet()
122  {
123  $backend = $this->‪setUpBackend();
124  $data = 'Some data';
125  $entryIdentifier = 'MyIdentifier';
126  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
127  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag3']);
128  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
129  $this->assertEquals([], $retrieved);
130  }
131 
136  {
137  $backend = $this->‪setUpBackend();
138  $data1 = 'data1';
139  $entryIdentifier = $this->getUniqueId('test');
140  $backend->set($entryIdentifier, $data1, [], 1);
141  $data2 = 'data2';
142  ‪$GLOBALS['EXEC_TIME'] += 2;
143  $backend->set($entryIdentifier, $data2, [], 10);
144  $this->assertEquals($data2, $backend->get($entryIdentifier));
145  }
146 
151  {
152  $backend = $this->‪setUpBackend();
153  $identifier = 'NonExistingIdentifier';
154  $this->assertFalse($backend->has($identifier));
155  }
156 
161  {
162  $backend = $this->‪setUpBackend();
163  $identifier = 'NonExistingIdentifier';
164  $this->assertFalse($backend->remove($identifier));
165  }
166 
171  {
172  $backend = $this->‪setUpBackend();
173  $data = 'some data' . microtime();
174  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
175  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
176  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
177  $backend->flushByTag('UnitTestTag%special');
178  $this->assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
179  $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
180  $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
181  }
182 
187  {
188  $backend = $this->‪setUpBackend();
189  $data = 'some data' . microtime();
190  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTags%boring']);
191  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
192  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
193  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTags%boring']);
194  $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
195  $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
196  $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
197  }
198 
202  public function ‪flushRemovesAllCacheEntries()
203  {
204  $backend = $this->‪setUpBackend();
205  $data = 'some data' . microtime();
206  $backend->set('PdoBackendTest1', $data);
207  $backend->set('PdoBackendTest2', $data);
208  $backend->set('PdoBackendTest3', $data);
209  $backend->flush();
210  $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
211  $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
212  $this->assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
213  }
214 
218  public function ‪flushRemovesOnlyOwnEntries()
219  {
220  $thisCache = $this->createMock(FrontendInterface::class);
221  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
222  $thisBackend = $this->‪setUpBackend();
223  $thisBackend->setCache($thisCache);
224  $thatCache = $this->createMock(FrontendInterface::class);
225  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
226  $thatBackend = $this->‪setUpBackend();
227  $thatBackend->setCache($thatCache);
228  $thisBackend->set('thisEntry', 'Hello');
229  $thatBackend->set('thatEntry', 'World!');
230  $thatBackend->flush();
231  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
232  $this->assertFalse($thatBackend->has('thatEntry'));
233  }
234 
239  {
240  $backend = $this->‪setUpBackend();
241  $data = 'some data' . microtime();
242  $entryIdentifier = 'BackendPDORemovalTest';
243  $backend->set($entryIdentifier, $data, [], 1);
244  $this->assertTrue($backend->has($entryIdentifier));
245  ‪$GLOBALS['EXEC_TIME'] += 2;
246  $backend->collectGarbage();
247  $this->assertFalse($backend->has($entryIdentifier));
248  }
249 
254  {
255  $backend = $this->‪setUpBackend();
256  $data = 'some data' . microtime();
257  $entryIdentifier = 'BackendPDORemovalTest';
258  $backend->set($entryIdentifier . 'A', $data, [], null);
259  $backend->set($entryIdentifier . 'B', $data, [], 10);
260  $backend->set($entryIdentifier . 'C', $data, [], 1);
261  $backend->set($entryIdentifier . 'D', $data, [], 1);
262  $this->assertTrue($backend->has($entryIdentifier . 'A'));
263  $this->assertTrue($backend->has($entryIdentifier . 'B'));
264  $this->assertTrue($backend->has($entryIdentifier . 'C'));
265  $this->assertTrue($backend->has($entryIdentifier . 'D'));
266  ‪$GLOBALS['EXEC_TIME'] += 2;
267  $backend->collectGarbage();
268  $this->assertTrue($backend->has($entryIdentifier . 'A'));
269  $this->assertTrue($backend->has($entryIdentifier . 'B'));
270  $this->assertFalse($backend->has($entryIdentifier . 'C'));
271  $this->assertFalse($backend->has($entryIdentifier . 'D'));
272  }
273 
279  protected function ‪setUpBackend()
280  {
281  $mockCache = $this->createMock(FrontendInterface::class);
282  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache'));
283  $backend = GeneralUtility::makeInstance(PdoBackend::class, 'Testing');
284  $backend->setCache($mockCache);
285  $backend->setDataSourceName('sqlite::memory:');
286  $backend->initializeObject();
287  return $backend;
288  }
289 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\collectGarbageReallyRemovesAllExpiredCacheEntries
‪collectGarbageReallyRemovesAllExpiredCacheEntries()
Definition: PdoBackendTest.php:252
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:2
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\findIdentifiersByTagFindsSetEntries
‪findIdentifiersByTagFindsSetEntries()
Definition: PdoBackendTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\itIsPossibleToSetAndGetEntry
‪itIsPossibleToSetAndGetEntry()
Definition: PdoBackendTest.php:64
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\itIsPossibleToOverwriteAnEntryInTheCache
‪itIsPossibleToOverwriteAnEntryInTheCache()
Definition: PdoBackendTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\itIsPossibleToSetAndCheckExistenceInCache
‪itIsPossibleToSetAndCheckExistenceInCache()
Definition: PdoBackendTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\flushRemovesOnlyOwnEntries
‪flushRemovesOnlyOwnEntries()
Definition: PdoBackendTest.php:217
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\setRemovesTagsFromPreviousSet
‪setRemovesTagsFromPreviousSet()
Definition: PdoBackendTest.php:120
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\removeReturnsFalseIfTheEntryDoesntExist
‪removeReturnsFalseIfTheEntryDoesntExist()
Definition: PdoBackendTest.php:159
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: PdoBackendTest.php:169
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:2
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\setThrowsExceptionIfNoFrontEndHasBeenSet
‪setThrowsExceptionIfNoFrontEndHasBeenSet()
Definition: PdoBackendTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest
Definition: PdoBackendTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PdoBackendTest.php:33
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\Cache\Backend\PdoBackend
Definition: PdoBackend.php:27
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\itIsPossibleToRemoveEntryFromCache
‪itIsPossibleToRemoveEntryFromCache()
Definition: PdoBackendTest.php:77
‪$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:278
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\hasReturnsFalseIfTheEntryDoesntExist
‪hasReturnsFalseIfTheEntryDoesntExist()
Definition: PdoBackendTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\flushByTagsRemovesCacheEntriesWithSpecifiedTags
‪flushByTagsRemovesCacheEntriesWithSpecifiedTags()
Definition: PdoBackendTest.php:185
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\setOverwritesExistingEntryThatExceededItsLifetimeWithNewData
‪setOverwritesExistingEntryThatExceededItsLifetimeWithNewData()
Definition: PdoBackendTest.php:134
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: PdoBackendTest.php:201
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\PdoBackendTest\collectGarbageReallyRemovesAnExpiredCacheEntry
‪collectGarbageReallyRemovesAnExpiredCacheEntry()
Definition: PdoBackendTest.php:237