TYPO3 CMS  TYPO3_8-7
FileBackendTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
27 class FileBackendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
28 {
32  protected function setUp()
33  {
34  if (!class_exists('org\\bovigo\\vfs\\vfsStreamWrapper')) {
35  $this->markTestSkipped('File backend tests are not available with this phpunit version.');
36  }
37 
38  vfsStreamWrapper::register();
39  vfsStreamWrapper::setRoot(new vfsStreamDirectory('Foo'));
40  }
41 
46  {
47  $this->expectException(\TYPO3\CMS\Core\Cache\Exception::class);
48  $this->expectExceptionCode(1303669848);
49 
50  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
51 
52  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
53  ->setMethods(['dummy'])
54  ->disableOriginalConstructor()
55  ->getMock();
56  $backend->setCacheDirectory('http://localhost/');
57 
58  $backend->setCache($mockCache);
59  }
60 
65  {
66  $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, ['dummy'], [], '', false);
67  $backend->setCacheDirectory('/tmp/foo');
68  $this->assertEquals('/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
69  }
70 
75  {
76  $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, ['dummy'], [], '', false);
77  $backend->setCacheDirectory('/tmp/foo/');
78  $this->assertEquals('/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
79  }
80 
85  {
86  $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, ['dummy'], [], '', false);
87  $backend->setCacheDirectory('tmp/foo');
88  // get PATH_site without trailing slash
89  $path = GeneralUtility::fixWindowsFilePath(realpath(PATH_site));
90  $this->assertEquals($path . '/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
91  }
92 
97  {
98  $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, ['dummy'], [], '', false);
99  $backend->setCacheDirectory('tmp/foo/');
100  // get PATH_site without trailing slash
101  $path = GeneralUtility::fixWindowsFilePath(realpath(PATH_site));
102  $this->assertEquals($path . '/tmp/foo/', $backend->_get('temporaryCacheDirectory'));
103  }
104 
109  {
110  $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, ['dummy'], [], '', false);
111  $backend->setCacheDirectory('../tmp/foo');
112  // get PATH_site without trailing slash
113  $path = GeneralUtility::fixWindowsFilePath(realpath(PATH_site));
114  $this->assertEquals($path . '/../tmp/foo/', $backend->_get('temporaryCacheDirectory'));
115  }
116 
121  {
122  $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, ['dummy'], [], '', false);
123  $backend->setCacheDirectory('../tmp/foo/');
124  // get PATH_site without trailing slash
125  $path = GeneralUtility::fixWindowsFilePath(realpath(PATH_site));
126  $this->assertEquals($path . '/../tmp/foo/', $backend->_get('temporaryCacheDirectory'));
127  }
128 
133  {
134  $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, ['dummy'], [], '', false);
135  $backend->setCacheDirectory('/tmp/../foo');
136  $this->assertEquals('/tmp/../foo/', $backend->_get('temporaryCacheDirectory'));
137  }
138 
143  {
144  $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, ['dummy'], [], '', false);
145  $backend->setCacheDirectory('/tmp/../foo/');
146  $this->assertEquals('/tmp/../foo/', $backend->_get('temporaryCacheDirectory'));
147  }
148 
153  {
154  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
155  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
156 
157  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
158  ->setMethods(['dummy'])
159  ->disableOriginalConstructor()
160  ->getMock();
161  $backend->setCacheDirectory('vfs://Foo/');
162  $backend->setCache($mockCache);
163 
164  $this->assertEquals('vfs://Foo/Cache/Data/SomeCache/', $backend->getCacheDirectory());
165  }
166 
171  {
172  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\PhpFrontend::class);
173  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
174 
175  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
176  ->setMethods(['dummy'])
177  ->disableOriginalConstructor()
178  ->getMock();
179  $backend->setCacheDirectory('vfs://Foo/');
180  $backend->setCache($mockCache);
181 
182  $this->assertEquals('vfs://Foo/Cache/Code/SomeCache/', $backend->getCacheDirectory());
183  }
184 
189  {
190  $this->expectException(InvalidDataException::class);
191  $this->expectExceptionCode(1204481674);
192 
193  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
194 
195  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
196  ->setMethods(['dummy'])
197  ->disableOriginalConstructor()
198  ->getMock();
199  $backend->setCacheDirectory('vfs://Foo/');
200  $backend->setCache($mockCache);
201 
202  $backend->set('some identifier', ['not a string']);
203  }
204 
209  {
210  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
211  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
212 
213  $data = 'some data' . microtime();
214  $entryIdentifier = 'BackendFileTest';
215  $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
216 
217  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
218  ->setMethods(['dummy'])
219  ->disableOriginalConstructor()
220  ->getMock();
221  $backend->setCacheDirectory('vfs://Foo/');
222  $backend->setCache($mockCache);
223 
224  $backend->set($entryIdentifier, $data);
225 
226  $this->assertFileExists($pathAndFilename);
227  $retrievedData = file_get_contents($pathAndFilename, null, null, 0, strlen($data));
228  $this->assertEquals($data, $retrievedData);
229  }
230 
235  {
236  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
237  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
238 
239  $data1 = 'some data' . microtime();
240  $data2 = 'some data' . microtime();
241  $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
242 
243  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
244  ->setMethods(['dummy'])
245  ->disableOriginalConstructor()
246  ->getMock();
247  $backend->setCacheDirectory('vfs://Foo/');
248  $backend->setCache($mockCache);
249 
250  $backend->set($entryIdentifier, $data1, [], 500);
251  $backend->set($entryIdentifier, $data2, [], 200);
252 
253  $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
254  $this->assertFileExists($pathAndFilename);
255  $retrievedData = file_get_contents($pathAndFilename, null, null, 0, strlen($data2));
256  $this->assertEquals($data2, $retrievedData);
257  }
258 
262  public function setAlsoSavesSpecifiedTags()
263  {
264  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
265  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
266 
267  $data = 'some data' . microtime();
268  $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
269 
270  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
271  ->setMethods(['dummy'])
272  ->disableOriginalConstructor()
273  ->getMock();
274  $backend->setCacheDirectory('vfs://Foo/');
275  $backend->setCache($mockCache);
276 
277  $backend->set($entryIdentifier, $data, ['Tag1', 'Tag2']);
278 
279  $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
280  $this->assertFileExists($pathAndFilename);
281  $retrievedData = file_get_contents($pathAndFilename, null, null, (strlen($data) + \TYPO3\CMS\Core\Cache\Backend\FileBackend::EXPIRYTIME_LENGTH), 9);
282  $this->assertEquals('Tag1 Tag2', $retrievedData);
283  }
284 
289  {
290  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
291  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
292 
293  $data = 'some data' . microtime();
294  $entryIdentifier = 'BackendFileTest';
295 
296  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
297  ->setMethods(['dummy'])
298  ->disableOriginalConstructor()
299  ->getMock();
300  $backend->setCacheDirectory('vfs://Foo/');
301  $backend->setCache($mockCache);
302 
303  $backend->set($entryIdentifier, $data, ['Tag1', 'Tag2']);
304 
305  $backend->freeze();
306 
307  unset($backend);
308 
309  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
310  ->setMethods(['dummy'])
311  ->disableOriginalConstructor()
312  ->getMock();
313  $backend->setCacheDirectory('vfs://Foo/');
314  $backend->setCache($mockCache);
315 
316  $this->assertTrue($backend->isFrozen());
317  $this->assertEquals($data, $backend->get($entryIdentifier));
318  }
319 
324  {
325  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
326  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
327 
328  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
329  ->setMethods(['setTag'])
330  ->disableOriginalConstructor()
331  ->getMock();
332  $backend->setCacheDirectory('vfs://Foo/');
333  $backend->setCache($mockCache);
334 
335  $entryIdentifier = 'BackendFileTest';
336 
337  $data = 'some data' . microtime();
338  $backend->set($entryIdentifier, $data, [], 500);
339 
340  $data = 'some other data' . microtime();
341  $backend->set($entryIdentifier, $data, [], 100);
342 
343  $loadedData = $backend->get($entryIdentifier);
344  $this->assertEquals($data, $loadedData);
345  }
346 
351  {
352  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
353  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
354 
355  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
356  ->setMethods(['isCacheFileExpired'])
357  ->disableOriginalConstructor()
358  ->getMock();
359  $backend->expects($this->once())->method('isCacheFileExpired')->with('vfs://Foo/Cache/Data/UnitTestCache/ExpiredEntry')->will($this->returnValue(true));
360  $backend->setCacheDirectory('vfs://Foo/');
361  $backend->setCache($mockCache);
362 
363  $this->assertFalse($backend->get('ExpiredEntry'));
364  }
365 
370  {
371  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
372  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
373 
374  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
375  ->setMethods(['isCacheFileExpired'])
376  ->disableOriginalConstructor()
377  ->getMock();
378  $backend->setCacheDirectory('vfs://Foo/');
379  $backend->setCache($mockCache);
380 
381  $backend->expects($this->once())->method('isCacheFileExpired');
382 
383  $backend->set('foo', 'some data');
384  $backend->freeze();
385  $this->assertEquals('some data', $backend->get('foo'));
386  $this->assertFalse($backend->get('bar'));
387  }
388 
393  {
394  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
395  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
396 
397  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
398  ->setMethods(['dummy'])
399  ->disableOriginalConstructor()
400  ->getMock();
401  $backend->setCacheDirectory('vfs://Foo/');
402  $backend->setCache($mockCache);
403 
404  $entryIdentifier = 'BackendFileTest';
405 
406  $data = 'some data' . microtime();
407  $backend->set($entryIdentifier, $data);
408 
409  $this->assertTrue($backend->has($entryIdentifier), 'has() did not return TRUE.');
410  $this->assertFalse($backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
411  }
412 
417  {
418  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
419  ->setMethods(['isCacheFileExpired'])
420  ->disableOriginalConstructor()
421  ->getMock();
422  $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(true, false));
423 
424  $this->assertFalse($backend->has('foo'));
425  $this->assertTrue($backend->has('bar'));
426  }
427 
432  {
433  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
434  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
435 
436  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
437  ->setMethods(['isCacheFileExpired'])
438  ->disableOriginalConstructor()
439  ->getMock();
440  $backend->setCacheDirectory('vfs://Foo/');
441  $backend->setCache($mockCache);
442 
443  $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
444 
445  $backend->set('foo', 'some data');
446  $backend->freeze();
447  $this->assertTrue($backend->has('foo'));
448  $this->assertFalse($backend->has('bar'));
449  }
450 
455  {
456  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
457  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
458 
459  $data = 'some data' . microtime();
460  $entryIdentifier = 'BackendFileTest';
461  $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
462 
463  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
464  ->setMethods(['dummy'])
465  ->disableOriginalConstructor()
466  ->getMock();
467  $backend->setCacheDirectory('vfs://Foo/');
468  $backend->setCache($mockCache);
469 
470  $backend->set($entryIdentifier, $data);
471  $this->assertFileExists($pathAndFilename);
472 
473  $backend->remove($entryIdentifier);
474  $this->assertFileNotExists($pathAndFilename);
475  }
476 
479  public function invalidEntryIdentifiers()
480  {
481  return [
482  'trailing slash' => ['/myIdentifer'],
483  'trailing dot and slash' => ['./myIdentifer'],
484  'trailing two dots and slash' => ['../myIdentifier'],
485  'trailing with multiple dots and slashes' => ['.././../myIdentifier'],
486  'slash in middle part' => ['my/Identifier'],
487  'dot and slash in middle part' => ['my./Identifier'],
488  'two dots and slash in middle part' => ['my../Identifier'],
489  'multiple dots and slashes in middle part' => ['my.././../Identifier'],
490  'pending slash' => ['myIdentifier/'],
491  'pending dot and slash' => ['myIdentifier./'],
492  'pending dots and slash' => ['myIdentifier../'],
493  'pending multiple dots and slashes' => ['myIdentifier.././../'],
494  ];
495  }
496 
501  public function setThrowsExceptionForInvalidIdentifier($identifier)
502  {
503  $this->expectException(\InvalidArgumentException::class);
504  $this->expectExceptionCode(1282073032);
505 
506  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
507  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
508 
509  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
510  ->setMethods(['dummy'])
511  ->setConstructorArgs(['test'])
512  ->getMock();
513  $backend->setCacheDirectory('vfs://Foo/');
514  $backend->setCache($mockCache);
515 
516  $backend->set($identifier, 'cache data', []);
517  }
518 
523  public function getThrowsExceptionForInvalidIdentifier($identifier)
524  {
525  $this->expectException(\InvalidArgumentException::class);
526  $this->expectExceptionCode(1282073033);
527 
528  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
529  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
530 
531  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
532  ->setMethods(['dummy'])
533  ->disableOriginalConstructor()
534  ->getMock();
535  $backend->setCacheDirectory('vfs://Foo/');
536  $backend->setCache($mockCache);
537 
538  $backend->get($identifier);
539  }
540 
545  public function hasThrowsExceptionForInvalidIdentifier($identifier)
546  {
547  $this->expectException(\InvalidArgumentException::class);
548  $this->expectExceptionCode(1282073034);
549 
550  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
551  ->setMethods(['dummy'])
552  ->disableOriginalConstructor()
553  ->getMock();
554 
555  $backend->has($identifier);
556  }
557 
562  public function removeThrowsExceptionForInvalidIdentifier($identifier)
563  {
564  $this->expectException(\InvalidArgumentException::class);
565  $this->expectExceptionCode(1282073035);
566 
567  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
568  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
569 
570  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
571  ->setMethods(['dummy'])
572  ->disableOriginalConstructor()
573  ->getMock();
574  $backend->setCacheDirectory('vfs://Foo/');
575  $backend->setCache($mockCache);
576 
577  $backend->remove($identifier);
578  }
579 
585  {
586  $this->expectException(\InvalidArgumentException::class);
587  $this->expectExceptionCode(1282073036);
588 
589  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
590  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
591 
592  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
593  ->setMethods(['dummy'])
594  ->disableOriginalConstructor()
595  ->getMock();
596  $backend->setCacheDirectory('vfs://Foo/');
597  $backend->setCache($mockCache);
598 
599  $backend->requireOnce($identifier);
600  }
601 
606  {
607  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
608  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
609 
610  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
611  ->setMethods(['dummy'])
612  ->disableOriginalConstructor()
613  ->getMock();
614  $backend->setCacheDirectory('vfs://Foo/');
615  $backend->setCache($mockCache);
616 
617  $entryIdentifier = 'SomePhpEntry';
618 
619  $data = '<?php return "foo"; ?>';
620  $backend->set($entryIdentifier, $data);
621 
622  $loadedData = $backend->requireOnce($entryIdentifier);
623  $this->assertEquals('foo', $loadedData);
624  }
625 
630  {
631  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
632  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
633 
634  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
635  ->setMethods(['isCacheFileExpired'])
636  ->disableOriginalConstructor()
637  ->getMock();
638  $backend->setCacheDirectory('vfs://Foo/');
639  $backend->setCache($mockCache);
640 
641  $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
642 
643  $data = '<?php return "foo"; ?>';
644  $backend->set('FooEntry', $data);
645 
646  $backend->freeze();
647 
648  $loadedData = $backend->requireOnce('FooEntry');
649  $this->assertEquals('foo', $loadedData);
650  }
651 
656  {
657  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
658  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
659 
660  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
661  ->setMethods(['dummy'])
662  ->disableOriginalConstructor()
663  ->getMock();
664  $backend->setCacheDirectory('vfs://Foo/');
665  $backend->setCache($mockCache);
666 
667  $data = 'some data' . microtime();
668  $backend->set('BackendFileTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
669  $backend->set('BackendFileTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
670  $backend->set('BackendFileTest3', $data, ['UnitTestTag%test']);
671 
672  $expectedEntry = 'BackendFileTest2';
673 
674  $actualEntries = $backend->findIdentifiersByTag('UnitTestTag%special');
675  $this->assertInternalType('array', $actualEntries);
676  $this->assertEquals($expectedEntry, array_pop($actualEntries));
677  }
678 
683  {
684  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
685  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
686 
687  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
688  ->setMethods(['dummy'])
689  ->disableOriginalConstructor()
690  ->getMock();
691  $backend->setCacheDirectory('vfs://Foo/');
692  $backend->setCache($mockCache);
693 
694  $data = 'some data';
695  $backend->set('BackendFileTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
696  $backend->set('BackendFileTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special'], -100);
697  $backend->set('BackendFileTest3', $data, ['UnitTestTag%test']);
698 
699  $this->assertSame([], $backend->findIdentifiersByTag('UnitTestTag%special'));
700  $this->assertSame(['BackendFileTest1', 'BackendFileTest3'], $backend->findIdentifiersByTag('UnitTestTag%test'));
701  }
702 
706  public function flushRemovesAllCacheEntries()
707  {
708  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
709  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
710 
711  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
712  ->setMethods(['dummy'])
713  ->disableOriginalConstructor()
714  ->getMock();
715  $backend->setCacheDirectory('vfs://Foo/');
716  $backend->setCache($mockCache);
717 
718  $data = 'some data';
719  $backend->set('BackendFileTest1', $data);
720  $backend->set('BackendFileTest2', $data);
721 
722  $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
723  $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
724 
725  $backend->flush();
726 
727  $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
728  $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
729  }
730 
735  {
736  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
737  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
738 
739  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
740  ->setMethods(['dummy'])
741  ->disableOriginalConstructor()
742  ->getMock();
743  $backend->setCacheDirectory('vfs://Foo/');
744  $backend->setCache($mockCache);
745 
746  $backend->flush();
747  $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/');
748  }
749 
754  {
755  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
756  ->setMethods(['findIdentifiersByTag', 'remove'])
757  ->disableOriginalConstructor()
758  ->getMock();
759 
760  $backend->expects($this->once())->method('findIdentifiersByTag')->with('UnitTestTag%special')->will($this->returnValue(['foo', 'bar', 'baz']));
761  $backend->expects($this->at(1))->method('remove')->with('foo');
762  $backend->expects($this->at(2))->method('remove')->with('bar');
763  $backend->expects($this->at(3))->method('remove')->with('baz');
764 
765  $backend->flushByTag('UnitTestTag%special');
766  }
767 
772  {
773  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
774  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
775 
776  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
777  ->setMethods(['isCacheFileExpired'])
778  ->disableOriginalConstructor()
779  ->getMock();
780  $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(true, false));
781  $backend->setCacheDirectory('vfs://Foo/');
782  $backend->setCache($mockCache);
783 
784  $data = 'some data';
785  $backend->set('BackendFileTest1', $data);
786  $backend->set('BackendFileTest2', $data);
787 
788  $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
789  $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
790 
791  $backend->collectGarbage();
792  $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
793  $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
794  }
795 
799  public function flushUnfreezesTheCache()
800  {
801  $mockCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class);
802  $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
803 
804  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class)
805  ->setMethods(['dummy'])
806  ->disableOriginalConstructor()
807  ->getMock();
808  $backend->setCacheDirectory('vfs://Foo/');
809  $backend->setCache($mockCache);
810 
811  $backend->freeze();
812 
813  $this->assertTrue($backend->isFrozen());
814  $backend->flush();
815  $this->assertFalse($backend->isFrozen());
816  }
817 }