‪TYPO3CMS  9.5
FileBackendTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
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.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use org\bovigo\vfs\vfsStreamDirectory;
20 use org\bovigo\vfs\vfsStreamWrapper;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪FileBackendTest extends UnitTestCase
33 {
34  protected ‪$resetSingletonInstances = true;
35 
40  protected function ‪setUp(): void
41  {
42  vfsStreamWrapper::register();
43  vfsStreamWrapper::setRoot(new vfsStreamDirectory('Foo'));
44  }
45 
51  {
52  $this->expectException(Exception::class);
53  $this->expectExceptionCode(1303669848);
54 
55  $mockCache = $this->createMock(AbstractFrontend::class);
56 
57  $backend = $this->getMockBuilder(FileBackend::class)
58  ->setMethods(['dummy'])
59  ->disableOriginalConstructor()
60  ->getMock();
61  $backend->setCacheDirectory('http://localhost/');
62 
63  $backend->setCache($mockCache);
64  }
65 
70  {
71  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
72  $backend->setCacheDirectory('/tmp/foo');
73  $this->assertEquals('/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
74  }
75 
80  {
81  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
82  $backend->setCacheDirectory('/tmp/foo/');
83  $this->assertEquals('/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
84  }
85 
90  {
91  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
92  $backend->setCacheDirectory('tmp/foo');
94  $this->assertEquals($path . '/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
95  }
96 
101  {
102  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
103  $backend->setCacheDirectory('tmp/foo/');
105  $this->assertEquals($path . '/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
106  }
107 
112  {
113  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
114  $backend->setCacheDirectory('../tmp/foo');
116  $this->assertEquals($path . '/../tmp/foo/', $backend->_get('temporaryCacheDirectory'));
117  }
118 
123  {
124  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
125  $backend->setCacheDirectory('../tmp/foo/');
127  $this->assertEquals($path . '/../tmp/foo/', $backend->_get('temporaryCacheDirectory'));
128  }
129 
134  {
135  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
136  $backend->setCacheDirectory('/tmp/../foo');
137  $this->assertEquals('/tmp/../foo/', $backend->_get('temporaryCacheDirectory'));
138  }
139 
144  {
145  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
146  $backend->setCacheDirectory('/tmp/../foo/');
147  $this->assertEquals('/tmp/../foo/', $backend->_get('temporaryCacheDirectory'));
148  }
149 
155  {
156  $mockCache = $this->createMock(AbstractFrontend::class);
157  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
158 
159  $backend = $this->getMockBuilder(FileBackend::class)
160  ->setMethods(['dummy'])
161  ->disableOriginalConstructor()
162  ->getMock();
163  $backend->setCacheDirectory('vfs://Foo/');
164  $backend->setCache($mockCache);
165 
166  $this->assertEquals('vfs://Foo/cache/data/SomeCache/', $backend->getCacheDirectory());
167  }
168 
174  {
175  $mockCache = $this->createMock(PhpFrontend::class);
176  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
177 
178  $backend = $this->getMockBuilder(FileBackend::class)
179  ->setMethods(['dummy'])
180  ->disableOriginalConstructor()
181  ->getMock();
182  $backend->setCacheDirectory('vfs://Foo/');
183  $backend->setCache($mockCache);
184 
185  $this->assertEquals('vfs://Foo/cache/code/SomeCache/', $backend->getCacheDirectory());
186  }
187 
193  {
194  $this->expectException(InvalidDataException::class);
195  $this->expectExceptionCode(1204481674);
196 
197  $mockCache = $this->createMock(AbstractFrontend::class);
198 
199  $backend = $this->getMockBuilder(FileBackend::class)
200  ->setMethods(['dummy'])
201  ->disableOriginalConstructor()
202  ->getMock();
203  $backend->setCacheDirectory('vfs://Foo/');
204  $backend->setCache($mockCache);
205 
206  $backend->set('some identifier', ['not a string']);
207  }
208 
214  {
215  $mockCache = $this->createMock(AbstractFrontend::class);
216  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
217 
218  $data = 'some data' . microtime();
219  $entryIdentifier = 'BackendFileTest';
220  $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
221 
222  $backend = $this->getMockBuilder(FileBackend::class)
223  ->setMethods(['dummy'])
224  ->disableOriginalConstructor()
225  ->getMock();
226  $backend->setCacheDirectory('vfs://Foo/');
227  $backend->setCache($mockCache);
228 
229  $backend->set($entryIdentifier, $data);
230 
231  $this->assertFileExists($pathAndFilename);
232  $retrievedData = file_get_contents($pathAndFilename, false, null, 0, \strlen($data));
233  $this->assertEquals($data, $retrievedData);
234  }
235 
241  {
242  $mockCache = $this->createMock(AbstractFrontend::class);
243  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
244 
245  $data1 = 'some data' . microtime();
246  $data2 = 'some data' . microtime();
247  $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
248 
249  $backend = $this->getMockBuilder(FileBackend::class)
250  ->setMethods(['dummy'])
251  ->disableOriginalConstructor()
252  ->getMock();
253  $backend->setCacheDirectory('vfs://Foo/');
254  $backend->setCache($mockCache);
255 
256  $backend->set($entryIdentifier, $data1, [], 500);
257  $backend->set($entryIdentifier, $data2, [], 200);
258 
259  $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
260  $this->assertFileExists($pathAndFilename);
261  $retrievedData = file_get_contents($pathAndFilename, false, null, 0, \strlen($data2));
262  $this->assertEquals($data2, $retrievedData);
263  }
264 
269  public function ‪setAlsoSavesSpecifiedTags(): void
270  {
271  $mockCache = $this->createMock(AbstractFrontend::class);
272  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
273 
274  $data = 'some data' . microtime();
275  $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
276 
277  $backend = $this->getMockBuilder(FileBackend::class)
278  ->setMethods(['dummy'])
279  ->disableOriginalConstructor()
280  ->getMock();
281  $backend->setCacheDirectory('vfs://Foo/');
282  $backend->setCache($mockCache);
283 
284  $backend->set($entryIdentifier, $data, ['Tag1', 'Tag2']);
285 
286  $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
287  $this->assertFileExists($pathAndFilename);
288  $retrievedData = file_get_contents(
289  $pathAndFilename,
290  false,
291  null,
292  \strlen($data) + ‪FileBackend::EXPIRYTIME_LENGTH,
293  9
294  );
295  $this->assertEquals('Tag1 Tag2', $retrievedData);
296  }
297 
303  {
304  $mockCache = $this->createMock(AbstractFrontend::class);
305  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
306 
307  $data = 'some data' . microtime();
308  $entryIdentifier = 'BackendFileTest';
309 
310  $backend = $this->getMockBuilder(FileBackend::class)
311  ->setMethods(['dummy'])
312  ->disableOriginalConstructor()
313  ->getMock();
314  $backend->setCacheDirectory('vfs://Foo/');
315  $backend->setCache($mockCache);
316 
317  $backend->set($entryIdentifier, $data, ['Tag1', 'Tag2']);
318 
319  $backend->freeze();
320 
321  unset($backend);
322 
323  $backend = $this->getMockBuilder(FileBackend::class)
324  ->setMethods(['dummy'])
325  ->disableOriginalConstructor()
326  ->getMock();
327  $backend->setCacheDirectory('vfs://Foo/');
328  $backend->setCache($mockCache);
329 
330  $this->assertTrue($backend->isFrozen());
331  $this->assertEquals($data, $backend->get($entryIdentifier));
332  }
333 
339  {
340  $mockCache = $this->createMock(AbstractFrontend::class);
341  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
342 
343  $backend = $this->getMockBuilder(FileBackend::class)
344  ->setMethods(['setTag'])
345  ->disableOriginalConstructor()
346  ->getMock();
347  $backend->setCacheDirectory('vfs://Foo/');
348  $backend->setCache($mockCache);
349 
350  $entryIdentifier = 'BackendFileTest';
351 
352  $data = 'some data' . microtime();
353  $backend->set($entryIdentifier, $data, [], 500);
354 
355  $data = 'some other data' . microtime();
356  $backend->set($entryIdentifier, $data, [], 100);
357 
358  $loadedData = $backend->get($entryIdentifier);
359  $this->assertEquals($data, $loadedData);
360  }
361 
366  public function ‪getReturnsFalseForExpiredEntries(): void
367  {
368  $mockCache = $this->createMock(AbstractFrontend::class);
369  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
370 
371  $backend = $this->getMockBuilder(FileBackend::class)
372  ->setMethods(['isCacheFileExpired'])
373  ->disableOriginalConstructor()
374  ->getMock();
375  $backend->expects($this->once())->method('isCacheFileExpired')->with('vfs://Foo/cache/data/UnitTestCache/ExpiredEntry')->will($this->returnValue(true));
376  $backend->setCacheDirectory('vfs://Foo/');
377  $backend->setCache($mockCache);
378 
379  $this->assertFalse($backend->get('ExpiredEntry'));
380  }
381 
387  {
388  $mockCache = $this->createMock(AbstractFrontend::class);
389  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
390 
391  $backend = $this->getMockBuilder(FileBackend::class)
392  ->setMethods(['isCacheFileExpired'])
393  ->disableOriginalConstructor()
394  ->getMock();
395  $backend->setCacheDirectory('vfs://Foo/');
396  $backend->setCache($mockCache);
397 
398  $backend->expects($this->once())->method('isCacheFileExpired');
399 
400  $backend->set('foo', 'some data');
401  $backend->freeze();
402  $this->assertEquals('some data', $backend->get('foo'));
403  $this->assertFalse($backend->get('bar'));
404  }
405 
410  public function ‪hasReturnsTrueIfAnEntryExists(): void
411  {
412  $mockCache = $this->createMock(AbstractFrontend::class);
413  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
414 
415  $backend = $this->getMockBuilder(FileBackend::class)
416  ->setMethods(['dummy'])
417  ->disableOriginalConstructor()
418  ->getMock();
419  $backend->setCacheDirectory('vfs://Foo/');
420  $backend->setCache($mockCache);
421 
422  $entryIdentifier = 'BackendFileTest';
423 
424  $data = 'some data' . microtime();
425  $backend->set($entryIdentifier, $data);
426 
427  $this->assertTrue($backend->has($entryIdentifier), 'has() did not return TRUE.');
428  $this->assertFalse($backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
429  }
430 
434  public function ‪hasReturnsFalseForExpiredEntries(): void
435  {
436  $backend = $this->getMockBuilder(FileBackend::class)
437  ->setMethods(['isCacheFileExpired'])
438  ->disableOriginalConstructor()
439  ->getMock();
440  $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(
441  true,
442  false
443  ));
444 
445  $this->assertFalse($backend->has('foo'));
446  $this->assertTrue($backend->has('bar'));
447  }
448 
454  {
455  $mockCache = $this->createMock(AbstractFrontend::class);
456  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
457 
458  $backend = $this->getMockBuilder(FileBackend::class)
459  ->setMethods(['isCacheFileExpired'])
460  ->disableOriginalConstructor()
461  ->getMock();
462  $backend->setCacheDirectory('vfs://Foo/');
463  $backend->setCache($mockCache);
464 
465  $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
466 
467  $backend->set('foo', 'some data');
468  $backend->freeze();
469  $this->assertTrue($backend->has('foo'));
470  $this->assertFalse($backend->has('bar'));
471  }
472 
477  public function ‪removeReallyRemovesACacheEntry(): void
478  {
479  $mockCache = $this->createMock(AbstractFrontend::class);
480  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
481 
482  $data = 'some data' . microtime();
483  $entryIdentifier = 'BackendFileTest';
484  $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
485 
486  $backend = $this->getMockBuilder(FileBackend::class)
487  ->setMethods(['dummy'])
488  ->disableOriginalConstructor()
489  ->getMock();
490  $backend->setCacheDirectory('vfs://Foo/');
491  $backend->setCache($mockCache);
492 
493  $backend->set($entryIdentifier, $data);
494  $this->assertFileExists($pathAndFilename);
495 
496  $backend->remove($entryIdentifier);
497  $this->assertFileNotExists($pathAndFilename);
498  }
499 
502  public function ‪invalidEntryIdentifiers(): array
503  {
504  return [
505  'trailing slash' => ['/myIdentifer'],
506  'trailing dot and slash' => ['./myIdentifer'],
507  'trailing two dots and slash' => ['../myIdentifier'],
508  'trailing with multiple dots and slashes' => ['.././../myIdentifier'],
509  'slash in middle part' => ['my/Identifier'],
510  'dot and slash in middle part' => ['my./Identifier'],
511  'two dots and slash in middle part' => ['my../Identifier'],
512  'multiple dots and slashes in middle part' => ['my.././../Identifier'],
513  'pending slash' => ['myIdentifier/'],
514  'pending dot and slash' => ['myIdentifier./'],
515  'pending dots and slash' => ['myIdentifier../'],
516  'pending multiple dots and slashes' => ['myIdentifier.././../'],
517  ];
518  }
519 
527  public function ‪setThrowsExceptionForInvalidIdentifier(string $identifier): void
528  {
529  $this->expectException(\InvalidArgumentException::class);
530  $this->expectExceptionCode(1282073032);
531 
532  $mockCache = $this->createMock(AbstractFrontend::class);
533  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
534 
535  $backend = $this->getMockBuilder(FileBackend::class)
536  ->setMethods(['dummy'])
537  ->setConstructorArgs(['test'])
538  ->getMock();
539  $backend->setCacheDirectory('vfs://Foo/');
540  $backend->setCache($mockCache);
541 
542  $backend->set($identifier, 'cache data', []);
543  }
544 
551  public function ‪getThrowsExceptionForInvalidIdentifier(string $identifier): void
552  {
553  $this->expectException(\InvalidArgumentException::class);
554  $this->expectExceptionCode(1282073033);
555 
556  $mockCache = $this->createMock(AbstractFrontend::class);
557  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
558 
559  $backend = $this->getMockBuilder(FileBackend::class)
560  ->setMethods(['dummy'])
561  ->disableOriginalConstructor()
562  ->getMock();
563  $backend->setCacheDirectory('vfs://Foo/');
564  $backend->setCache($mockCache);
565 
566  $backend->get($identifier);
567  }
568 
574  public function ‪hasThrowsExceptionForInvalidIdentifier(string $identifier): void
575  {
576  $this->expectException(\InvalidArgumentException::class);
577  $this->expectExceptionCode(1282073034);
578 
579  $backend = $this->getMockBuilder(FileBackend::class)
580  ->setMethods(['dummy'])
581  ->disableOriginalConstructor()
582  ->getMock();
583 
584  $backend->has($identifier);
585  }
586 
593  public function ‪removeThrowsExceptionForInvalidIdentifier(string $identifier): void
594  {
595  $this->expectException(\InvalidArgumentException::class);
596  $this->expectExceptionCode(1282073035);
597 
598  $mockCache = $this->createMock(AbstractFrontend::class);
599  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
600 
601  $backend = $this->getMockBuilder(FileBackend::class)
602  ->setMethods(['dummy'])
603  ->disableOriginalConstructor()
604  ->getMock();
605  $backend->setCacheDirectory('vfs://Foo/');
606  $backend->setCache($mockCache);
607 
608  $backend->remove($identifier);
609  }
610 
617  public function ‪requireOnceThrowsExceptionForInvalidIdentifier(string $identifier): void
618  {
619  $this->expectException(\InvalidArgumentException::class);
620  $this->expectExceptionCode(1282073036);
621 
622  $mockCache = $this->createMock(AbstractFrontend::class);
623  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
624 
625  $backend = $this->getMockBuilder(FileBackend::class)
626  ->setMethods(['dummy'])
627  ->disableOriginalConstructor()
628  ->getMock();
629  $backend->setCacheDirectory('vfs://Foo/');
630  $backend->setCache($mockCache);
631 
632  $backend->requireOnce($identifier);
633  }
634 
640  {
641  $mockCache = $this->createMock(AbstractFrontend::class);
642  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
643 
644  $backend = $this->getMockBuilder(FileBackend::class)
645  ->setMethods(['dummy'])
646  ->disableOriginalConstructor()
647  ->getMock();
648  $backend->setCacheDirectory('vfs://Foo/');
649  $backend->setCache($mockCache);
650 
651  $entryIdentifier = 'SomePhpEntry';
652 
653  $data = '<?php return "foo"; ?>';
654  $backend->set($entryIdentifier, $data);
655 
656  $loadedData = $backend->requireOnce($entryIdentifier);
657  $this->assertEquals('foo', $loadedData);
658  }
659 
665  {
666  $mockCache = $this->createMock(AbstractFrontend::class);
667  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
668 
669  $backend = $this->getMockBuilder(FileBackend::class)
670  ->setMethods(['isCacheFileExpired'])
671  ->disableOriginalConstructor()
672  ->getMock();
673  $backend->setCacheDirectory('vfs://Foo/');
674  $backend->setCache($mockCache);
675 
676  $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
677 
678  $data = '<?php return "foo"; ?>';
679  $backend->set('FooEntry', $data);
680 
681  $backend->freeze();
682 
683  $loadedData = $backend->requireOnce('FooEntry');
684  $this->assertEquals('foo', $loadedData);
685  }
686 
692  public function ‪requireThrowsExceptionForInvalidIdentifier(string $identifier): void
693  {
694  $this->expectException(\InvalidArgumentException::class);
695  $this->expectExceptionCode(1532528246);
696  $mockCache = $this->createMock(AbstractFrontend::class);
697  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
698 
699  $backend = $this->getMockBuilder(FileBackend::class)
700  ->setMethods(['dummy'])
701  ->disableOriginalConstructor()
702  ->getMock();
703  $backend->setCacheDirectory('vfs://Foo/');
704  $backend->setCache($mockCache);
705 
706  $backend->require($identifier);
707  }
708 
713  {
714  $mockCache = $this->createMock(AbstractFrontend::class);
715  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
716  $backend = $this->getMockBuilder(FileBackend::class)
717  ->setMethods(['dummy'])
718  ->disableOriginalConstructor()
719  ->getMock();
720  $backend->setCacheDirectory('vfs://Foo/');
721  $backend->setCache($mockCache);
722 
723  $entryIdentifier = 'SomePhpEntry2';
724 
725  $data = '<?php return "foo2"; ?>';
726  $backend->set($entryIdentifier, $data);
727 
728  $loadedData = $backend->require($entryIdentifier);
729  $this->assertEquals('foo2', $loadedData);
730  }
731 
736  {
737  $mockCache = $this->createMock(AbstractFrontend::class);
738  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
739  $backend = $this->getMockBuilder(FileBackend::class)
740  ->setMethods(['isCacheFileExpired'])
741  ->disableOriginalConstructor()
742  ->getMock();
743  $backend->setCacheDirectory('vfs://Foo/');
744  $backend->setCache($mockCache);
745 
746  $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
747 
748  $data = '<?php return "foo"; ?>';
749  $backend->set('FooEntry2', $data);
750 
751  $backend->freeze();
752 
753  $loadedData = $backend->require('FooEntry2');
754  $this->assertEquals('foo', $loadedData);
755  }
756 
761  {
762  $frontendProphecy = $this->prophesize(AbstractFrontend::class);
763  $frontendProphecy->getIdentifier()->willReturn('UnitTestCache');
764  $subject = new ‪FileBackend('Testing');
765  $subject->setCacheDirectory('vfs://Foo/');
766  $subject->setCache($frontendProphecy->reveal());
767  $subject->set('BarEntry', '<?php return "foo"; ?>');
768  $loadedData = $subject->require('BarEntry');
769  $this->assertEquals('foo', $loadedData);
770  $loadedData = $subject->require('BarEntry');
771  $this->assertEquals('foo', $loadedData);
772  }
773 
779  {
780  $mockCache = $this->createMock(AbstractFrontend::class);
781  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
782 
783  $backend = $this->getMockBuilder(FileBackend::class)
784  ->setMethods(['dummy'])
785  ->disableOriginalConstructor()
786  ->getMock();
787  $backend->setCacheDirectory('vfs://Foo/');
788  $backend->setCache($mockCache);
789 
790  $data = 'some data' . microtime();
791  $backend->set('BackendFileTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
792  $backend->set('BackendFileTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
793  $backend->set('BackendFileTest3', $data, ['UnitTestTag%test']);
794 
795  $expectedEntry = 'BackendFileTest2';
796 
797  $actualEntries = $backend->findIdentifiersByTag('UnitTestTag%special');
798  $this->assertInternalType('array', $actualEntries);
799  $this->assertEquals($expectedEntry, array_pop($actualEntries));
800  }
801 
807  {
808  $mockCache = $this->createMock(AbstractFrontend::class);
809  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
810 
811  $backend = $this->getMockBuilder(FileBackend::class)
812  ->setMethods(['dummy'])
813  ->disableOriginalConstructor()
814  ->getMock();
815  $backend->setCacheDirectory('vfs://Foo/');
816  $backend->setCache($mockCache);
817 
818  $data = 'some data';
819  $backend->set('BackendFileTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
820  $backend->set('BackendFileTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special'], -100);
821  $backend->set('BackendFileTest3', $data, ['UnitTestTag%test']);
822 
823  $this->assertSame([], $backend->findIdentifiersByTag('UnitTestTag%special'));
824  $this->assertSame(['BackendFileTest1', 'BackendFileTest3'], $backend->findIdentifiersByTag('UnitTestTag%test'));
825  }
826 
831  public function ‪flushRemovesAllCacheEntries(): void
832  {
833  $mockCache = $this->createMock(AbstractFrontend::class);
834  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
835 
836  $backend = $this->getMockBuilder(FileBackend::class)
837  ->setMethods(['dummy'])
838  ->disableOriginalConstructor()
839  ->getMock();
840  $backend->setCacheDirectory('vfs://Foo/');
841  $backend->setCache($mockCache);
842 
843  $data = 'some data';
844  $backend->set('BackendFileTest1', $data);
845  $backend->set('BackendFileTest2', $data);
846 
847  $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
848  $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
849 
850  $backend->flush();
851 
852  $this->assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
853  $this->assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
854  }
855 
860  public function ‪flushCreatesCacheDirectoryAgain(): void
861  {
862  $mockCache = $this->createMock(AbstractFrontend::class);
863  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
864 
865  $backend = $this->getMockBuilder(FileBackend::class)
866  ->setMethods(['dummy'])
867  ->disableOriginalConstructor()
868  ->getMock();
869  $backend->setCacheDirectory('vfs://Foo/');
870  $backend->setCache($mockCache);
871 
872  $backend->flush();
873  $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/');
874  }
875 
880  {
881  $backend = $this->getMockBuilder(FileBackend::class)
882  ->setMethods(['findIdentifiersByTag', 'remove'])
883  ->disableOriginalConstructor()
884  ->getMock();
885 
886  $backend->expects($this->once())->method('findIdentifiersByTag')->with('UnitTestTag%special')->will($this->returnValue([
887  'foo',
888  'bar',
889  'baz'
890  ]));
891  $backend->expects($this->at(1))->method('remove')->with('foo');
892  $backend->expects($this->at(2))->method('remove')->with('bar');
893  $backend->expects($this->at(3))->method('remove')->with('baz');
894 
895  $backend->flushByTag('UnitTestTag%special');
896  }
897 
903  {
904  $mockCache = $this->createMock(AbstractFrontend::class);
905  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
906 
907  $backend = $this->getMockBuilder(FileBackend::class)
908  ->setMethods(['isCacheFileExpired'])
909  ->disableOriginalConstructor()
910  ->getMock();
911  $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(
912  true,
913  false
914  ));
915  $backend->setCacheDirectory('vfs://Foo/');
916  $backend->setCache($mockCache);
917 
918  $data = 'some data';
919  $backend->set('BackendFileTest1', $data);
920  $backend->set('BackendFileTest2', $data);
921 
922  $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
923  $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
924 
925  $backend->collectGarbage();
926  $this->assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
927  $this->assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
928  }
929 
934  public function ‪flushUnfreezesTheCache(): void
935  {
936  $mockCache = $this->createMock(AbstractFrontend::class);
937  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
938 
939  $backend = $this->getMockBuilder(FileBackend::class)
940  ->setMethods(['dummy'])
941  ->disableOriginalConstructor()
942  ->getMock();
943  $backend->setCacheDirectory('vfs://Foo/');
944  $backend->setCache($mockCache);
945 
946  $backend->freeze();
947 
948  $this->assertTrue($backend->isFrozen());
949  $backend->flush();
950  $this->assertFalse($backend->isFrozen());
951  }
952 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryThrowsExceptionOnNonWritableDirectory
‪setCacheDirectoryThrowsExceptionOnNonWritableDirectory()
Definition: FileBackendTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireIncludesAndReturnsResultOfIncludedPhpFile
‪requireIncludesAndReturnsResultOfIncludedPhpFile()
Definition: FileBackendTest.php:712
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsAbsolutePathWithoutTrailingSlash
‪setCacheDirectoryAllowsAbsolutePathWithoutTrailingSlash()
Definition: FileBackendTest.php:69
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: FileBackendTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\removeThrowsExceptionForInvalidIdentifier
‪removeThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:593
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\hasThrowsExceptionForInvalidIdentifier
‪hasThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:574
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setReallySavesToTheSpecifiedDirectory
‪setReallySavesToTheSpecifiedDirectory()
Definition: FileBackendTest.php:213
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsRelativePathWithoutTrailingSlash
‪setCacheDirectoryAllowsRelativePathWithoutTrailingSlash()
Definition: FileBackendTest.php:89
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\EXPIRYTIME_LENGTH
‪const EXPIRYTIME_LENGTH
Definition: FileBackend.php:32
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setUp
‪setUp()
Definition: FileBackendTest.php:40
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:2
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getReturnsFalseForExpiredEntries
‪getReturnsFalseForExpiredEntries()
Definition: FileBackendTest.php:366
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:24
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsAbsoluteDottedPathWithoutTrailingSlash
‪setCacheDirectoryAllowsAbsoluteDottedPathWithoutTrailingSlash()
Definition: FileBackendTest.php:133
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\flushUnfreezesTheCache
‪flushUnfreezesTheCache()
Definition: FileBackendTest.php:934
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsRelativeDottedPathWithTrailingSlash
‪setCacheDirectoryAllowsRelativeDottedPathWithTrailingSlash()
Definition: FileBackendTest.php:122
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getThrowsExceptionForInvalidIdentifier
‪getThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:551
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireThrowsExceptionForInvalidIdentifier
‪requireThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:692
‪TYPO3\CMS\Core\Cache\Backend\FileBackend
Definition: FileBackend.php:29
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireOnceDoesNotCheckExpiryTimeIfBackendIsFrozen
‪requireOnceDoesNotCheckExpiryTimeIfBackendIsFrozen()
Definition: FileBackendTest.php:664
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\aDedicatedCacheDirectoryIsUsedForCodeCaches
‪aDedicatedCacheDirectoryIsUsedForCodeCaches()
Definition: FileBackendTest.php:173
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireOnceIncludesAndReturnsResultOfIncludedPhpFile
‪requireOnceIncludesAndReturnsResultOfIncludedPhpFile()
Definition: FileBackendTest.php:639
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireDoesNotCheckExpiryTimeIfBackendIsFrozen
‪requireDoesNotCheckExpiryTimeIfBackendIsFrozen()
Definition: FileBackendTest.php:735
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:142
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen
‪getDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen()
Definition: FileBackendTest.php:386
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:2
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setAlsoSavesSpecifiedTags
‪setAlsoSavesSpecifiedTags()
Definition: FileBackendTest.php:269
‪TYPO3\CMS\Core\Cache\Exception\InvalidDataException
Definition: InvalidDataException.php:21
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest
Definition: FileBackendTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag
‪findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag()
Definition: FileBackendTest.php:778
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\hasDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen
‪hasDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen()
Definition: FileBackendTest.php:453
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\removeReallyRemovesACacheEntry
‪removeReallyRemovesACacheEntry()
Definition: FileBackendTest.php:477
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\hasReturnsTrueIfAnEntryExists
‪hasReturnsTrueIfAnEntryExists()
Definition: FileBackendTest.php:410
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setThrowsExceptionForInvalidIdentifier
‪setThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:527
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\findIdentifiersByTagDoesNotReturnExpiredEntries
‪findIdentifiersByTagDoesNotReturnExpiredEntries()
Definition: FileBackendTest.php:806
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireOnceThrowsExceptionForInvalidIdentifier
‪requireOnceThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:617
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getReturnsContentOfTheCorrectCacheFile
‪getReturnsContentOfTheCorrectCacheFile()
Definition: FileBackendTest.php:338
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsAbsolutePathWithTrailingSlash
‪setCacheDirectoryAllowsAbsolutePathWithTrailingSlash()
Definition: FileBackendTest.php:79
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\collectGarbageRemovesExpiredCacheEntries
‪collectGarbageRemovesExpiredCacheEntries()
Definition: FileBackendTest.php:902
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\invalidEntryIdentifiers
‪invalidEntryIdentifiers()
Definition: FileBackendTest.php:502
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDetectsAndLoadsAFrozenCache
‪setCacheDetectsAndLoadsAFrozenCache()
Definition: FileBackendTest.php:302
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\hasReturnsFalseForExpiredEntries
‪hasReturnsFalseForExpiredEntries()
Definition: FileBackendTest.php:434
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getCacheDirectoryReturnsTheCurrentCacheDirectory
‪getCacheDirectoryReturnsTheCurrentCacheDirectory()
Definition: FileBackendTest.php:154
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireCanLoadSameEntryMultipleTimes
‪requireCanLoadSameEntryMultipleTimes()
Definition: FileBackendTest.php:760
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsRelativeDottedPathWithoutTrailingSlash
‪setCacheDirectoryAllowsRelativeDottedPathWithoutTrailingSlash()
Definition: FileBackendTest.php:111
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\flushCreatesCacheDirectoryAgain
‪flushCreatesCacheDirectoryAgain()
Definition: FileBackendTest.php:860
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier
‪setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier()
Definition: FileBackendTest.php:240
‪TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend
Definition: AbstractFrontend.php:26
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: FileBackendTest.php:831
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsAbsoluteDottedPathWithTrailingSlash
‪setCacheDirectoryAllowsAbsoluteDottedPathWithTrailingSlash()
Definition: FileBackendTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setThrowsExceptionIfDataIsNotAString
‪setThrowsExceptionIfDataIsNotAString()
Definition: FileBackendTest.php:192
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsRelativePathWithTrailingSlash
‪setCacheDirectoryAllowsRelativePathWithTrailingSlash()
Definition: FileBackendTest.php:100
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: FileBackendTest.php:879