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