[FOLLOWUP][TASK] Doctrine: migrate $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / PdoBackendTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
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
17 /**
18 * Testcase for the PDO cache backend
19 */
20 class PdoBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * Sets up this testcase
24 */
25 protected function setUp()
26 {
27 if (!extension_loaded('pdo_sqlite')) {
28 $this->markTestSkipped('pdo_sqlite extension was not available');
29 }
30 }
31
32 /**
33 * @test
34 * @expectedException \TYPO3\CMS\Core\Cache\Exception
35 */
36 public function setThrowsExceptionIfNoFrontEndHasBeenSet()
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
44 /**
45 * @test
46 */
47 public function itIsPossibleToSetAndCheckExistenceInCache()
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
56 /**
57 * @test
58 */
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
69 /**
70 * @test
71 */
72 public function itIsPossibleToRemoveEntryFromCache()
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
82 /**
83 * @test
84 */
85 public function itIsPossibleToOverwriteAnEntryInTheCache()
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
97 /**
98 * @test
99 */
100 public function findIdentifiersByTagFindsSetEntries()
101 {
102 $backend = $this->setUpBackend();
103 $data = 'Some data';
104 $entryIdentifier = 'MyIdentifier';
105 $backend->set($entryIdentifier, $data, array('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
112 /**
113 * @test
114 */
115 public function setRemovesTagsFromPreviousSet()
116 {
117 $backend = $this->setUpBackend();
118 $data = 'Some data';
119 $entryIdentifier = 'MyIdentifier';
120 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
121 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag3'));
122 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
123 $this->assertEquals(array(), $retrieved);
124 }
125
126 /**
127 * @test
128 */
129 public function setOverwritesExistingEntryThatExceededItsLifetimeWithNewData()
130 {
131 $backend = $this->setUpBackend();
132 $data1 = 'data1';
133 $entryIdentifier = $this->getUniqueId('test');
134 $backend->set($entryIdentifier, $data1, array(), 1);
135 $data2 = 'data2';
136 $GLOBALS['EXEC_TIME'] += 2;
137 $backend->set($entryIdentifier, $data2, array(), 10);
138 $this->assertEquals($data2, $backend->get($entryIdentifier));
139 }
140
141 /**
142 * @test
143 */
144 public function hasReturnsFalseIfTheEntryDoesntExist()
145 {
146 $backend = $this->setUpBackend();
147 $identifier = 'NonExistingIdentifier';
148 $this->assertFalse($backend->has($identifier));
149 }
150
151 /**
152 * @test
153 */
154 public function removeReturnsFalseIfTheEntryDoesntExist()
155 {
156 $backend = $this->setUpBackend();
157 $identifier = 'NonExistingIdentifier';
158 $this->assertFalse($backend->remove($identifier));
159 }
160
161 /**
162 * @test
163 */
164 public function flushByTagRemovesCacheEntriesWithSpecifiedTag()
165 {
166 $backend = $this->setUpBackend();
167 $data = 'some data' . microtime();
168 $backend->set('PdoBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
169 $backend->set('PdoBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
170 $backend->set('PdoBackendTest3', $data, array('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
177 /**
178 * @test
179 */
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
193 /**
194 * @test
195 */
196 public function flushRemovesOnlyOwnEntries()
197 {
198 $thisCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', 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, array(), array(), '', 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
213 /**
214 * @test
215 */
216 public function collectGarbageReallyRemovesAnExpiredCacheEntry()
217 {
218 $backend = $this->setUpBackend();
219 $data = 'some data' . microtime();
220 $entryIdentifier = 'BackendPDORemovalTest';
221 $backend->set($entryIdentifier, $data, array(), 1);
222 $this->assertTrue($backend->has($entryIdentifier));
223 $GLOBALS['EXEC_TIME'] += 2;
224 $backend->collectGarbage();
225 $this->assertFalse($backend->has($entryIdentifier));
226 }
227
228 /**
229 * @test
230 */
231 public function collectGarbageReallyRemovesAllExpiredCacheEntries()
232 {
233 $backend = $this->setUpBackend();
234 $data = 'some data' . microtime();
235 $entryIdentifier = 'BackendPDORemovalTest';
236 $backend->set($entryIdentifier . 'A', $data, array(), null);
237 $backend->set($entryIdentifier . 'B', $data, array(), 10);
238 $backend->set($entryIdentifier . 'C', $data, array(), 1);
239 $backend->set($entryIdentifier . 'D', $data, array(), 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
252 /**
253 * Sets up the PDO backend used for testing
254 *
255 * @return \TYPO3\CMS\Core\Cache\Backend\PdoBackend
256 */
257 protected function setUpBackend()
258 {
259 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', 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 }