[BUGFIX] Add missing namespace parts
[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\Components\TestingFramework\Core\Unit\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 */
35 public function setThrowsExceptionIfNoFrontEndHasBeenSet()
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
46 /**
47 * @test
48 */
49 public function itIsPossibleToSetAndCheckExistenceInCache()
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
58 /**
59 * @test
60 */
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
71 /**
72 * @test
73 */
74 public function itIsPossibleToRemoveEntryFromCache()
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
84 /**
85 * @test
86 */
87 public function itIsPossibleToOverwriteAnEntryInTheCache()
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
99 /**
100 * @test
101 */
102 public function findIdentifiersByTagFindsSetEntries()
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
114 /**
115 * @test
116 */
117 public function setRemovesTagsFromPreviousSet()
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
128 /**
129 * @test
130 */
131 public function setOverwritesExistingEntryThatExceededItsLifetimeWithNewData()
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
143 /**
144 * @test
145 */
146 public function hasReturnsFalseIfTheEntryDoesntExist()
147 {
148 $backend = $this->setUpBackend();
149 $identifier = 'NonExistingIdentifier';
150 $this->assertFalse($backend->has($identifier));
151 }
152
153 /**
154 * @test
155 */
156 public function removeReturnsFalseIfTheEntryDoesntExist()
157 {
158 $backend = $this->setUpBackend();
159 $identifier = 'NonExistingIdentifier';
160 $this->assertFalse($backend->remove($identifier));
161 }
162
163 /**
164 * @test
165 */
166 public function flushByTagRemovesCacheEntriesWithSpecifiedTag()
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
179 /**
180 * @test
181 */
182 public function flushByTagsRemovesCacheEntriesWithSpecifiedTags()
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
195 /**
196 * @test
197 */
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
211 /**
212 * @test
213 */
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
231 /**
232 * @test
233 */
234 public function collectGarbageReallyRemovesAnExpiredCacheEntry()
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
246 /**
247 * @test
248 */
249 public function collectGarbageReallyRemovesAllExpiredCacheEntries()
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
270 /**
271 * Sets up the PDO backend used for testing
272 *
273 * @return \TYPO3\CMS\Core\Cache\Backend\PdoBackend
274 */
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 }