1069c654d71922d42e2d448218bdc1a349eb5981
[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 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use \org\bovigo\vfs\vfsStreamDirectory;
28 use \org\bovigo\vfs\vfsStreamWrapper;
29
30 /**
31 * Testcase for the File cache backend
32 *
33 * This file is a backport from FLOW3
34 *
35 * @author Ingo Renner <ingo@typo3.org>
36 * @author Christian Kuhn <lolli@schwarzbu.ch>
37 */
38 class FileBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
39
40 /**
41 * Sets up this testcase
42 *
43 * @return void
44 */
45 public function setUp() {
46 if (!class_exists('org\\bovigo\\vfs\\vfsStreamWrapper')) {
47 $this->markTestSkipped('File backend tests are not available with this phpunit version.');
48 }
49
50 vfsStreamWrapper::register();
51 vfsStreamWrapper::setRoot(new vfsStreamDirectory('Foo'));
52 }
53
54 /**
55 * @test
56 * @expectedException \TYPO3\CMS\Core\Cache\Exception
57 */
58 public function setCacheDirectoryThrowsExceptionOnNonWritableDirectory() {
59 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
60
61 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
62 $backend->setCacheDirectory('http://localhost/');
63
64 $backend->setCache($mockCache);
65 }
66
67 /**
68 * @test
69 */
70 public function getCacheDirectoryReturnsTheCurrentCacheDirectory() {
71 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
72 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
73
74 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
75 $backend->setCacheDirectory('vfs://Foo/');
76 $backend->setCache($mockCache);
77
78 $this->assertEquals('vfs://Foo/Cache/Data/SomeCache/', $backend->getCacheDirectory());
79 }
80
81 /**
82 * @test
83 */
84 public function aDedicatedCacheDirectoryIsUsedForCodeCaches() {
85 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\PhpFrontend', array(), array(), '', FALSE);
86 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('SomeCache'));
87
88 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
89 $backend->setCacheDirectory('vfs://Foo/');
90 $backend->setCache($mockCache);
91
92 $this->assertEquals('vfs://Foo/Cache/Code/SomeCache/', $backend->getCacheDirectory());
93 }
94
95 /**
96 * @test
97 * @expectedException \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
98 */
99 public function setThrowsExceptionIfDataIsNotAString() {
100 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
101
102 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
103 $backend->setCacheDirectory('vfs://Foo/');
104 $backend->setCache($mockCache);
105
106 $backend->set('some identifier', array('not a string'));
107 }
108
109 /**
110 * @test
111 */
112 public function setReallySavesToTheSpecifiedDirectory() {
113 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
114 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
115
116 $data = 'some data' . microtime();
117 $entryIdentifier = 'BackendFileTest';
118 $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
119
120 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
121 $backend->setCacheDirectory('vfs://Foo/');
122 $backend->setCache($mockCache);
123
124 $backend->set($entryIdentifier, $data);
125
126 $this->assertFileExists($pathAndFilename);
127 $retrievedData = file_get_contents($pathAndFilename, NULL, NULL, 0, strlen($data));
128 $this->assertEquals($data, $retrievedData);
129 }
130
131 /**
132 * @test
133 */
134 public function setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier() {
135 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
136 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
137
138 $data1 = 'some data' . microtime();
139 $data2 = 'some data' . microtime();
140 $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
141
142 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
143 $backend->setCacheDirectory('vfs://Foo/');
144 $backend->setCache($mockCache);
145
146 $backend->set($entryIdentifier, $data1, array(), 500);
147 $backend->set($entryIdentifier, $data2, array(), 200);
148
149 $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
150 $this->assertFileExists($pathAndFilename);
151 $retrievedData = file_get_contents($pathAndFilename, NULL, NULL, 0, strlen($data2));
152 $this->assertEquals($data2, $retrievedData);
153 }
154
155 /**
156 * @test
157 */
158 public function setAlsoSavesSpecifiedTags() {
159 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
160 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
161
162 $data = 'some data' . microtime();
163 $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
164
165 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
166 $backend->setCacheDirectory('vfs://Foo/');
167 $backend->setCache($mockCache);
168
169 $backend->set($entryIdentifier, $data, array('Tag1', 'Tag2'));
170
171 $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
172 $this->assertFileExists($pathAndFilename);
173 $retrievedData = file_get_contents($pathAndFilename, NULL, NULL, (strlen($data) + \TYPO3\CMS\Core\Cache\Backend\FileBackend::EXPIRYTIME_LENGTH), 9);
174 $this->assertEquals('Tag1 Tag2', $retrievedData);
175 }
176
177 /**
178 * @test
179 */
180 public function setCacheDetectsAndLoadsAFrozenCache() {
181 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
182 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
183
184 $data = 'some data' . microtime();
185 $entryIdentifier = 'BackendFileTest';
186
187 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
188 $backend->setCacheDirectory('vfs://Foo/');
189 $backend->setCache($mockCache);
190
191 $backend->set($entryIdentifier, $data, array('Tag1', 'Tag2'));
192
193 $backend->freeze();
194
195 unset($backend);
196
197 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
198 $backend->setCacheDirectory('vfs://Foo/');
199 $backend->setCache($mockCache);
200
201 $this->assertTrue($backend->isFrozen());
202 $this->assertEquals($data, $backend->get($entryIdentifier));
203 }
204
205 /**
206 * @test
207 */
208 public function getReturnsContentOfTheCorrectCacheFile() {
209 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
210 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
211
212 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('setTag'), array(), '', FALSE);
213 $backend->setCacheDirectory('vfs://Foo/');
214 $backend->setCache($mockCache);
215
216 $entryIdentifier = 'BackendFileTest';
217
218 $data = 'some data' . microtime();
219 $backend->set($entryIdentifier, $data, array(), 500);
220
221 $data = 'some other data' . microtime();
222 $backend->set($entryIdentifier, $data, array(), 100);
223
224 $loadedData = $backend->get($entryIdentifier);
225 $this->assertEquals($data, $loadedData);
226 }
227
228 /**
229 * @test
230 */
231 public function getReturnsFalseForExpiredEntries() {
232 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
233 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
234
235 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('isCacheFileExpired'), array(), '', FALSE);
236 $backend->expects($this->once())->method('isCacheFileExpired')->with('vfs://Foo/Cache/Data/UnitTestCache/ExpiredEntry')->will($this->returnValue(TRUE));
237 $backend->setCacheDirectory('vfs://Foo/');
238 $backend->setCache($mockCache);
239
240 $this->assertFalse($backend->get('ExpiredEntry'));
241 }
242
243 /**
244 * @test
245 */
246 public function getDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen() {
247 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
248 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
249
250 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('isCacheFileExpired'), array(), '', FALSE);
251 $backend->setCacheDirectory('vfs://Foo/');
252 $backend->setCache($mockCache);
253
254 $backend->expects($this->once())->method('isCacheFileExpired');
255
256 $backend->set('foo', 'some data');
257 $backend->freeze();
258 $this->assertEquals('some data', $backend->get('foo'));
259 $this->assertFalse($backend->get('bar'));
260 }
261
262 /**
263 * @test
264 */
265 public function hasReturnsTrueIfAnEntryExists() {
266 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
267 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
268
269 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
270 $backend->setCacheDirectory('vfs://Foo/');
271 $backend->setCache($mockCache);
272
273 $entryIdentifier = 'BackendFileTest';
274
275 $data = 'some data' . microtime();
276 $backend->set($entryIdentifier, $data);
277
278 $this->assertTrue($backend->has($entryIdentifier), 'has() did not return TRUE.');
279 $this->assertFalse($backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
280 }
281
282 /**
283 * @test
284 */
285 public function hasReturnsFalseForExpiredEntries() {
286 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('isCacheFileExpired'), array(), '', FALSE);
287 $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(TRUE, FALSE));
288
289 $this->assertFalse($backend->has('foo'));
290 $this->assertTrue($backend->has('bar'));
291 }
292
293 /**
294 * @test
295 */
296 public function hasDoesNotCheckIfAnEntryIsExpiredIfTheCacheIsFrozen() {
297 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
298 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
299
300 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('isCacheFileExpired'), array(), '', FALSE);
301 $backend->setCacheDirectory('vfs://Foo/');
302 $backend->setCache($mockCache);
303
304 $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
305
306 $backend->set('foo', 'some data');
307 $backend->freeze();
308 $this->assertTrue($backend->has('foo'));
309 $this->assertFalse($backend->has('bar'));
310 }
311
312 /**
313 * @test
314 */
315 public function removeReallyRemovesACacheEntry() {
316 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
317 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
318
319 $data = 'some data' . microtime();
320 $entryIdentifier = 'BackendFileTest';
321 $pathAndFilename = 'vfs://Foo/Cache/Data/UnitTestCache/' . $entryIdentifier;
322
323 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
324 $backend->setCacheDirectory('vfs://Foo/');
325 $backend->setCache($mockCache);
326
327 $backend->set($entryIdentifier, $data);
328 $this->assertFileExists($pathAndFilename);
329
330 $backend->remove($entryIdentifier);
331 $this->assertFileNotExists($pathAndFilename);
332 }
333
334 /**
335 */
336 public function invalidEntryIdentifiers() {
337 return array(
338 'trailing slash' => array('/myIdentifer'),
339 'trailing dot and slash' => array('./myIdentifer'),
340 'trailing two dots and slash' => array('../myIdentifier'),
341 'trailing with multiple dots and slashes' => array('.././../myIdentifier'),
342 'slash in middle part' => array('my/Identifier'),
343 'dot and slash in middle part' => array('my./Identifier'),
344 'two dots and slash in middle part' => array('my../Identifier'),
345 'multiple dots and slashes in middle part' => array('my.././../Identifier'),
346 'pending slash' => array('myIdentifier/'),
347 'pending dot and slash' => array('myIdentifier./'),
348 'pending dots and slash' => array('myIdentifier../'),
349 'pending multiple dots and slashes' => array('myIdentifier.././../'),
350 );
351 }
352
353 /**
354 * @test
355 * @dataProvider invalidEntryIdentifiers
356 * @expectedException \InvalidArgumentException
357 */
358 public function setThrowsExceptionForInvalidIdentifier($identifier) {
359 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
360 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
361
362 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array('test'), '', TRUE);
363 $backend->setCacheDirectory('vfs://Foo/');
364 $backend->setCache($mockCache);
365
366 $backend->set($identifier, 'cache data', array());
367 }
368
369 /**
370 * @test
371 * @dataProvider invalidEntryIdentifiers
372 * @expectedException \InvalidArgumentException
373 */
374 public function getThrowsExceptionForInvalidIdentifier($identifier) {
375 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
376 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
377
378 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
379 $backend->setCacheDirectory('vfs://Foo/');
380 $backend->setCache($mockCache);
381
382 $backend->get($identifier);
383 }
384
385 /**
386 * @test
387 * @dataProvider invalidEntryIdentifiers
388 * @expectedException \InvalidArgumentException
389 */
390 public function hasThrowsExceptionForInvalidIdentifier($identifier) {
391 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
392
393 $backend->has($identifier);
394 }
395
396 /**
397 * @test
398 * @dataProvider invalidEntryIdentifiers
399 * @expectedException \InvalidArgumentException
400 */
401 public function removeThrowsExceptionForInvalidIdentifier($identifier) {
402 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
403 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
404
405 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
406 $backend->setCacheDirectory('vfs://Foo/');
407 $backend->setCache($mockCache);
408
409 $backend->remove($identifier);
410 }
411
412 /**
413 * @test
414 * @dataProvider invalidEntryIdentifiers
415 * @expectedException \InvalidArgumentException
416 */
417 public function requireOnceThrowsExceptionForInvalidIdentifier($identifier) {
418 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
419 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
420
421 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
422 $backend->setCacheDirectory('vfs://Foo/');
423 $backend->setCache($mockCache);
424
425 $backend->requireOnce($identifier);
426 }
427
428 /**
429 * @test
430 */
431 public function requireOnceIncludesAndReturnsResultOfIncludedPhpFile() {
432 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
433 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
434
435 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
436 $backend->setCacheDirectory('vfs://Foo/');
437 $backend->setCache($mockCache);
438
439 $entryIdentifier = 'SomePhpEntry';
440
441 $data = '<?php return "foo"; ?>';
442 $backend->set($entryIdentifier, $data);
443
444 $loadedData = $backend->requireOnce($entryIdentifier);
445 $this->assertEquals('foo', $loadedData);
446 }
447
448 /**
449 * @test
450 */
451 public function requireOnceDoesNotCheckExpiryTimeIfBackendIsFrozen() {
452 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
453 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
454
455 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('isCacheFileExpired'), array(), '', FALSE);
456 $backend->setCacheDirectory('vfs://Foo/');
457 $backend->setCache($mockCache);
458
459 $backend->expects($this->once())->method('isCacheFileExpired'); // Indirectly called by freeze() -> get()
460
461 $data = '<?php return "foo"; ?>';
462 $backend->set('FooEntry', $data);
463
464 $backend->freeze();
465
466 $loadedData = $backend->requireOnce('FooEntry');
467 $this->assertEquals('foo', $loadedData);
468 }
469
470 /**
471 * @test
472 */
473 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
474 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
475 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
476
477 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
478 $backend->setCacheDirectory('vfs://Foo/');
479 $backend->setCache($mockCache);
480
481 $data = 'some data' . microtime();
482 $backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
483 $backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
484 $backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
485
486 $expectedEntry = 'BackendFileTest2';
487
488 $actualEntries = $backend->findIdentifiersByTag('UnitTestTag%special');
489 $this->assertInternalType('array', $actualEntries);
490 $this->assertEquals($expectedEntry, array_pop($actualEntries));
491 }
492
493 /**
494 * @test
495 */
496 public function findIdentifiersByTagDoesNotReturnExpiredEntries() {
497 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
498 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
499
500 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
501 $backend->setCacheDirectory('vfs://Foo/');
502 $backend->setCache($mockCache);
503
504 $data = 'some data';
505 $backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
506 $backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'), -100);
507 $backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
508
509 $this->assertSame(array(), $backend->findIdentifiersByTag('UnitTestTag%special'));
510 $this->assertSame(array('BackendFileTest1', 'BackendFileTest3'), $backend->findIdentifiersByTag('UnitTestTag%test'));
511 }
512
513 /**
514 * @test
515 */
516 public function flushRemovesAllCacheEntries() {
517 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
518 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
519
520 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
521 $backend->setCacheDirectory('vfs://Foo/');
522 $backend->setCache($mockCache);
523
524 $data = 'some data';
525 $backend->set('BackendFileTest1', $data);
526 $backend->set('BackendFileTest2', $data);
527
528 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
529 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
530
531 $backend->flush();
532
533 $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
534 $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
535 }
536
537 /**
538 * @test
539 */
540 public function flushCreatesCacheDirectoryAgain() {
541 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
542 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
543
544 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
545 $backend->setCacheDirectory('vfs://Foo/');
546 $backend->setCache($mockCache);
547
548 $backend->flush();
549 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/');
550 }
551
552 /**
553 * @test
554 */
555 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
556 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('findIdentifiersByTag', 'remove'), array(), '', FALSE);
557
558 $backend->expects($this->once())->method('findIdentifiersByTag')->with('UnitTestTag%special')->will($this->returnValue(array('foo', 'bar', 'baz')));
559 $backend->expects($this->at(1))->method('remove')->with('foo');
560 $backend->expects($this->at(2))->method('remove')->with('bar');
561 $backend->expects($this->at(3))->method('remove')->with('baz');
562
563 $backend->flushByTag('UnitTestTag%special');
564 }
565
566 /**
567 * @test
568 */
569 public function collectGarbageRemovesExpiredCacheEntries() {
570 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
571 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
572
573 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('isCacheFileExpired'), array(), '', FALSE);
574 $backend->expects($this->exactly(2))->method('isCacheFileExpired')->will($this->onConsecutiveCalls(TRUE, FALSE));
575 $backend->setCacheDirectory('vfs://Foo/');
576 $backend->setCache($mockCache);
577
578 $data = 'some data';
579 $backend->set('BackendFileTest1', $data);
580 $backend->set('BackendFileTest2', $data);
581
582 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
583 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
584
585 $backend->collectGarbage();
586 $this->assertFileNotExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest1');
587 $this->assertFileExists('vfs://Foo/Cache/Data/UnitTestCache/BackendFileTest2');
588 }
589
590 /**
591 * @test
592 */
593 public function flushUnfreezesTheCache() {
594 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
595 $mockCache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
596
597 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\FileBackend', array('dummy'), array(), '', FALSE);
598 $backend->setCacheDirectory('vfs://Foo/');
599 $backend->setCache($mockCache);
600
601 $backend->freeze();
602
603 $this->assertTrue($backend->isFrozen());
604 $backend->flush();
605 $this->assertFalse($backend->isFrozen());
606 }
607 }