TYPO3 CMS  TYPO3_7-6
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 
21 {
25  protected function setUp()
26  {
27  if (!extension_loaded('pdo_sqlite')) {
28  $this->markTestSkipped('pdo_sqlite extension was not available');
29  }
30  }
31 
37  {
38  $backend = new \TYPO3\CMS\Core\Cache\Backend\PdoBackend('Testing');
39  $data = 'Some data';
40  $identifier = 'MyIdentifier';
41  $backend->set($identifier, $data);
42  }
43 
48  {
49  $backend = $this->setUpBackend();
50  $data = 'Some data';
51  $identifier = 'MyIdentifier';
52  $backend->set($identifier, $data);
53  $this->assertTrue($backend->has($identifier));
54  }
55 
59  public function itIsPossibleToSetAndGetEntry()
60  {
61  $backend = $this->setUpBackend();
62  $data = 'Some data';
63  $identifier = 'MyIdentifier';
64  $backend->set($identifier, $data);
65  $fetchedData = $backend->get($identifier);
66  $this->assertEquals($data, $fetchedData);
67  }
68 
73  {
74  $backend = $this->setUpBackend();
75  $data = 'Some data';
76  $identifier = 'MyIdentifier';
77  $backend->set($identifier, $data);
78  $backend->remove($identifier);
79  $this->assertFalse($backend->has($identifier));
80  }
81 
86  {
87  $backend = $this->setUpBackend();
88  $data = 'Some data';
89  $identifier = 'MyIdentifier';
90  $backend->set($identifier, $data);
91  $otherData = 'some other data';
92  $backend->set($identifier, $otherData);
93  $fetchedData = $backend->get($identifier);
94  $this->assertEquals($otherData, $fetchedData);
95  }
96 
101  {
102  $backend = $this->setUpBackend();
103  $data = 'Some data';
104  $entryIdentifier = 'MyIdentifier';
105  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
106  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
107  $this->assertEquals($entryIdentifier, $retrieved[0]);
108  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
109  $this->assertEquals($entryIdentifier, $retrieved[0]);
110  }
111 
116  {
117  $backend = $this->setUpBackend();
118  $data = 'Some data';
119  $entryIdentifier = 'MyIdentifier';
120  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
121  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag3']);
122  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
123  $this->assertEquals([], $retrieved);
124  }
125 
130  {
131  $backend = $this->setUpBackend();
132  $data1 = 'data1';
133  $entryIdentifier = $this->getUniqueId('test');
134  $backend->set($entryIdentifier, $data1, [], 1);
135  $data2 = 'data2';
136  $GLOBALS['EXEC_TIME'] += 2;
137  $backend->set($entryIdentifier, $data2, [], 10);
138  $this->assertEquals($data2, $backend->get($entryIdentifier));
139  }
140 
145  {
146  $backend = $this->setUpBackend();
147  $identifier = 'NonExistingIdentifier';
148  $this->assertFalse($backend->has($identifier));
149  }
150 
155  {
156  $backend = $this->setUpBackend();
157  $identifier = 'NonExistingIdentifier';
158  $this->assertFalse($backend->remove($identifier));
159  }
160 
165  {
166  $backend = $this->setUpBackend();
167  $data = 'some data' . microtime();
168  $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
169  $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
170  $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
171  $backend->flushByTag('UnitTestTag%special');
172  $this->assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
173  $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
174  $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
175  }
176 
180  public function flushRemovesAllCacheEntries()
181  {
182  $backend = $this->setUpBackend();
183  $data = 'some data' . microtime();
184  $backend->set('PdoBackendTest1', $data);
185  $backend->set('PdoBackendTest2', $data);
186  $backend->set('PdoBackendTest3', $data);
187  $backend->flush();
188  $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
189  $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
190  $this->assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
191  }
192 
196  public function flushRemovesOnlyOwnEntries()
197  {
198  $thisCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
199  $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
200  $thisBackend = $this->setUpBackend();
201  $thisBackend->setCache($thisCache);
202  $thatCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
203  $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
204  $thatBackend = $this->setUpBackend();
205  $thatBackend->setCache($thatCache);
206  $thisBackend->set('thisEntry', 'Hello');
207  $thatBackend->set('thatEntry', 'World!');
208  $thatBackend->flush();
209  $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
210  $this->assertFalse($thatBackend->has('thatEntry'));
211  }
212 
217  {
218  $backend = $this->setUpBackend();
219  $data = 'some data' . microtime();
220  $entryIdentifier = 'BackendPDORemovalTest';
221  $backend->set($entryIdentifier, $data, [], 1);
222  $this->assertTrue($backend->has($entryIdentifier));
223  $GLOBALS['EXEC_TIME'] += 2;
224  $backend->collectGarbage();
225  $this->assertFalse($backend->has($entryIdentifier));
226  }
227 
232  {
233  $backend = $this->setUpBackend();
234  $data = 'some data' . microtime();
235  $entryIdentifier = 'BackendPDORemovalTest';
236  $backend->set($entryIdentifier . 'A', $data, [], null);
237  $backend->set($entryIdentifier . 'B', $data, [], 10);
238  $backend->set($entryIdentifier . 'C', $data, [], 1);
239  $backend->set($entryIdentifier . 'D', $data, [], 1);
240  $this->assertTrue($backend->has($entryIdentifier . 'A'));
241  $this->assertTrue($backend->has($entryIdentifier . 'B'));
242  $this->assertTrue($backend->has($entryIdentifier . 'C'));
243  $this->assertTrue($backend->has($entryIdentifier . 'D'));
244  $GLOBALS['EXEC_TIME'] += 2;
245  $backend->collectGarbage();
246  $this->assertTrue($backend->has($entryIdentifier . 'A'));
247  $this->assertTrue($backend->has($entryIdentifier . 'B'));
248  $this->assertFalse($backend->has($entryIdentifier . 'C'));
249  $this->assertFalse($backend->has($entryIdentifier . 'D'));
250  }
251 
257  protected function setUpBackend()
258  {
259  $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, [], [], '', false);
260  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache'));
261  $backend = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\Backend\PdoBackend::class, 'Testing');
262  $backend->setCache($mockCache);
263  $backend->setDataSourceName('sqlite::memory:');
264  $backend->initializeObject();
265  return $backend;
266  }
267 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']