‪TYPO3CMS  10.4
FileBackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use org\bovigo\vfs\vfsStreamDirectory;
21 use org\bovigo\vfs\vfsStreamWrapper;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪FileBackendTest extends UnitTestCase
34 {
35  protected ‪$resetSingletonInstances = true;
36 
41  protected function ‪setUp(): void
42  {
43  parent::setUp();
44  vfsStreamWrapper::register();
45  vfsStreamWrapper::setRoot(new vfsStreamDirectory('Foo'));
46  }
47 
53  {
54  $this->expectException(Exception::class);
55  $this->expectExceptionCode(1303669848);
56 
57  $mockCache = $this->createMock(AbstractFrontend::class);
58 
59  $backend = $this->getMockBuilder(FileBackend::class)
60  ->setMethods(['dummy'])
61  ->disableOriginalConstructor()
62  ->getMock();
63  $backend->setCacheDirectory('http://localhost/');
64 
65  $backend->setCache($mockCache);
66  }
67 
72  {
73  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
74  $backend->setCacheDirectory('/tmp/foo');
75  self::assertEquals('/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
76  }
77 
82  {
83  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
84  $backend->setCacheDirectory('/tmp/foo/');
85  self::assertEquals('/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
86  }
87 
92  {
93  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
94  $backend->setCacheDirectory('tmp/foo');
96  self::assertEquals($path . '/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
97  }
98 
103  {
104  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
105  $backend->setCacheDirectory('tmp/foo/');
107  self::assertEquals($path . '/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
108  }
109 
114  {
115  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
116  $backend->setCacheDirectory('../tmp/foo');
118  self::assertEquals($path . '/../tmp/foo/', $backend->_get('temporaryCacheDirectory'));
119  }
120 
125  {
126  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
127  $backend->setCacheDirectory('../tmp/foo/');
129  self::assertEquals($path . '/../tmp/foo/', $backend->_get('temporaryCacheDirectory'));
130  }
131 
136  {
137  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
138  $backend->setCacheDirectory('/tmp/../foo');
139  self::assertEquals('/tmp/../foo/', $backend->_get('temporaryCacheDirectory'));
140  }
141 
146  {
147  $backend = $this->getAccessibleMock(FileBackend::class, ['dummy'], [], '', false);
148  $backend->setCacheDirectory('/tmp/../foo/');
149  self::assertEquals('/tmp/../foo/', $backend->_get('temporaryCacheDirectory'));
150  }
151 
157  {
158  $mockCache = $this->createMock(AbstractFrontend::class);
159  $mockCache->expects(self::any())->method('getIdentifier')->willReturn('SomeCache');
160 
161  $backend = $this->getMockBuilder(FileBackend::class)
162  ->setMethods(['dummy'])
163  ->disableOriginalConstructor()
164  ->getMock();
165  $backend->setCacheDirectory('vfs://Foo/');
166  $backend->setCache($mockCache);
167 
168  self::assertEquals('vfs://Foo/cache/data/SomeCache/', $backend->getCacheDirectory());
169  }
170 
176  {
177  $mockCache = $this->createMock(PhpFrontend::class);
178  $mockCache->expects(self::any())->method('getIdentifier')->willReturn('SomeCache');
179 
180  $backend = $this->getMockBuilder(FileBackend::class)
181  ->setMethods(['dummy'])
182  ->disableOriginalConstructor()
183  ->getMock();
184  $backend->setCacheDirectory('vfs://Foo/');
185  $backend->setCache($mockCache);
186 
187  self::assertEquals('vfs://Foo/cache/code/SomeCache/', $backend->getCacheDirectory());
188  }
189 
195  {
196  $this->expectException(InvalidDataException::class);
197  $this->expectExceptionCode(1204481674);
198 
199  $mockCache = $this->createMock(AbstractFrontend::class);
200 
201  $backend = $this->getMockBuilder(FileBackend::class)
202  ->setMethods(['dummy'])
203  ->disableOriginalConstructor()
204  ->getMock();
205  $backend->setCacheDirectory('vfs://Foo/');
206  $backend->setCache($mockCache);
207 
208  $backend->set('some identifier', ['not a string']);
209  }
210 
216  {
217  $mockCache = $this->createMock(AbstractFrontend::class);
218  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
219 
220  $data = 'some data' . microtime();
221  $entryIdentifier = 'BackendFileTest';
222  $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
223 
224  $backend = $this->getMockBuilder(FileBackend::class)
225  ->setMethods(['dummy'])
226  ->disableOriginalConstructor()
227  ->getMock();
228  $backend->setCacheDirectory('vfs://Foo/');
229  $backend->setCache($mockCache);
230 
231  $backend->set($entryIdentifier, $data);
232 
233  self::assertFileExists($pathAndFilename);
234  $retrievedData = file_get_contents($pathAndFilename, false, null, 0, \strlen($data));
235  self::assertEquals($data, $retrievedData);
236  }
237 
243  {
244  $mockCache = $this->createMock(AbstractFrontend::class);
245  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
246 
247  $data1 = 'some data' . microtime();
248  $data2 = 'some data' . microtime();
249  $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
250 
251  $backend = $this->getMockBuilder(FileBackend::class)
252  ->setMethods(['dummy'])
253  ->disableOriginalConstructor()
254  ->getMock();
255  $backend->setCacheDirectory('vfs://Foo/');
256  $backend->setCache($mockCache);
257 
258  $backend->set($entryIdentifier, $data1, [], 500);
259  $backend->set($entryIdentifier, $data2, [], 200);
260 
261  $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
262  self::assertFileExists($pathAndFilename);
263  $retrievedData = file_get_contents($pathAndFilename, false, null, 0, \strlen($data2));
264  self::assertEquals($data2, $retrievedData);
265  }
266 
271  public function ‪setAlsoSavesSpecifiedTags(): void
272  {
273  $mockCache = $this->createMock(AbstractFrontend::class);
274  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
275 
276  $data = 'some data' . microtime();
277  $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
278 
279  $backend = $this->getMockBuilder(FileBackend::class)
280  ->setMethods(['dummy'])
281  ->disableOriginalConstructor()
282  ->getMock();
283  $backend->setCacheDirectory('vfs://Foo/');
284  $backend->setCache($mockCache);
285 
286  $backend->set($entryIdentifier, $data, ['Tag1', 'Tag2']);
287 
288  $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
289  self::assertFileExists($pathAndFilename);
290  $retrievedData = file_get_contents(
291  $pathAndFilename,
292  false,
293  null,
294  \strlen($data) + ‪FileBackend::EXPIRYTIME_LENGTH,
295  9
296  );
297  self::assertEquals('Tag1 Tag2', $retrievedData);
298  }
299 
305  {
306  $mockCache = $this->createMock(AbstractFrontend::class);
307  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
308 
309  $data = 'some data' . microtime();
310  $entryIdentifier = 'BackendFileTest';
311 
312  $backend = $this->getMockBuilder(FileBackend::class)
313  ->setMethods(['dummy'])
314  ->disableOriginalConstructor()
315  ->getMock();
316  $backend->setCacheDirectory('vfs://Foo/');
317  $backend->setCache($mockCache);
318 
319  $backend->set($entryIdentifier, $data, ['Tag1', 'Tag2']);
320 
321  $backend->freeze();
322 
323  unset($backend);
324 
325  $backend = $this->getMockBuilder(FileBackend::class)
326  ->setMethods(['dummy'])
327  ->disableOriginalConstructor()
328  ->getMock();
329  $backend->setCacheDirectory('vfs://Foo/');
330  $backend->setCache($mockCache);
331 
332  self::assertTrue($backend->isFrozen());
333  self::assertEquals($data, $backend->get($entryIdentifier));
334  }
335 
341  {
342  $mockCache = $this->createMock(AbstractFrontend::class);
343  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
344 
345  $backend = $this->getMockBuilder(FileBackend::class)
346  ->setMethods(['setTag'])
347  ->disableOriginalConstructor()
348  ->getMock();
349  $backend->setCacheDirectory('vfs://Foo/');
350  $backend->setCache($mockCache);
351 
352  $entryIdentifier = 'BackendFileTest';
353 
354  $data = 'some data' . microtime();
355  $backend->set($entryIdentifier, $data, [], 500);
356 
357  $data = 'some other data' . microtime();
358  $backend->set($entryIdentifier, $data, [], 100);
359 
360  $loadedData = $backend->get($entryIdentifier);
361  self::assertEquals($data, $loadedData);
362  }
363 
368  public function ‪getReturnsFalseForExpiredEntries(): void
369  {
370  $mockCache = $this->createMock(AbstractFrontend::class);
371  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
372 
373  $backend = $this->getMockBuilder(FileBackend::class)
374  ->setMethods(['isCacheFileExpired'])
375  ->disableOriginalConstructor()
376  ->getMock();
377  $backend->expects(self::once())->method('isCacheFileExpired')->with('vfs://Foo/cache/data/UnitTestCache/ExpiredEntry')->willReturn(true);
378  $backend->setCacheDirectory('vfs://Foo/');
379  $backend->setCache($mockCache);
380 
381  self::assertFalse($backend->get('ExpiredEntry'));
382  }
383 
389  {
390  $mockCache = $this->createMock(AbstractFrontend::class);
391  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
392 
393  $backend = $this->getMockBuilder(FileBackend::class)
394  ->setMethods(['isCacheFileExpired'])
395  ->disableOriginalConstructor()
396  ->getMock();
397  $backend->setCacheDirectory('vfs://Foo/');
398  $backend->setCache($mockCache);
399 
400  $backend->expects(self::once())->method('isCacheFileExpired');
401 
402  $backend->set('foo', 'some data');
403  $backend->freeze();
404  self::assertEquals('some data', $backend->get('foo'));
405  self::assertFalse($backend->get('bar'));
406  }
407 
412  public function ‪hasReturnsTrueIfAnEntryExists(): void
413  {
414  $mockCache = $this->createMock(AbstractFrontend::class);
415  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
416 
417  $backend = $this->getMockBuilder(FileBackend::class)
418  ->setMethods(['dummy'])
419  ->disableOriginalConstructor()
420  ->getMock();
421  $backend->setCacheDirectory('vfs://Foo/');
422  $backend->setCache($mockCache);
423 
424  $entryIdentifier = 'BackendFileTest';
425 
426  $data = 'some data' . microtime();
427  $backend->set($entryIdentifier, $data);
428 
429  self::assertTrue($backend->has($entryIdentifier), 'has() did not return TRUE.');
430  self::assertFalse($backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
431  }
432 
436  public function ‪hasReturnsFalseForExpiredEntries(): void
437  {
438  $backend = $this->getMockBuilder(FileBackend::class)
439  ->setMethods(['isCacheFileExpired'])
440  ->disableOriginalConstructor()
441  ->getMock();
442  $backend->expects(self::exactly(2))->method('isCacheFileExpired')->will(self::onConsecutiveCalls(
443  true,
444  false
445  ));
446 
447  self::assertFalse($backend->has('foo'));
448  self::assertTrue($backend->has('bar'));
449  }
450 
456  {
457  $mockCache = $this->createMock(AbstractFrontend::class);
458  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
459 
460  $backend = $this->getMockBuilder(FileBackend::class)
461  ->setMethods(['isCacheFileExpired'])
462  ->disableOriginalConstructor()
463  ->getMock();
464  $backend->setCacheDirectory('vfs://Foo/');
465  $backend->setCache($mockCache);
466 
467  $backend->expects(self::once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
468 
469  $backend->set('foo', 'some data');
470  $backend->freeze();
471  self::assertTrue($backend->has('foo'));
472  self::assertFalse($backend->has('bar'));
473  }
474 
479  public function ‪removeReallyRemovesACacheEntry(): void
480  {
481  $mockCache = $this->createMock(AbstractFrontend::class);
482  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
483 
484  $data = 'some data' . microtime();
485  $entryIdentifier = 'BackendFileTest';
486  $pathAndFilename = 'vfs://Foo/cache/data/UnitTestCache/' . $entryIdentifier;
487 
488  $backend = $this->getMockBuilder(FileBackend::class)
489  ->setMethods(['dummy'])
490  ->disableOriginalConstructor()
491  ->getMock();
492  $backend->setCacheDirectory('vfs://Foo/');
493  $backend->setCache($mockCache);
494 
495  $backend->set($entryIdentifier, $data);
496  self::assertFileExists($pathAndFilename);
497 
498  $backend->remove($entryIdentifier);
499  // @todo remove condition and else branch as soon as phpunit v8 goes out of support
500  if (method_exists($this, 'assertFileDoesNotExist')) {
501  self::assertFileDoesNotExist($pathAndFilename);
502  } else {
503  self::assertFileNotExists($pathAndFilename);
504  }
505  }
506 
507  public function ‪invalidEntryIdentifiers(): array
508  {
509  return [
510  'trailing slash' => ['/myIdentifier'],
511  'trailing dot and slash' => ['./myIdentifier'],
512  'trailing two dots and slash' => ['../myIdentifier'],
513  'trailing with multiple dots and slashes' => ['.././../myIdentifier'],
514  'slash in middle part' => ['my/Identifier'],
515  'dot and slash in middle part' => ['my./Identifier'],
516  'two dots and slash in middle part' => ['my../Identifier'],
517  'multiple dots and slashes in middle part' => ['my.././../Identifier'],
518  'pending slash' => ['myIdentifier/'],
519  'pending dot and slash' => ['myIdentifier./'],
520  'pending dots and slash' => ['myIdentifier../'],
521  'pending multiple dots and slashes' => ['myIdentifier.././../'],
522  ];
523  }
524 
532  public function ‪setThrowsExceptionForInvalidIdentifier(string $identifier): void
533  {
534  $this->expectException(\InvalidArgumentException::class);
535  $this->expectExceptionCode(1282073032);
536 
537  $mockCache = $this->createMock(AbstractFrontend::class);
538  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
539 
540  $backend = $this->getMockBuilder(FileBackend::class)
541  ->setMethods(['dummy'])
542  ->setConstructorArgs(['test'])
543  ->getMock();
544  $backend->setCacheDirectory('vfs://Foo/');
545  $backend->setCache($mockCache);
546 
547  $backend->set($identifier, 'cache data', []);
548  }
549 
556  public function ‪getThrowsExceptionForInvalidIdentifier(string $identifier): void
557  {
558  $this->expectException(\InvalidArgumentException::class);
559  $this->expectExceptionCode(1282073033);
560 
561  $mockCache = $this->createMock(AbstractFrontend::class);
562  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
563 
564  $backend = $this->getMockBuilder(FileBackend::class)
565  ->setMethods(['dummy'])
566  ->disableOriginalConstructor()
567  ->getMock();
568  $backend->setCacheDirectory('vfs://Foo/');
569  $backend->setCache($mockCache);
570 
571  $backend->get($identifier);
572  }
573 
579  public function ‪hasThrowsExceptionForInvalidIdentifier(string $identifier): void
580  {
581  $this->expectException(\InvalidArgumentException::class);
582  $this->expectExceptionCode(1282073034);
583 
584  $backend = $this->getMockBuilder(FileBackend::class)
585  ->setMethods(['dummy'])
586  ->disableOriginalConstructor()
587  ->getMock();
588 
589  $backend->has($identifier);
590  }
591 
598  public function ‪removeThrowsExceptionForInvalidIdentifier(string $identifier): void
599  {
600  $this->expectException(\InvalidArgumentException::class);
601  $this->expectExceptionCode(1282073035);
602 
603  $mockCache = $this->createMock(AbstractFrontend::class);
604  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
605 
606  $backend = $this->getMockBuilder(FileBackend::class)
607  ->setMethods(['dummy'])
608  ->disableOriginalConstructor()
609  ->getMock();
610  $backend->setCacheDirectory('vfs://Foo/');
611  $backend->setCache($mockCache);
612 
613  $backend->remove($identifier);
614  }
615 
622  public function ‪requireOnceThrowsExceptionForInvalidIdentifier(string $identifier): void
623  {
624  $this->expectException(\InvalidArgumentException::class);
625  $this->expectExceptionCode(1282073036);
626 
627  $mockCache = $this->createMock(AbstractFrontend::class);
628  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
629 
630  $backend = $this->getMockBuilder(FileBackend::class)
631  ->setMethods(['dummy'])
632  ->disableOriginalConstructor()
633  ->getMock();
634  $backend->setCacheDirectory('vfs://Foo/');
635  $backend->setCache($mockCache);
636 
637  $backend->requireOnce($identifier);
638  }
639 
645  {
646  $mockCache = $this->createMock(AbstractFrontend::class);
647  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
648 
649  $backend = $this->getMockBuilder(FileBackend::class)
650  ->setMethods(['dummy'])
651  ->disableOriginalConstructor()
652  ->getMock();
653  $backend->setCacheDirectory('vfs://Foo/');
654  $backend->setCache($mockCache);
655 
656  $entryIdentifier = 'SomePhpEntry';
657 
658  $data = '<?php return "foo"; ?>';
659  $backend->set($entryIdentifier, $data);
660 
661  $loadedData = $backend->requireOnce($entryIdentifier);
662  self::assertEquals('foo', $loadedData);
663  }
664 
670  {
671  $mockCache = $this->createMock(AbstractFrontend::class);
672  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
673 
674  $backend = $this->getMockBuilder(FileBackend::class)
675  ->setMethods(['isCacheFileExpired'])
676  ->disableOriginalConstructor()
677  ->getMock();
678  $backend->setCacheDirectory('vfs://Foo/');
679  $backend->setCache($mockCache);
680 
681  $backend->expects(self::once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
682 
683  $data = '<?php return "foo"; ?>';
684  $backend->set('FooEntry', $data);
685 
686  $backend->freeze();
687 
688  $loadedData = $backend->requireOnce('FooEntry');
689  self::assertEquals('foo', $loadedData);
690  }
691 
697  public function ‪requireThrowsExceptionForInvalidIdentifier(string $identifier): void
698  {
699  $this->expectException(\InvalidArgumentException::class);
700  $this->expectExceptionCode(1532528246);
701  $mockCache = $this->createMock(AbstractFrontend::class);
702  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
703 
704  $backend = $this->getMockBuilder(FileBackend::class)
705  ->setMethods(['dummy'])
706  ->disableOriginalConstructor()
707  ->getMock();
708  $backend->setCacheDirectory('vfs://Foo/');
709  $backend->setCache($mockCache);
710 
711  $backend->require($identifier);
712  }
713 
718  {
719  $mockCache = $this->createMock(AbstractFrontend::class);
720  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
721  $backend = $this->getMockBuilder(FileBackend::class)
722  ->setMethods(['dummy'])
723  ->disableOriginalConstructor()
724  ->getMock();
725  $backend->setCacheDirectory('vfs://Foo/');
726  $backend->setCache($mockCache);
727 
728  $entryIdentifier = 'SomePhpEntry2';
729 
730  $data = '<?php return "foo2"; ?>';
731  $backend->set($entryIdentifier, $data);
732 
733  $loadedData = $backend->require($entryIdentifier);
734  self::assertEquals('foo2', $loadedData);
735  }
736 
741  {
742  $mockCache = $this->createMock(AbstractFrontend::class);
743  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
744  $backend = $this->getMockBuilder(FileBackend::class)
745  ->setMethods(['isCacheFileExpired'])
746  ->disableOriginalConstructor()
747  ->getMock();
748  $backend->setCacheDirectory('vfs://Foo/');
749  $backend->setCache($mockCache);
750 
751  $backend->expects(self::once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
752 
753  $data = '<?php return "foo"; ?>';
754  $backend->set('FooEntry2', $data);
755 
756  $backend->freeze();
757 
758  $loadedData = $backend->require('FooEntry2');
759  self::assertEquals('foo', $loadedData);
760  }
761 
766  {
767  $frontendProphecy = $this->prophesize(AbstractFrontend::class);
768  $frontendProphecy->getIdentifier()->willReturn('UnitTestCache');
769  $subject = new ‪FileBackend('Testing');
770  $subject->setCacheDirectory('vfs://Foo/');
771  $subject->setCache($frontendProphecy->reveal());
772  $subject->set('BarEntry', '<?php return "foo"; ?>');
773  $loadedData = $subject->require('BarEntry');
774  self::assertEquals('foo', $loadedData);
775  $loadedData = $subject->require('BarEntry');
776  self::assertEquals('foo', $loadedData);
777  }
778 
784  {
785  $mockCache = $this->createMock(AbstractFrontend::class);
786  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
787 
788  $backend = $this->getMockBuilder(FileBackend::class)
789  ->setMethods(['dummy'])
790  ->disableOriginalConstructor()
791  ->getMock();
792  $backend->setCacheDirectory('vfs://Foo/');
793  $backend->setCache($mockCache);
794 
795  $data = 'some data' . microtime();
796  $backend->set('BackendFileTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
797  $backend->set('BackendFileTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
798  $backend->set('BackendFileTest3', $data, ['UnitTestTag%test']);
799 
800  $expectedEntry = 'BackendFileTest2';
801 
802  $actualEntries = $backend->findIdentifiersByTag('UnitTestTag%special');
803  self::assertIsArray($actualEntries);
804  self::assertEquals($expectedEntry, array_pop($actualEntries));
805  }
806 
812  {
813  $mockCache = $this->createMock(AbstractFrontend::class);
814  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
815 
816  $backend = $this->getMockBuilder(FileBackend::class)
817  ->setMethods(['dummy'])
818  ->disableOriginalConstructor()
819  ->getMock();
820  $backend->setCacheDirectory('vfs://Foo/');
821  $backend->setCache($mockCache);
822 
823  $data = 'some data';
824  $backend->set('BackendFileTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
825  $backend->set('BackendFileTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special'], -100);
826  $backend->set('BackendFileTest3', $data, ['UnitTestTag%test']);
827 
828  self::assertSame([], $backend->findIdentifiersByTag('UnitTestTag%special'));
829  self::assertSame(['BackendFileTest1', 'BackendFileTest3'], $backend->findIdentifiersByTag('UnitTestTag%test'));
830  }
831 
836  public function ‪flushRemovesAllCacheEntries(): void
837  {
838  $mockCache = $this->createMock(AbstractFrontend::class);
839  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
840 
841  $backend = $this->getMockBuilder(FileBackend::class)
842  ->setMethods(['dummy'])
843  ->disableOriginalConstructor()
844  ->getMock();
845  $backend->setCacheDirectory('vfs://Foo/');
846  $backend->setCache($mockCache);
847 
848  $data = 'some data';
849  $backend->set('BackendFileTest1', $data);
850  $backend->set('BackendFileTest2', $data);
851 
852  self::assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
853  self::assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
854 
855  $backend->flush();
856 
857  // @todo remove condition and else branch as soon as phpunit v8 goes out of support
858  if (method_exists($this, 'assertFileDoesNotExist')) {
859  self::assertFileDoesNotExist('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
860  self::assertFileDoesNotExist('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
861  } else {
862  self::assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
863  self::assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
864  }
865  }
866 
871  public function ‪flushCreatesCacheDirectoryAgain(): void
872  {
873  $mockCache = $this->createMock(AbstractFrontend::class);
874  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
875 
876  $backend = $this->getMockBuilder(FileBackend::class)
877  ->setMethods(['dummy'])
878  ->disableOriginalConstructor()
879  ->getMock();
880  $backend->setCacheDirectory('vfs://Foo/');
881  $backend->setCache($mockCache);
882 
883  $backend->flush();
884  self::assertFileExists('vfs://Foo/cache/data/UnitTestCache/');
885  }
886 
891  {
892  $backend = $this->getMockBuilder(FileBackend::class)
893  ->setMethods(['findIdentifiersByTag', 'remove'])
894  ->disableOriginalConstructor()
895  ->getMock();
896 
897  $backend->expects(self::once())->method('findIdentifiersByTag')->with('UnitTestTag%special')->willReturn([
898  'foo',
899  'bar',
900  'baz'
901  ]);
902  $backend->expects(self::exactly(3))->method('remove')->withConsecutive(['foo'], ['bar'], ['baz']);
903 
904  $backend->flushByTag('UnitTestTag%special');
905  }
906 
912  {
913  $mockCache = $this->createMock(AbstractFrontend::class);
914  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
915 
916  $backend = $this->getMockBuilder(FileBackend::class)
917  ->setMethods(['isCacheFileExpired'])
918  ->disableOriginalConstructor()
919  ->getMock();
920  $backend->expects(self::exactly(2))->method('isCacheFileExpired')->will(self::onConsecutiveCalls(
921  true,
922  false
923  ));
924  $backend->setCacheDirectory('vfs://Foo/');
925  $backend->setCache($mockCache);
926 
927  $data = 'some data';
928  $backend->set('BackendFileTest1', $data);
929  $backend->set('BackendFileTest2', $data);
930 
931  self::assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
932  self::assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
933 
934  $backend->collectGarbage();
935 
936  // @todo remove condition and else branch as soon as phpunit v8 goes out of support
937  if (method_exists($this, 'assertFileDoesNotExist')) {
938  self::assertFileDoesNotExist('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
939  } else {
940  self::assertFileNotExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest1');
941  }
942 
943  self::assertFileExists('vfs://Foo/cache/data/UnitTestCache/BackendFileTest2');
944  }
945 
950  public function ‪flushUnfreezesTheCache(): void
951  {
952  $mockCache = $this->createMock(AbstractFrontend::class);
953  $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache');
954 
955  $backend = $this->getMockBuilder(FileBackend::class)
956  ->setMethods(['dummy'])
957  ->disableOriginalConstructor()
958  ->getMock();
959  $backend->setCacheDirectory('vfs://Foo/');
960  $backend->setCache($mockCache);
961 
962  $backend->freeze();
963 
964  self::assertTrue($backend->isFrozen());
965  $backend->flush();
966  self::assertFalse($backend->isFrozen());
967  }
968 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryThrowsExceptionOnNonWritableDirectory
‪setCacheDirectoryThrowsExceptionOnNonWritableDirectory()
Definition: FileBackendTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireIncludesAndReturnsResultOfIncludedPhpFile
‪requireIncludesAndReturnsResultOfIncludedPhpFile()
Definition: FileBackendTest.php:717
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsAbsolutePathWithoutTrailingSlash
‪setCacheDirectoryAllowsAbsolutePathWithoutTrailingSlash()
Definition: FileBackendTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: FileBackendTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\removeThrowsExceptionForInvalidIdentifier
‪removeThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:598
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\hasThrowsExceptionForInvalidIdentifier
‪hasThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:579
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setReallySavesToTheSpecifiedDirectory
‪setReallySavesToTheSpecifiedDirectory()
Definition: FileBackendTest.php:215
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsRelativePathWithoutTrailingSlash
‪setCacheDirectoryAllowsRelativePathWithoutTrailingSlash()
Definition: FileBackendTest.php:91
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\EXPIRYTIME_LENGTH
‪const EXPIRYTIME_LENGTH
Definition: FileBackend.php:33
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setUp
‪setUp()
Definition: FileBackendTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getReturnsFalseForExpiredEntries
‪getReturnsFalseForExpiredEntries()
Definition: FileBackendTest.php:368
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsAbsoluteDottedPathWithoutTrailingSlash
‪setCacheDirectoryAllowsAbsoluteDottedPathWithoutTrailingSlash()
Definition: FileBackendTest.php:135
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\flushUnfreezesTheCache
‪flushUnfreezesTheCache()
Definition: FileBackendTest.php:950
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsRelativeDottedPathWithTrailingSlash
‪setCacheDirectoryAllowsRelativeDottedPathWithTrailingSlash()
Definition: FileBackendTest.php:124
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getThrowsExceptionForInvalidIdentifier
‪getThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:556
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireThrowsExceptionForInvalidIdentifier
‪requireThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:697
‪TYPO3\CMS\Core\Cache\Backend\FileBackend
Definition: FileBackend.php:30
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireOnceDoesNotCheckExpiryTimeIfBackendIsFrozen
‪requireOnceDoesNotCheckExpiryTimeIfBackendIsFrozen()
Definition: FileBackendTest.php:669
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\aDedicatedCacheDirectoryIsUsedForCodeCaches
‪aDedicatedCacheDirectoryIsUsedForCodeCaches()
Definition: FileBackendTest.php:175
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireOnceIncludesAndReturnsResultOfIncludedPhpFile
‪requireOnceIncludesAndReturnsResultOfIncludedPhpFile()
Definition: FileBackendTest.php:644
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireDoesNotCheckExpiryTimeIfBackendIsFrozen
‪requireDoesNotCheckExpiryTimeIfBackendIsFrozen()
Definition: FileBackendTest.php:740
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen
‪getDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen()
Definition: FileBackendTest.php:388
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setAlsoSavesSpecifiedTags
‪setAlsoSavesSpecifiedTags()
Definition: FileBackendTest.php:271
‪TYPO3\CMS\Core\Cache\Exception\InvalidDataException
Definition: InvalidDataException.php:24
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest
Definition: FileBackendTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag
‪findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag()
Definition: FileBackendTest.php:783
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\hasDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen
‪hasDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen()
Definition: FileBackendTest.php:455
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\removeReallyRemovesACacheEntry
‪removeReallyRemovesACacheEntry()
Definition: FileBackendTest.php:479
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\hasReturnsTrueIfAnEntryExists
‪hasReturnsTrueIfAnEntryExists()
Definition: FileBackendTest.php:412
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setThrowsExceptionForInvalidIdentifier
‪setThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:532
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\findIdentifiersByTagDoesNotReturnExpiredEntries
‪findIdentifiersByTagDoesNotReturnExpiredEntries()
Definition: FileBackendTest.php:811
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireOnceThrowsExceptionForInvalidIdentifier
‪requireOnceThrowsExceptionForInvalidIdentifier(string $identifier)
Definition: FileBackendTest.php:622
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getReturnsContentOfTheCorrectCacheFile
‪getReturnsContentOfTheCorrectCacheFile()
Definition: FileBackendTest.php:340
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsAbsolutePathWithTrailingSlash
‪setCacheDirectoryAllowsAbsolutePathWithTrailingSlash()
Definition: FileBackendTest.php:81
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\collectGarbageRemovesExpiredCacheEntries
‪collectGarbageRemovesExpiredCacheEntries()
Definition: FileBackendTest.php:911
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\invalidEntryIdentifiers
‪invalidEntryIdentifiers()
Definition: FileBackendTest.php:507
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDetectsAndLoadsAFrozenCache
‪setCacheDetectsAndLoadsAFrozenCache()
Definition: FileBackendTest.php:304
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\hasReturnsFalseForExpiredEntries
‪hasReturnsFalseForExpiredEntries()
Definition: FileBackendTest.php:436
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\getCacheDirectoryReturnsTheCurrentCacheDirectory
‪getCacheDirectoryReturnsTheCurrentCacheDirectory()
Definition: FileBackendTest.php:156
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\requireCanLoadSameEntryMultipleTimes
‪requireCanLoadSameEntryMultipleTimes()
Definition: FileBackendTest.php:765
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsRelativeDottedPathWithoutTrailingSlash
‪setCacheDirectoryAllowsRelativeDottedPathWithoutTrailingSlash()
Definition: FileBackendTest.php:113
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\flushCreatesCacheDirectoryAgain
‪flushCreatesCacheDirectoryAgain()
Definition: FileBackendTest.php:871
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier
‪setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier()
Definition: FileBackendTest.php:242
‪TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend
Definition: AbstractFrontend.php:27
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: FileBackendTest.php:836
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsAbsoluteDottedPathWithTrailingSlash
‪setCacheDirectoryAllowsAbsoluteDottedPathWithTrailingSlash()
Definition: FileBackendTest.php:145
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setThrowsExceptionIfDataIsNotAString
‪setThrowsExceptionIfDataIsNotAString()
Definition: FileBackendTest.php:194
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\setCacheDirectoryAllowsRelativePathWithTrailingSlash
‪setCacheDirectoryAllowsRelativePathWithTrailingSlash()
Definition: FileBackendTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\FileBackendTest\flushByTagRemovesCacheEntriesWithSpecifiedTag
‪flushByTagRemovesCacheEntriesWithSpecifiedTag()
Definition: FileBackendTest.php:890