4d48efc60265f225a3ed7733654d816580eba1e9
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / FileBackendTest.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 use org\bovigo\vfs\vfsStreamDirectory;
18 use org\bovigo\vfs\vfsStreamWrapper;
19
20 /**
21 * Testcase for the File cache backend
22 *
23 * This file is a backport from FLOW3
24 */
25 class FileBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
26 {
27 /**
28 * Sets up this testcase
29 *
30 * @return void
31 */
32 protected function setUp()
33 {
34 if (!class_exists('org\\bovigo\\vfs\\vfsStreamWrapper')) {
35 $this->markTestSkipped('File backend tests are not available with this phpunit version.');
36 }
37
38 vfsStreamWrapper::register();
39 vfsStreamWrapper::setRoot(new vfsStreamDirectory('Foo'));
40 }
41
42 /**
43 * @test
44 * @expectedException \TYPO3\CMS\Core\Cache\Exception
45 */
46 public function setCacheDirectoryThrowsExceptionOnNonWritableDirectory()
47 {
48 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
49
50 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
51 $backend->setCacheDirectory('http://localhost/');
52
53 $backend->setCache($mockCache);
54 }
55
56 /**
57 * @test
58 */
59 public function setCacheDirectoryAllowsAbsolutePathWithoutTrailingSlash()
60 {
61 $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
62 $backend->_set('cacheIdentifier', 'test');
63 $backend->setCacheDirectory('/tmp/foo');
64 $this->assertEquals('/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
65 }
66
67 /**
68 * @test
69 */
70 public function setCacheDirectoryAllowsAbsolutePathWithTrailingSlash()
71 {
72 $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
73 $backend->_set('cacheIdentifier', 'test');
74 $backend->setCacheDirectory('/tmp/foo/');
75 $this->assertEquals('/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
76 }
77
78 /**
79 * @test
80 */
81 public function setCacheDirectoryAllowsRelativePathWithoutTrailingSlash()
82 {
83 $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
84 $backend->_set('cacheIdentifier', 'test');
85 $backend->setCacheDirectory('tmp/foo');
86 // get PATH_site without trailing slash
87 $path = realpath(PATH_site);
88 $this->assertEquals($path . '/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
89 }
90
91 /**
92 * @test
93 */
94 public function setCacheDirectoryAllowsRelativePathWithTrailingSlash()
95 {
96 $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
97 $backend->_set('cacheIdentifier', 'test');
98 $backend->setCacheDirectory('tmp/foo/');
99 // get PATH_site without trailing slash
100 $path = realpath(PATH_site);
101 $this->assertEquals($path . '/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
102 }
103
104 /**
105 * @test
106 */
107 public function setCacheDirectoryAllowsRelativeDottedPathWithoutTrailingSlash()
108 {
109 $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
110 $backend->_set('cacheIdentifier', 'test');
111 $backend->setCacheDirectory('../tmp/foo');
112 // get PATH_site without trailing slash
113 $path = realpath(PATH_site);
114 $this->assertEquals($path . '/../tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
115 }
116
117 /**
118 * @test
119 */
120 public function setCacheDirectoryAllowsRelativeDottedPathWithTrailingSlash()
121 {
122 $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
123 $backend->_set('cacheIdentifier', 'test');
124 $backend->setCacheDirectory('../tmp/foo/');
125 // get PATH_site without trailing slash
126 $path = realpath(PATH_site);
127 $this->assertEquals($path . '/../tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
128 }
129
130 /**
131 * @test
132 */
133 public function setCacheDirectoryAllowsAbsoluteDottedPathWithoutTrailingSlash()
134 {
135 $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
136 $backend->_set('cacheIdentifier', 'test');
137 $backend->setCacheDirectory('/tmp/../foo');
138 $this->assertEquals('/tmp/../foo/test/', $backend->_get('temporaryCacheDirectory'));
139 }
140
141 /**
142 * @test
143 */
144 public function setCacheDirectoryAllowsAbsoluteDottedPathWithTrailingSlash()
145 {
146 $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
147 $backend->_set('cacheIdentifier', 'test');
148 $backend->setCacheDirectory('/tmp/../foo/');
149 $this->assertEquals('/tmp/../foo/test/', $backend->_get('temporaryCacheDirectory'));
150 }
151
152 /**
153 * @test
154 */
155 public function getCacheDirectoryReturnsTheCurrentCacheDirectory()
156 {
157 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
158 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
159
160 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
161 $backend->setCacheDirectory('vfs://Foo/');
162 $backend->setCache($mockCache);
163
164 $this->assertEquals('vfs://Foo/Cache/Data/SomeCache/', $backend->getCacheDirectory());
165 }
166
167 /**
168 * @test
169 */
170 public function aDedicatedCacheDirectoryIsUsedForCodeCaches()
171 {
172 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\PhpFrontend::class, array(), array(), '', false);
173 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
174
175 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
176 $backend->setCacheDirectory('vfs://Foo/');
177 $backend->setCache($mockCache);
178
179 $this->assertEquals('vfs://Foo/Cache/Code/SomeCache/', $backend->getCacheDirectory());
180 }
181
182 /**
183 * @test
184 * @expectedException \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
185 */
186 public function setThrowsExceptionIfDataIsNotAString()
187 {
188 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
189
190 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
191 $backend->setCacheDirectory('vfs://Foo/');
192 $backend->setCache($mockCache);
193
194 $backend->set('some identifier', array('not a string'));
195 }
196
197 /**
198 * @test
199 */
200 public function setReallySavesToTheSpecifiedDirectory()
201 {
202 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
203 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
204
205 $data = 'some data' . microtime();
206 $entryIdentifier = 'BackendFileTest';
207 $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
208
209 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
210 $backend->setCacheDirectory('vfs://Foo/');
211 $backend->setCache($mockCache);
212
213 $backend->set($entryIdentifier, $data);
214
215 $this->assertFileExists($pathAndFilename);
216 $retrievedData = file_get_contents($pathAndFilename, null, null, 0, strlen($data));
217 $this->assertEquals($data, $retrievedData);
218 }
219
220 /**
221 * @test
222 */
223 public function setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier()
224 {
225 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
226 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
227
228 $data1 = 'some data' . microtime();
229 $data2 = 'some data' . microtime();
230 $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
231
232 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
233 $backend->setCacheDirectory('vfs://Foo/');
234 $backend->setCache($mockCache);
235
236 $backend->set($entryIdentifier, $data1, array(), 500);
237 $backend->set($entryIdentifier, $data2, array(), 200);
238
239 $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
240 $this->assertFileExists($pathAndFilename);
241 $retrievedData = file_get_contents($pathAndFilename, null, null, 0, strlen($data2));
242 $this->assertEquals($data2, $retrievedData);
243 }
244
245 /**
246 * @test
247 */
248 public function setAlsoSavesSpecifiedTags()
249 {
250 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
251 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
252
253 $data = 'some data' . microtime();
254 $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
255
256 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
257 $backend->setCacheDirectory('vfs://Foo/');
258 $backend->setCache($mockCache);
259
260 $backend->set($entryIdentifier, $data, array('Tag1', 'Tag2'));
261
262 $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
263 $this->assertFileExists($pathAndFilename);
264 $retrievedData = file_get_contents($pathAndFilename, null, null, (strlen($data) + \TYPO3\CMS\Core\Cache\Backend\FileBackend::EXPIRYTIME_LENGTH), 9);
265 $this->assertEquals('Tag1 Tag2', $retrievedData);
266 }
267
268 /**
269 * @test
270 */
271 public function setCacheDetectsAndLoadsAFrozenCache()
272 {
273 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
274 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
275
276 $data = 'some data' . microtime();
277 $entryIdentifier = 'BackendFileTest';
278
279 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
280 $backend->setCacheDirectory('vfs://Foo/');
281 $backend->setCache($mockCache);
282
283 $backend->set($entryIdentifier, $data, array('Tag1', 'Tag2'));
284
285 $backend->freeze();
286
287 unset($backend);
288
289 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
290 $backend->setCacheDirectory('vfs://Foo/');
291 $backend->setCache($mockCache);
292
293 $this->assertTrue($backend->isFrozen());
294 $this->assertEquals($data, $backend->get($entryIdentifier));
295 }
296
297 /**
298 * @test
299 */
300 public function getReturnsContentOfTheCorrectCacheFile()
301 {
302 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
303 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
304
305 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('setTag'), array(), '', false);
306 $backend->setCacheDirectory('vfs://Foo/');
307 $backend->setCache($mockCache);
308
309 $entryIdentifier = 'BackendFileTest';
310
311 $data = 'some data' . microtime();
312 $backend->set($entryIdentifier, $data, array(), 500);
313
314 $data = 'some other data' . microtime();
315 $backend->set($entryIdentifier, $data, array(), 100);
316
317 $loadedData = $backend->get($entryIdentifier);
318 $this->assertEquals($data, $loadedData);
319 }
320
321 /**
322 * @test
323 */
324 public function getReturnsFalseForExpiredEntries()
325 {
326 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
327 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
328
329 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('isCacheFileExpired'), array(), '', false);
330 $backend->expects($this->once())->method('isCacheFileExpired')->with('vfs://Foo/Cache/Data/UnitTestCache/ExpiredEntry')->will($this->returnValue(true));
331 $backend->setCacheDirectory('vfs://Foo/');
332 $backend->setCache($mockCache);
333
334 $this->assertFalse($backend->get('ExpiredEntry'));
335 }
336
337 /**
338 * @test
339 */
340 public function getDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen()
341 {
342 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
343 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
344
345 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('isCacheFileExpired'), array(), '', false);
346 $backend->setCacheDirectory('vfs://Foo/');
347 $backend->setCache($mockCache);
348
349 $backend->expects($this->once())->method('isCacheFileExpired');
350
351 $backend->set('foo', 'some data');
352 $backend->freeze();
353 $this->assertEquals('some data', $backend->get('foo'));
354 $this->assertFalse($backend->get('bar'));
355 }
356
357 /**
358 * @test
359 */
360 public function hasReturnsTrueIfAnEntryExists()
361 {
362 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
363 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
364
365 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
366 $backend->setCacheDirectory('vfs://Foo/');
367 $backend->setCache($mockCache);
368
369 $entryIdentifier = 'BackendFileTest';
370
371 $data = 'some data' . microtime();
372 $backend->set($entryIdentifier, $data);
373
374 $this->assertTrue($backend->has($entryIdentifier), 'has() did not return TRUE.');
375 $this->assertFalse($backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
376 }
377
378 /**
379 * @test
380 */
381 public function hasReturnsFalseForExpiredEntries()
382 {
383 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('isCacheFileExpired'), array(), '', false);
384 $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(true, false));
385
386 $this->assertFalse($backend->has('foo'));
387 $this->assertTrue($backend->has('bar'));
388 }
389
390 /**
391 * @test
392 */
393 public function hasDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen()
394 {
395 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
396 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
397
398 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('isCacheFileExpired'), array(), '', false);
399 $backend->setCacheDirectory('vfs://Foo/');
400 $backend->setCache($mockCache);
401
402 $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
403
404 $backend->set('foo', 'some data');
405 $backend->freeze();
406 $this->assertTrue($backend->has('foo'));
407 $this->assertFalse($backend->has('bar'));
408 }
409
410 /**
411 * @test
412 */
413 public function removeReallyRemovesACacheEntry()
414 {
415 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
416 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
417
418 $data = 'some data' . microtime();
419 $entryIdentifier = 'BackendFileTest';
420 $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
421
422 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
423 $backend->setCacheDirectory('vfs://Foo/');
424 $backend->setCache($mockCache);
425
426 $backend->set($entryIdentifier, $data);
427 $this->assertFileExists($pathAndFilename);
428
429 $backend->remove($entryIdentifier);
430 $this->assertFileNotExists($pathAndFilename);
431 }
432
433 /**
434 */
435 public function invalidEntryIdentifiers()
436 {
437 return array(
438 'trailing slash' => array('/myIdentifer'),
439 'trailing dot and slash' => array('./myIdentifer'),
440 'trailing two dots and slash' => array('../myIdentifier'),
441 'trailing with multiple dots and slashes' => array('.././../myIdentifier'),
442 'slash in middle part' => array('my/Identifier'),
443 'dot and slash in middle part' => array('my./Identifier'),
444 'two dots and slash in middle part' => array('my../Identifier'),
445 'multiple dots and slashes in middle part' => array('my.././../Identifier'),
446 'pending slash' => array('myIdentifier/'),
447 'pending dot and slash' => array('myIdentifier./'),
448 'pending dots and slash' => array('myIdentifier../'),
449 'pending multiple dots and slashes' => array('myIdentifier.././../'),
450 );
451 }
452
453 /**
454 * @test
455 * @dataProvider invalidEntryIdentifiers
456 * @expectedException \InvalidArgumentException
457 */
458 public function setThrowsExceptionForInvalidIdentifier($identifier)
459 {
460 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
461 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
462
463 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array('test'), '', true);
464 $backend->setCacheDirectory('vfs://Foo/');
465 $backend->setCache($mockCache);
466
467 $backend->set($identifier, 'cache data', array());
468 }
469
470 /**
471 * @test
472 * @dataProvider invalidEntryIdentifiers
473 * @expectedException \InvalidArgumentException
474 */
475 public function getThrowsExceptionForInvalidIdentifier($identifier)
476 {
477 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
478 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
479
480 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
481 $backend->setCacheDirectory('vfs://Foo/');
482 $backend->setCache($mockCache);
483
484 $backend->get($identifier);
485 }
486
487 /**
488 * @test
489 * @dataProvider invalidEntryIdentifiers
490 * @expectedException \InvalidArgumentException
491 */
492 public function hasThrowsExceptionForInvalidIdentifier($identifier)
493 {
494 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
495
496 $backend->has($identifier);
497 }
498
499 /**
500 * @test
501 * @dataProvider invalidEntryIdentifiers
502 * @expectedException \InvalidArgumentException
503 */
504 public function removeThrowsExceptionForInvalidIdentifier($identifier)
505 {
506 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
507 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
508
509 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
510 $backend->setCacheDirectory('vfs://Foo/');
511 $backend->setCache($mockCache);
512
513 $backend->remove($identifier);
514 }
515
516 /**
517 * @test
518 * @dataProvider invalidEntryIdentifiers
519 * @expectedException \InvalidArgumentException
520 */
521 public function requireOnceThrowsExceptionForInvalidIdentifier($identifier)
522 {
523 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
524 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
525
526 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
527 $backend->setCacheDirectory('vfs://Foo/');
528 $backend->setCache($mockCache);
529
530 $backend->requireOnce($identifier);
531 }
532
533 /**
534 * @test
535 */
536 public function requireOnceIncludesAndReturnsResultOfIncludedPhpFile()
537 {
538 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
539 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
540
541 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
542 $backend->setCacheDirectory('vfs://Foo/');
543 $backend->setCache($mockCache);
544
545 $entryIdentifier = 'SomePhpEntry';
546
547 $data = '<?php return "foo"; ?>';
548 $backend->set($entryIdentifier, $data);
549
550 $loadedData = $backend->requireOnce($entryIdentifier);
551 $this->assertEquals('foo', $loadedData);
552 }
553
554 /**
555 * @test
556 */
557 public function requireOnceDoesNotCheckExpiryTimeIfBackendIsFrozen()
558 {
559 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
560 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
561
562 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('isCacheFileExpired'), array(), '', false);
563 $backend->setCacheDirectory('vfs://Foo/');
564 $backend->setCache($mockCache);
565
566 $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
567
568 $data = '<?php return "foo"; ?>';
569 $backend->set('FooEntry', $data);
570
571 $backend->freeze();
572
573 $loadedData = $backend->requireOnce('FooEntry');
574 $this->assertEquals('foo', $loadedData);
575 }
576
577 /**
578 * @test
579 */
580 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag()
581 {
582 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
583 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
584
585 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
586 $backend->setCacheDirectory('vfs://Foo/');
587 $backend->setCache($mockCache);
588
589 $data = 'some data' . microtime();
590 $backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
591 $backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
592 $backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
593
594 $expectedEntry = 'BackendFileTest2';
595
596 $actualEntries = $backend->findIdentifiersByTag('UnitTestTag%special');
597 $this->assertInternalType('array', $actualEntries);
598 $this->assertEquals($expectedEntry, array_pop($actualEntries));
599 }
600
601 /**
602 * @test
603 */
604 public function findIdentifiersByTagDoesNotReturnExpiredEntries()
605 {
606 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
607 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
608
609 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
610 $backend->setCacheDirectory('vfs://Foo/');
611 $backend->setCache($mockCache);
612
613 $data = 'some data';
614 $backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
615 $backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'), -100);
616 $backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
617
618 $this->assertSame(array(), $backend->findIdentifiersByTag('UnitTestTag%special'));
619 $this->assertSame(array('BackendFileTest1', 'BackendFileTest3'), $backend->findIdentifiersByTag('UnitTestTag%test'));
620 }
621
622 /**
623 * @test
624 */
625 public function flushRemovesAllCacheEntries()
626 {
627 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
628 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
629
630 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
631 $backend->setCacheDirectory('vfs://Foo/');
632 $backend->setCache($mockCache);
633
634 $data = 'some data';
635 $backend->set('BackendFileTest1', $data);
636 $backend->set('BackendFileTest2', $data);
637
638 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
639 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
640
641 $backend->flush();
642
643 $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
644 $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
645 }
646
647 /**
648 * @test
649 */
650 public function flushCreatesCacheDirectoryAgain()
651 {
652 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
653 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
654
655 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
656 $backend->setCacheDirectory('vfs://Foo/');
657 $backend->setCache($mockCache);
658
659 $backend->flush();
660 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/');
661 }
662
663 /**
664 * @test
665 */
666 public function flushByTagRemovesCacheEntriesWithSpecifiedTag()
667 {
668 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('findIdentifiersByTag', 'remove'), array(), '', false);
669
670 $backend->expects($this->once())->method('findIdentifiersByTag')->with('UnitTestTag%special')->will($this->returnValue(array('foo', 'bar', 'baz')));
671 $backend->expects($this->at(1))->method('remove')->with('foo');
672 $backend->expects($this->at(2))->method('remove')->with('bar');
673 $backend->expects($this->at(3))->method('remove')->with('baz');
674
675 $backend->flushByTag('UnitTestTag%special');
676 }
677
678 /**
679 * @test
680 */
681 public function collectGarbageRemovesExpiredCacheEntries()
682 {
683 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
684 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
685
686 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('isCacheFileExpired'), array(), '', false);
687 $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(true, false));
688 $backend->setCacheDirectory('vfs://Foo/');
689 $backend->setCache($mockCache);
690
691 $data = 'some data';
692 $backend->set('BackendFileTest1', $data);
693 $backend->set('BackendFileTest2', $data);
694
695 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
696 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
697
698 $backend->collectGarbage();
699 $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
700 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
701 }
702
703 /**
704 * @test
705 */
706 public function flushUnfreezesTheCache()
707 {
708 $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);
709 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
710
711 $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
712 $backend->setCacheDirectory('vfs://Foo/');
713 $backend->setCache($mockCache);
714
715 $backend->freeze();
716
717 $this->assertTrue($backend->isFrozen());
718 $backend->flush();
719 $this->assertFalse($backend->isFrozen());
720 }
721 }