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