TYPO3 CMS  TYPO3_8-7
PdoBackendTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 class PdoBackendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
25  protected function setUp()
26  {
27  if (!extension_loaded('pdo_sqlite')) {
28  $this->markTestSkipped('pdo_sqlite extension was not available');
29  }
30  }
31 
36  {
37  $this->expectException(\TYPO3\CMS\Core\Cache\Exception::class);
38  $this->expectExceptionCode(1259515600);
39 
40  $backend = new \TYPO3\CMS\Core\Cache\Backend\PdoBackend('Testing');
41  $data = 'Some data';
42  $identifier = 'MyIdentifier';
43  $backend->set($identifier, $data);
44  }
45 
50  {
51  $backend = $this->setUpBackend();
52  $data = 'Some data';
53  $identifier = 'MyIdentifier';
54  $backend->set($identifier, $data);
55  $this->assertTrue($backend->has($identifier));
56  }
57 
61  public function itIsPossibleToSetAndGetEntry()
62  {
63  $backend = $this->setUpBackend();
64  $data = 'Some data';
65  $identifier = 'MyIdentifier';
66  $backend->set($identifier, $data);
67  $fetchedData = $backend->get($identifier);
68  $this->assertEquals($data, $fetchedData);
69  }
70 
75  {
76  $backend = $this->setUpBackend();
77  $data = 'Some data';
78  $identifier = 'MyIdentifier';
79  $backend->set($identifier, $data);
80  $backend->remove($identifier);
81  $this->assertFalse($backend->has($identifier));
82  }
83 
88  {
89  $backend = $this->setUpBackend();
90  $data = 'Some data';
91  $identifier = 'MyIdentifier';
92  $backend->set($identifier, $data);
93  $otherData = 'some other data';
94  $backend->set($identifier, $otherData);
95  $fetchedData = $backend->get($identifier);
96  $this->assertEquals($otherData, $fetchedData);
97  }
98 
103  {
104  $backend = $this->setUpBackend();
105  $data = 'Some data';
106  $entryIdentifier = 'MyIdentifier';
107  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
108  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
109  $this->assertEquals($entryIdentifier, $retrieved[0]);
110  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
111  $this->assertEquals($entryIdentifier, $retrieved[0]);
112  }
113 
118  {
119  $backend = $this->setUpBackend();
120  $data = 'Some data';
121  $entryIdentifier = 'MyIdentifier';
122  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
123  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag3']);
124  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
125  $this->assertEquals([], $retrieved);
126  }
127 
132  {
133  $backend = $this->setUpBackend();
134  $data1 = 'data1';
135  $entryIdentifier = $this->getUniqueId('test');
136  $backend->set($entryIdentifier, $data1, [], 1);
137  $data2 = 'data2';
138  $GLOBALS['EXEC_TIME'] += 2;
139  $backend->set($entryIdentifier, $data2, [], 10);
140  $this->assertEquals($data2, $backend->get($entryIdentifier));
141  }
142 
147  {
148  $backend = $this->setUpBackend();
149  $identifier = 'NonExistingIdentifier';
150  $this->assertFalse($backend->has($identifier));
151  }
152 
157  {
158  $backend = $this->setUpBackend();
159  $identifier = 'NonExistingIdentifier';
160  $this->assertFalse($backend->remove($identifier));
161  }
162 
167  {
168  $backend = $this->setUpBackend();
169  $data = 'some data' . microtime();
170  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
171  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
172  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
173  $backend->flushByTag('UnitTestTag%special');
174  $this->assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
175  $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
176  $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
177  }
178 
183  {
184  $backend = $this->setUpBackend();
185  $data = 'some data' . microtime();
186  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTags%boring']);
187  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
188  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
189  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTags%boring']);
190  $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
191  $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
192  $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
193  }
194 
198  public function flushRemovesAllCacheEntries()
199  {
200  $backend = $this->setUpBackend();
201  $data = 'some data' . microtime();
202  $backend->set('PdoBackendTest1', $data);
203  $backend->set('PdoBackendTest2', $data);
204  $backend->set('PdoBackendTest3', $data);
205  $backend->flush();
206  $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
207  $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
208  $this->assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
209  }
210 
214  public function flushRemovesOnlyOwnEntries()
215  {
216  $thisCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
217  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
218  $thisBackend = $this->setUpBackend();
219  $thisBackend->setCache($thisCache);
220  $thatCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
221  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
222  $thatBackend = $this->setUpBackend();
223  $thatBackend->setCache($thatCache);
224  $thisBackend->set('thisEntry', 'Hello');
225  $thatBackend->set('thatEntry', 'World!');
226  $thatBackend->flush();
227  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
228  $this->assertFalse($thatBackend->has('thatEntry'));
229  }
230 
235  {
236  $backend = $this->setUpBackend();
237  $data = 'some data' . microtime();
238  $entryIdentifier = 'BackendPDORemovalTest';
239  $backend->set($entryIdentifier, $data, [], 1);
240  $this->assertTrue($backend->has($entryIdentifier));
241  $GLOBALS['EXEC_TIME'] += 2;
242  $backend->collectGarbage();
243  $this->assertFalse($backend->has($entryIdentifier));
244  }
245 
250  {
251  $backend = $this->setUpBackend();
252  $data = 'some data' . microtime();
253  $entryIdentifier = 'BackendPDORemovalTest';
254  $backend->set($entryIdentifier . 'A', $data, [], null);
255  $backend->set($entryIdentifier . 'B', $data, [], 10);
256  $backend->set($entryIdentifier . 'C', $data, [], 1);
257  $backend->set($entryIdentifier . 'D', $data, [], 1);
258  $this->assertTrue($backend->has($entryIdentifier . 'A'));
259  $this->assertTrue($backend->has($entryIdentifier . 'B'));
260  $this->assertTrue($backend->has($entryIdentifier . 'C'));
261  $this->assertTrue($backend->has($entryIdentifier . 'D'));
262  $GLOBALS['EXEC_TIME'] += 2;
263  $backend->collectGarbage();
264  $this->assertTrue($backend->has($entryIdentifier . 'A'));
265  $this->assertTrue($backend->has($entryIdentifier . 'B'));
266  $this->assertFalse($backend->has($entryIdentifier . 'C'));
267  $this->assertFalse($backend->has($entryIdentifier . 'D'));
268  }
269 
275  protected function setUpBackend()
276  {
277  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
278  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache'));
279  $backend = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\Backend\PdoBackend::class, 'Testing');
280  $backend->setCache($mockCache);
281  $backend->setDataSourceName('sqlite::memory:');
282  $backend->initializeObject();
283  return $backend;
284  }
285 }
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']