[TASK] Make PdoBackendTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / PdoBackendTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
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
18 use TYPO3\CMS\Core\Cache\Backend\PdoBackend;
19 use TYPO3\CMS\Core\Cache\Exception;
20 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Testcase for the PDO cache backend
26 */
27 class PdoBackendTest extends UnitTestCase
28 {
29 /**
30 * Sets up this testcase
31 */
32 protected function setUp()
33 {
34 if (!extension_loaded('pdo_sqlite')) {
35 $this->markTestSkipped('pdo_sqlite extension was not available');
36 }
37 }
38
39 /**
40 * @test
41 */
42 public function setThrowsExceptionIfNoFrontEndHasBeenSet()
43 {
44 $this->expectException(Exception::class);
45 $this->expectExceptionCode(1259515600);
46
47 $backend = new PdoBackend('Testing');
48 $data = 'Some data';
49 $identifier = 'MyIdentifier';
50 $backend->set($identifier, $data);
51 }
52
53 /**
54 * @test
55 */
56 public function itIsPossibleToSetAndCheckExistenceInCache()
57 {
58 $backend = $this->setUpBackend();
59 $data = 'Some data';
60 $identifier = 'MyIdentifier';
61 $backend->set($identifier, $data);
62 $this->assertTrue($backend->has($identifier));
63 }
64
65 /**
66 * @test
67 */
68 public function itIsPossibleToSetAndGetEntry()
69 {
70 $backend = $this->setUpBackend();
71 $data = 'Some data';
72 $identifier = 'MyIdentifier';
73 $backend->set($identifier, $data);
74 $fetchedData = $backend->get($identifier);
75 $this->assertEquals($data, $fetchedData);
76 }
77
78 /**
79 * @test
80 */
81 public function itIsPossibleToRemoveEntryFromCache()
82 {
83 $backend = $this->setUpBackend();
84 $data = 'Some data';
85 $identifier = 'MyIdentifier';
86 $backend->set($identifier, $data);
87 $backend->remove($identifier);
88 $this->assertFalse($backend->has($identifier));
89 }
90
91 /**
92 * @test
93 */
94 public function itIsPossibleToOverwriteAnEntryInTheCache()
95 {
96 $backend = $this->setUpBackend();
97 $data = 'Some data';
98 $identifier = 'MyIdentifier';
99 $backend->set($identifier, $data);
100 $otherData = 'some other data';
101 $backend->set($identifier, $otherData);
102 $fetchedData = $backend->get($identifier);
103 $this->assertEquals($otherData, $fetchedData);
104 }
105
106 /**
107 * @test
108 */
109 public function findIdentifiersByTagFindsSetEntries()
110 {
111 $backend = $this->setUpBackend();
112 $data = 'Some data';
113 $entryIdentifier = 'MyIdentifier';
114 $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
115 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
116 $this->assertEquals($entryIdentifier, $retrieved[0]);
117 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
118 $this->assertEquals($entryIdentifier, $retrieved[0]);
119 }
120
121 /**
122 * @test
123 */
124 public function setRemovesTagsFromPreviousSet()
125 {
126 $backend = $this->setUpBackend();
127 $data = 'Some data';
128 $entryIdentifier = 'MyIdentifier';
129 $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
130 $backend->set($entryIdentifier, $data, ['UnitTestTag%tag3']);
131 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
132 $this->assertEquals([], $retrieved);
133 }
134
135 /**
136 * @test
137 */
138 public function setOverwritesExistingEntryThatExceededItsLifetimeWithNewData()
139 {
140 $backend = $this->setUpBackend();
141 $data1 = 'data1';
142 $entryIdentifier = $this->getUniqueId('test');
143 $backend->set($entryIdentifier, $data1, [], 1);
144 $data2 = 'data2';
145 $GLOBALS['EXEC_TIME'] += 2;
146 $backend->set($entryIdentifier, $data2, [], 10);
147 $this->assertEquals($data2, $backend->get($entryIdentifier));
148 }
149
150 /**
151 * @test
152 */
153 public function hasReturnsFalseIfTheEntryDoesntExist()
154 {
155 $backend = $this->setUpBackend();
156 $identifier = 'NonExistingIdentifier';
157 $this->assertFalse($backend->has($identifier));
158 }
159
160 /**
161 * @test
162 */
163 public function removeReturnsFalseIfTheEntryDoesntExist()
164 {
165 $backend = $this->setUpBackend();
166 $identifier = 'NonExistingIdentifier';
167 $this->assertFalse($backend->remove($identifier));
168 }
169
170 /**
171 * @test
172 */
173 public function flushByTagRemovesCacheEntriesWithSpecifiedTag()
174 {
175 $backend = $this->setUpBackend();
176 $data = 'some data' . microtime();
177 $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
178 $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
179 $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
180 $backend->flushByTag('UnitTestTag%special');
181 $this->assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
182 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
183 $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
184 }
185
186 /**
187 * @test
188 */
189 public function flushByTagsRemovesCacheEntriesWithSpecifiedTags()
190 {
191 $backend = $this->setUpBackend();
192 $data = 'some data' . microtime();
193 $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTags%boring']);
194 $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
195 $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
196 $backend->flushByTags(['UnitTestTag%special', 'UnitTestTags%boring']);
197 $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
198 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
199 $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
200 }
201
202 /**
203 * @test
204 */
205 public function flushRemovesAllCacheEntries()
206 {
207 $backend = $this->setUpBackend();
208 $data = 'some data' . microtime();
209 $backend->set('PdoBackendTest1', $data);
210 $backend->set('PdoBackendTest2', $data);
211 $backend->set('PdoBackendTest3', $data);
212 $backend->flush();
213 $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
214 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
215 $this->assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
216 }
217
218 /**
219 * @test
220 */
221 public function flushRemovesOnlyOwnEntries()
222 {
223 $thisCache = $this->createMock(FrontendInterface::class);
224 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
225 $thisBackend = $this->setUpBackend();
226 $thisBackend->setCache($thisCache);
227 $thatCache = $this->createMock(FrontendInterface::class);
228 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
229 $thatBackend = $this->setUpBackend();
230 $thatBackend->setCache($thatCache);
231 $thisBackend->set('thisEntry', 'Hello');
232 $thatBackend->set('thatEntry', 'World!');
233 $thatBackend->flush();
234 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
235 $this->assertFalse($thatBackend->has('thatEntry'));
236 }
237
238 /**
239 * @test
240 */
241 public function collectGarbageReallyRemovesAnExpiredCacheEntry()
242 {
243 $backend = $this->setUpBackend();
244 $data = 'some data' . microtime();
245 $entryIdentifier = 'BackendPDORemovalTest';
246 $backend->set($entryIdentifier, $data, [], 1);
247 $this->assertTrue($backend->has($entryIdentifier));
248 $GLOBALS['EXEC_TIME'] += 2;
249 $backend->collectGarbage();
250 $this->assertFalse($backend->has($entryIdentifier));
251 }
252
253 /**
254 * @test
255 */
256 public function collectGarbageReallyRemovesAllExpiredCacheEntries()
257 {
258 $backend = $this->setUpBackend();
259 $data = 'some data' . microtime();
260 $entryIdentifier = 'BackendPDORemovalTest';
261 $backend->set($entryIdentifier . 'A', $data, [], null);
262 $backend->set($entryIdentifier . 'B', $data, [], 10);
263 $backend->set($entryIdentifier . 'C', $data, [], 1);
264 $backend->set($entryIdentifier . 'D', $data, [], 1);
265 $this->assertTrue($backend->has($entryIdentifier . 'A'));
266 $this->assertTrue($backend->has($entryIdentifier . 'B'));
267 $this->assertTrue($backend->has($entryIdentifier . 'C'));
268 $this->assertTrue($backend->has($entryIdentifier . 'D'));
269 $GLOBALS['EXEC_TIME'] += 2;
270 $backend->collectGarbage();
271 $this->assertTrue($backend->has($entryIdentifier . 'A'));
272 $this->assertTrue($backend->has($entryIdentifier . 'B'));
273 $this->assertFalse($backend->has($entryIdentifier . 'C'));
274 $this->assertFalse($backend->has($entryIdentifier . 'D'));
275 }
276
277 /**
278 * Sets up the PDO backend used for testing
279 *
280 * @return \TYPO3\CMS\Core\Cache\Backend\PdoBackend
281 */
282 protected function setUpBackend()
283 {
284 $mockCache = $this->createMock(FrontendInterface::class);
285 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache'));
286 $backend = GeneralUtility::makeInstance(PdoBackend::class, 'Testing');
287 $backend->setCache($mockCache);
288 $backend->setDataSourceName('sqlite::memory:');
289 $backend->initializeObject();
290 return $backend;
291 }
292 }