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