2 declare(strict_types
= 1);
4 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend
;
7 * This file is part of the TYPO3 CMS project.
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
16 * The TYPO3 project - inspiring people to share!
19 use org
\bovigo
\vfs
\vfsStreamDirectory
;
20 use org
\bovigo
\vfs
\vfsStreamWrapper
;
21 use TYPO3\CMS\Core\Cache\Backend\FileBackend
;
22 use TYPO3\CMS\Core\Cache\Exception
;
23 use TYPO3\CMS\Core\Cache\Exception\InvalidDataException
;
24 use TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend
;
25 use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
;
26 use TYPO3\CMS\Core\Core\Environment
;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase
;
30 * Testcase for the File cache backend
32 class FileBackendTest
extends UnitTestCase
35 * Sets up this testcase
36 * @throws \org\bovigo\vfs\vfsStreamException
38 protected function setUp(): void
40 if (!class_exists(vfsStreamWrapper
::class)) {
41 $this->markTestSkipped('File backend tests are not available with this phpunit version.');
44 vfsStreamWrapper
::register();
45 vfsStreamWrapper
::setRoot(new vfsStreamDirectory('Foo'));
52 public function setCacheDirectoryThrowsExceptionOnNonWritableDirectory(): void
54 $this->expectException(Exception
::class);
55 $this->expectExceptionCode(1303669848);
57 $mockCache = $this->createMock(AbstractFrontend
::class);
59 $backend = $this->getMockBuilder(FileBackend
::class)
60 ->setMethods(['dummy'])
61 ->disableOriginalConstructor()
63 $backend->setCacheDirectory('http://localhost/');
65 $backend->setCache($mockCache);
71 public function setCacheDirectoryAllowsAbsolutePathWithoutTrailingSlash(): void
73 $backend = $this->getAccessibleMock(FileBackend
::class, ['dummy'], [], '', false
);
74 $backend->_set('cacheIdentifier', 'test');
75 $backend->setCacheDirectory('/tmp/foo');
76 $this->assertEquals('/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
82 public function setCacheDirectoryAllowsAbsolutePathWithTrailingSlash(): void
84 $backend = $this->getAccessibleMock(FileBackend
::class, ['dummy'], [], '', false
);
85 $backend->_set('cacheIdentifier', 'test');
86 $backend->setCacheDirectory('/tmp/foo/');
87 $this->assertEquals('/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
93 public function setCacheDirectoryAllowsRelativePathWithoutTrailingSlash(): void
95 $backend = $this->getAccessibleMock(FileBackend
::class, ['dummy'], [], '', false
);
96 $backend->_set('cacheIdentifier', 'test');
97 $backend->setCacheDirectory('tmp/foo');
98 $path = Environment
::getProjectPath();
99 $this->assertEquals($path . '/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
105 public function setCacheDirectoryAllowsRelativePathWithTrailingSlash(): void
107 $backend = $this->getAccessibleMock(FileBackend
::class, ['dummy'], [], '', false
);
108 $backend->_set('cacheIdentifier', 'test');
109 $backend->setCacheDirectory('tmp/foo/');
110 $path = Environment
::getProjectPath();
111 $this->assertEquals($path . '/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
117 public function setCacheDirectoryAllowsRelativeDottedPathWithoutTrailingSlash(): void
119 $backend = $this->getAccessibleMock(FileBackend
::class, ['dummy'], [], '', false
);
120 $backend->_set('cacheIdentifier', 'test');
121 $backend->setCacheDirectory('../tmp/foo');
122 $path = Environment
::getProjectPath();
123 $this->assertEquals($path . '/../tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
129 public function setCacheDirectoryAllowsRelativeDottedPathWithTrailingSlash(): void
131 $backend = $this->getAccessibleMock(FileBackend
::class, ['dummy'], [], '', false
);
132 $backend->_set('cacheIdentifier', 'test');
133 $backend->setCacheDirectory('../tmp/foo/');
134 $path = Environment
::getProjectPath();
135 $this->assertEquals($path . '/../tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
141 public function setCacheDirectoryAllowsAbsoluteDottedPathWithoutTrailingSlash(): void
143 $backend = $this->getAccessibleMock(FileBackend
::class, ['dummy'], [], '', false
);
144 $backend->_set('cacheIdentifier', 'test');
145 $backend->setCacheDirectory('/tmp/../foo');
146 $this->assertEquals('/tmp/../foo/test/', $backend->_get('temporaryCacheDirectory'));
152 public function setCacheDirectoryAllowsAbsoluteDottedPathWithTrailingSlash(): void
154 $backend = $this->getAccessibleMock(FileBackend
::class, ['dummy'], [], '', false
);
155 $backend->_set('cacheIdentifier', 'test');
156 $backend->setCacheDirectory('/tmp/../foo/');
157 $this->assertEquals('/tmp/../foo/test/', $backend->_get('temporaryCacheDirectory'));
164 public function getCacheDirectoryReturnsTheCurrentCacheDirectory(): void
166 $mockCache = $this->createMock(AbstractFrontend
::class);
167 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
169 $backend = $this->getMockBuilder(FileBackend
::class)
170 ->setMethods(['dummy'])
171 ->disableOriginalConstructor()
173 $backend->setCacheDirectory('vfs://Foo/');
174 $backend->setCache($mockCache);
176 $this->assertEquals('vfs://Foo/cache/data/SomeCache/', $backend->getCacheDirectory());
183 public function aDedicatedCacheDirectoryIsUsedForCodeCaches(): void
185 $mockCache = $this->createMock(PhpFrontend
::class);
186 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
188 $backend = $this->getMockBuilder(FileBackend
::class)
189 ->setMethods(['dummy'])
190 ->disableOriginalConstructor()
192 $backend->setCacheDirectory('vfs://Foo/');
193 $backend->setCache($mockCache);
195 $this->assertEquals('vfs://Foo/cache/code/SomeCache/', $backend->getCacheDirectory());
202 public function setThrowsExceptionIfDataIsNotAString(): void
204 $this->expectException(InvalidDataException
::class);
205 $this->expectExceptionCode(1204481674);
207 $mockCache = $this->createMock(AbstractFrontend
::class);
209 $backend = $this->getMockBuilder(FileBackend
::class)
210 ->setMethods(['dummy'])
211 ->disableOriginalConstructor()
213 $backend->setCacheDirectory('vfs://Foo/');
214 $backend->setCache($mockCache);
216 $backend->set('some identifier', ['not a string']);
223 public function setReallySavesToTheSpecifiedDirectory(): void
225 $mockCache = $this->createMock(AbstractFrontend
::class);
226 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
228 $data = 'some data' . microtime();
229 $entryIdentifier = 'BackendFileTest';
230 $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
232 $backend = $this->getMockBuilder(FileBackend
::class)
233 ->setMethods(['dummy'])
234 ->disableOriginalConstructor()
236 $backend->setCacheDirectory('vfs://Foo/');
237 $backend->setCache($mockCache);
239 $backend->set($entryIdentifier, $data);
241 $this->assertFileExists($pathAndFilename);
242 $retrievedData = file_get_contents($pathAndFilename, false
, null
, 0, \
strlen($data));
243 $this->assertEquals($data, $retrievedData);
250 public function setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier(): void
252 $mockCache = $this->createMock(AbstractFrontend
::class);
253 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
255 $data1 = 'some data' . microtime();
256 $data2 = 'some data' . microtime();
257 $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
259 $backend = $this->getMockBuilder(FileBackend
::class)
260 ->setMethods(['dummy'])
261 ->disableOriginalConstructor()
263 $backend->setCacheDirectory('vfs://Foo/');
264 $backend->setCache($mockCache);
266 $backend->set($entryIdentifier, $data1, [], 500);
267 $backend->set($entryIdentifier, $data2, [], 200);
269 $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
270 $this->assertFileExists($pathAndFilename);
271 $retrievedData = file_get_contents($pathAndFilename, false
, null
, 0, \
strlen($data2));
272 $this->assertEquals($data2, $retrievedData);
279 public function setAlsoSavesSpecifiedTags(): void
281 $mockCache = $this->createMock(AbstractFrontend
::class);
282 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
284 $data = 'some data' . microtime();
285 $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
287 $backend = $this->getMockBuilder(FileBackend
::class)
288 ->setMethods(['dummy'])
289 ->disableOriginalConstructor()
291 $backend->setCacheDirectory('vfs://Foo/');
292 $backend->setCache($mockCache);
294 $backend->set($entryIdentifier, $data, ['Tag1', 'Tag2']);
296 $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
297 $this->assertFileExists($pathAndFilename);
298 $retrievedData = file_get_contents(
302 \
strlen($data) + FileBackend
::EXPIRYTIME_LENGTH
,
305 $this->assertEquals('Tag1 Tag2', $retrievedData);
312 public function setCacheDetectsAndLoadsAFrozenCache(): void
314 $mockCache = $this->createMock(AbstractFrontend
::class);
315 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
317 $data = 'some data' . microtime();
318 $entryIdentifier = 'BackendFileTest';
320 $backend = $this->getMockBuilder(FileBackend
::class)
321 ->setMethods(['dummy'])
322 ->disableOriginalConstructor()
324 $backend->setCacheDirectory('vfs://Foo/');
325 $backend->setCache($mockCache);
327 $backend->set($entryIdentifier, $data, ['Tag1', 'Tag2']);
333 $backend = $this->getMockBuilder(FileBackend
::class)
334 ->setMethods(['dummy'])
335 ->disableOriginalConstructor()
337 $backend->setCacheDirectory('vfs://Foo/');
338 $backend->setCache($mockCache);
340 $this->assertTrue($backend->isFrozen());
341 $this->assertEquals($data, $backend->get($entryIdentifier));
348 public function getReturnsContentOfTheCorrectCacheFile(): void
350 $mockCache = $this->createMock(AbstractFrontend
::class);
351 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
353 $backend = $this->getMockBuilder(FileBackend
::class)
354 ->setMethods(['setTag'])
355 ->disableOriginalConstructor()
357 $backend->setCacheDirectory('vfs://Foo/');
358 $backend->setCache($mockCache);
360 $entryIdentifier = 'BackendFileTest';
362 $data = 'some data' . microtime();
363 $backend->set($entryIdentifier, $data, [], 500);
365 $data = 'some other data' . microtime();
366 $backend->set($entryIdentifier, $data, [], 100);
368 $loadedData = $backend->get($entryIdentifier);
369 $this->assertEquals($data, $loadedData);
376 public function getReturnsFalseForExpiredEntries(): void
378 $mockCache = $this->createMock(AbstractFrontend
::class);
379 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
381 $backend = $this->getMockBuilder(FileBackend
::class)
382 ->setMethods(['isCacheFileExpired'])
383 ->disableOriginalConstructor()
385 $backend->expects($this->once())->method('isCacheFileExpired')->with('vfs://Foo/cache/data/UnitTestCache/ExpiredEntry')->will($this->returnValue(true
));
386 $backend->setCacheDirectory('vfs://Foo/');
387 $backend->setCache($mockCache);
389 $this->assertFalse($backend->get('ExpiredEntry'));
396 public function getDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen(): void
398 $mockCache = $this->createMock(AbstractFrontend
::class);
399 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
401 $backend = $this->getMockBuilder(FileBackend
::class)
402 ->setMethods(['isCacheFileExpired'])
403 ->disableOriginalConstructor()
405 $backend->setCacheDirectory('vfs://Foo/');
406 $backend->setCache($mockCache);
408 $backend->expects($this->once())->method('isCacheFileExpired');
410 $backend->set('foo', 'some data');
412 $this->assertEquals('some data', $backend->get('foo'));
413 $this->assertFalse($backend->get('bar'));
420 public function hasReturnsTrueIfAnEntryExists(): void
422 $mockCache = $this->createMock(AbstractFrontend
::class);
423 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
425 $backend = $this->getMockBuilder(FileBackend
::class)
426 ->setMethods(['dummy'])
427 ->disableOriginalConstructor()
429 $backend->setCacheDirectory('vfs://Foo/');
430 $backend->setCache($mockCache);
432 $entryIdentifier = 'BackendFileTest';
434 $data = 'some data' . microtime();
435 $backend->set($entryIdentifier, $data);
437 $this->assertTrue($backend->has($entryIdentifier), 'has() did not return TRUE.');
438 $this->assertFalse($backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
444 public function hasReturnsFalseForExpiredEntries(): void
446 $backend = $this->getMockBuilder(FileBackend
::class)
447 ->setMethods(['isCacheFileExpired'])
448 ->disableOriginalConstructor()
450 $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(
455 $this->assertFalse($backend->has('foo'));
456 $this->assertTrue($backend->has('bar'));
463 public function hasDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen(): void
465 $mockCache = $this->createMock(AbstractFrontend
::class);
466 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
468 $backend = $this->getMockBuilder(FileBackend
::class)
469 ->setMethods(['isCacheFileExpired'])
470 ->disableOriginalConstructor()
472 $backend->setCacheDirectory('vfs://Foo/');
473 $backend->setCache($mockCache);
475 $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
477 $backend->set('foo', 'some data');
479 $this->assertTrue($backend->has('foo'));
480 $this->assertFalse($backend->has('bar'));
487 public function removeReallyRemovesACacheEntry(): void
489 $mockCache = $this->createMock(AbstractFrontend
::class);
490 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
492 $data = 'some data' . microtime();
493 $entryIdentifier = 'BackendFileTest';
494 $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
496 $backend = $this->getMockBuilder(FileBackend
::class)
497 ->setMethods(['dummy'])
498 ->disableOriginalConstructor()
500 $backend->setCacheDirectory('vfs://Foo/');
501 $backend->setCache($mockCache);
503 $backend->set($entryIdentifier, $data);
504 $this->assertFileExists($pathAndFilename);
506 $backend->remove($entryIdentifier);
507 $this->assertFileNotExists($pathAndFilename);
512 public function invalidEntryIdentifiers(): array
515 'trailing slash' => ['/myIdentifer'],
516 'trailing dot and slash' => ['./myIdentifer'],
517 'trailing two dots and slash' => ['../myIdentifier'],
518 'trailing with multiple dots and slashes' => ['.././../myIdentifier'],
519 'slash in middle part' => ['my/Identifier'],
520 'dot and slash in middle part' => ['my./Identifier'],
521 'two dots and slash in middle part' => ['my../Identifier'],
522 'multiple dots and slashes in middle part' => ['my.././../Identifier'],
523 'pending slash' => ['myIdentifier/'],
524 'pending dot and slash' => ['myIdentifier./'],
525 'pending dots and slash' => ['myIdentifier../'],
526 'pending multiple dots and slashes' => ['myIdentifier.././../'],
532 * @dataProvider invalidEntryIdentifiers
533 * @param string $identifier
535 * @throws InvalidDataException
537 public function setThrowsExceptionForInvalidIdentifier(string $identifier): void
539 $this->expectException(\InvalidArgumentException
::class);
540 $this->expectExceptionCode(1282073032);
542 $mockCache = $this->createMock(AbstractFrontend
::class);
543 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
545 $backend = $this->getMockBuilder(FileBackend
::class)
546 ->setMethods(['dummy'])
547 ->setConstructorArgs(['test'])
549 $backend->setCacheDirectory('vfs://Foo/');
550 $backend->setCache($mockCache);
552 $backend->set($identifier, 'cache data', []);
557 * @dataProvider invalidEntryIdentifiers
558 * @param string $identifier
561 public function getThrowsExceptionForInvalidIdentifier(string $identifier): void
563 $this->expectException(\InvalidArgumentException
::class);
564 $this->expectExceptionCode(1282073033);
566 $mockCache = $this->createMock(AbstractFrontend
::class);
567 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
569 $backend = $this->getMockBuilder(FileBackend
::class)
570 ->setMethods(['dummy'])
571 ->disableOriginalConstructor()
573 $backend->setCacheDirectory('vfs://Foo/');
574 $backend->setCache($mockCache);
576 $backend->get($identifier);
581 * @dataProvider invalidEntryIdentifiers
582 * @param string $identifier
584 public function hasThrowsExceptionForInvalidIdentifier(string $identifier): void
586 $this->expectException(\InvalidArgumentException
::class);
587 $this->expectExceptionCode(1282073034);
589 $backend = $this->getMockBuilder(FileBackend
::class)
590 ->setMethods(['dummy'])
591 ->disableOriginalConstructor()
594 $backend->has($identifier);
599 * @dataProvider invalidEntryIdentifiers
600 * @param string $identifier
603 public function removeThrowsExceptionForInvalidIdentifier(string $identifier): void
605 $this->expectException(\InvalidArgumentException
::class);
606 $this->expectExceptionCode(1282073035);
608 $mockCache = $this->createMock(AbstractFrontend
::class);
609 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
611 $backend = $this->getMockBuilder(FileBackend
::class)
612 ->setMethods(['dummy'])
613 ->disableOriginalConstructor()
615 $backend->setCacheDirectory('vfs://Foo/');
616 $backend->setCache($mockCache);
618 $backend->remove($identifier);
623 * @dataProvider invalidEntryIdentifiers
624 * @param string $identifier
627 public function requireOnceThrowsExceptionForInvalidIdentifier(string $identifier): void
629 $this->expectException(\InvalidArgumentException
::class);
630 $this->expectExceptionCode(1282073036);
632 $mockCache = $this->createMock(AbstractFrontend
::class);
633 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
635 $backend = $this->getMockBuilder(FileBackend
::class)
636 ->setMethods(['dummy'])
637 ->disableOriginalConstructor()
639 $backend->setCacheDirectory('vfs://Foo/');
640 $backend->setCache($mockCache);
642 $backend->requireOnce($identifier);
649 public function requireOnceIncludesAndReturnsResultOfIncludedPhpFile(): void
651 $mockCache = $this->createMock(AbstractFrontend
::class);
652 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
654 $backend = $this->getMockBuilder(FileBackend
::class)
655 ->setMethods(['dummy'])
656 ->disableOriginalConstructor()
658 $backend->setCacheDirectory('vfs://Foo/');
659 $backend->setCache($mockCache);
661 $entryIdentifier = 'SomePhpEntry';
663 $data = '<?php return "foo"; ?>';
664 $backend->set($entryIdentifier, $data);
666 $loadedData = $backend->requireOnce($entryIdentifier);
667 $this->assertEquals('foo', $loadedData);
674 public function requireOnceDoesNotCheckExpiryTimeIfBackendIsFrozen(): void
676 $mockCache = $this->createMock(AbstractFrontend
::class);
677 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
679 $backend = $this->getMockBuilder(FileBackend
::class)
680 ->setMethods(['isCacheFileExpired'])
681 ->disableOriginalConstructor()
683 $backend->setCacheDirectory('vfs://Foo/');
684 $backend->setCache($mockCache);
686 $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
688 $data = '<?php return "foo"; ?>';
689 $backend->set('FooEntry', $data);
693 $loadedData = $backend->requireOnce('FooEntry');
694 $this->assertEquals('foo', $loadedData);
701 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag(): void
703 $mockCache = $this->createMock(AbstractFrontend
::class);
704 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
706 $backend = $this->getMockBuilder(FileBackend
::class)
707 ->setMethods(['dummy'])
708 ->disableOriginalConstructor()
710 $backend->setCacheDirectory('vfs://Foo/');
711 $backend->setCache($mockCache);
713 $data = 'some data' . microtime();
714 $backend->set('BackendFileTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
715 $backend->set('BackendFileTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
716 $backend->set('BackendFileTest3', $data, ['UnitTestTag%test']);
718 $expectedEntry = 'BackendFileTest2';
720 $actualEntries = $backend->findIdentifiersByTag('UnitTestTag%special');
721 $this->assertInternalType('array', $actualEntries);
722 $this->assertEquals($expectedEntry, array_pop($actualEntries));
729 public function findIdentifiersByTagDoesNotReturnExpiredEntries(): void
731 $mockCache = $this->createMock(AbstractFrontend
::class);
732 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
734 $backend = $this->getMockBuilder(FileBackend
::class)
735 ->setMethods(['dummy'])
736 ->disableOriginalConstructor()
738 $backend->setCacheDirectory('vfs://Foo/');
739 $backend->setCache($mockCache);
742 $backend->set('BackendFileTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
743 $backend->set('BackendFileTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special'], -100);
744 $backend->set('BackendFileTest3', $data, ['UnitTestTag%test']);
746 $this->assertSame([], $backend->findIdentifiersByTag('UnitTestTag%special'));
747 $this->assertSame(['BackendFileTest1', 'BackendFileTest3'], $backend->findIdentifiersByTag('UnitTestTag%test'));
754 public function flushRemovesAllCacheEntries(): void
756 $mockCache = $this->createMock(AbstractFrontend
::class);
757 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
759 $backend = $this->getMockBuilder(FileBackend
::class)
760 ->setMethods(['dummy'])
761 ->disableOriginalConstructor()
763 $backend->setCacheDirectory('vfs://Foo/');
764 $backend->setCache($mockCache);
767 $backend->set('BackendFileTest1', $data);
768 $backend->set('BackendFileTest2', $data);
770 $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
771 $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
775 $this->assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
776 $this->assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
783 public function flushCreatesCacheDirectoryAgain(): void
785 $mockCache = $this->createMock(AbstractFrontend
::class);
786 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
788 $backend = $this->getMockBuilder(FileBackend
::class)
789 ->setMethods(['dummy'])
790 ->disableOriginalConstructor()
792 $backend->setCacheDirectory('vfs://Foo/');
793 $backend->setCache($mockCache);
796 $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/');
802 public function flushByTagRemovesCacheEntriesWithSpecifiedTag(): void
804 $backend = $this->getMockBuilder(FileBackend
::class)
805 ->setMethods(['findIdentifiersByTag', 'remove'])
806 ->disableOriginalConstructor()
809 $backend->expects($this->once())->method('findIdentifiersByTag')->with('UnitTestTag%special')->will($this->returnValue([
814 $backend->expects($this->at(1))->method('remove')->with('foo');
815 $backend->expects($this->at(2))->method('remove')->with('bar');
816 $backend->expects($this->at(3))->method('remove')->with('baz');
818 $backend->flushByTag('UnitTestTag%special');
825 public function collectGarbageRemovesExpiredCacheEntries(): void
827 $mockCache = $this->createMock(AbstractFrontend
::class);
828 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
830 $backend = $this->getMockBuilder(FileBackend
::class)
831 ->setMethods(['isCacheFileExpired'])
832 ->disableOriginalConstructor()
834 $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(
838 $backend->setCacheDirectory('vfs://Foo/');
839 $backend->setCache($mockCache);
842 $backend->set('BackendFileTest1', $data);
843 $backend->set('BackendFileTest2', $data);
845 $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
846 $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
848 $backend->collectGarbage();
849 $this->assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
850 $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
857 public function flushUnfreezesTheCache(): void
859 $mockCache = $this->createMock(AbstractFrontend
::class);
860 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
862 $backend = $this->getMockBuilder(FileBackend
::class)
863 ->setMethods(['dummy'])
864 ->disableOriginalConstructor()
866 $backend->setCacheDirectory('vfs://Foo/');
867 $backend->setCache($mockCache);
871 $this->assertTrue($backend->isFrozen());
873 $this->assertFalse($backend->isFrozen());