added unit tests for the caching framework (backported from FLOW3)
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / backend / t3lib_cache_backend_filebackendtestcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 // TODO implement autoloading so that we only require stuff we really need
27 require_once(PATH_t3lib . 'class.t3lib_cache.php');
28
29 require_once(PATH_t3lib . 'cache/backend/interfaces/interface.t3lib_cache_backend_backend.php');
30 require_once(PATH_t3lib . 'cache/frontend/interfaces/interface.t3lib_cache_frontend_frontend.php');
31
32 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_abstractbackend.php');
33 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php');
34 require_once(PATH_t3lib . 'cache/class.t3lib_cache_exception.php');
35 require_once(PATH_t3lib . 'cache/class.t3lib_cache_factory.php');
36 require_once(PATH_t3lib . 'cache/class.t3lib_cache_manager.php');
37 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_variablefrontend.php');
38
39 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_classalreadyloaded.php');
40 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_duplicateidentifier.php');
41 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invalidbackend.php');
42 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invalidcache.php');
43 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invaliddata.php');
44 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_nosuchcache.php');
45
46 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_filebackend.php');
47
48 /**
49 * Testcase for the Fsile cache backend
50 *
51 * This file is a backport from FLOW3
52 *
53 * @author Ingo Renner <ingo@typo3.org>
54 * @package TYPO3
55 * @subpackage tests
56 * @version $Id$
57 */
58 class t3lib_cache_backend_FileBackendTestCase extends tx_phpunit_testcase {
59
60 /**
61 * If set, the tearDown() method will clean up the cache subdirectory used by this unit test.
62 *
63 * @var t3lib_cache_backend_FileBackend
64 */
65 protected $backend;
66
67 protected $testingCacheDirectory;
68
69 /**
70 * Sets up this testcase
71 *
72 * @return void
73 */
74 public function setUp() {
75 $this->testingCacheDirectory = 'typo3temp/cache/testing/';
76
77 $this->backend = t3lib_div::makeInstance(
78 't3lib_cache_backend_FileBackend',
79 array('cacheDirectory' => $this->testingCacheDirectory)
80 );
81 }
82
83 /**
84 * @test
85 * @author Robert Lemke <robert@typo3.org>
86 * @author Ingo Renner <ingo@typo3.org>
87 */
88 public function defaultCacheDirectoryIsWritable() {
89 $cacheDirectory = $this->backend->getCacheDirectory();
90
91 $this->assertTrue(is_writable($cacheDirectory), 'The default cache directory "' . $cacheDirectory . '" is not writable.');
92 }
93
94 /**
95 * @test
96 * @author Robert Lemke <robert@typo3.org>
97 * @expectedException t3lib_cache_Exception
98 */
99 public function setCacheDirectoryThrowsExceptionOnNonWritableDirectory() {
100 if (DIRECTORY_SEPARATOR == '\\') {
101 $this->markTestSkipped('test not reliable in Windows environment');
102 }
103 $directoryName = '/sbin';
104
105 $this->backend->setCacheDirectory($directoryName);
106 }
107
108 /**
109 * @test
110 * @author Robert Lemke <robert@typo3.org>
111 * @author Ingo Renner <ingo@typo3.org>
112 */
113 public function getCacheDirectoryReturnsThePreviouslySetDirectory() {
114 $directory = $this->testingCacheDirectory;
115 $fullPathToDirectory = t3lib_div::getIndpEnv('TYPO3_DOCUMENT_ROOT') . '/' . $directory;
116
117 $this->backend->setCacheDirectory($directory);
118 $this->assertEquals($fullPathToDirectory, $this->backend->getCacheDirectory(), 'getDirectory() did not return the expected value.');
119 }
120
121 /**
122 * @test
123 * @author Robert Lemke <robert@typo3.org>
124 * @author Ingo Renner <ingo@typo3.org>
125 * @expectedException t3lib_cache_exception_InvalidData
126 */
127 public function setThrowsExceptionIfDataIsNotAString() {
128 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
129 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
130 array(),
131 '',
132 FALSE
133 );
134
135 $data = array('Some data');
136 $entryIdentifier = 'BackendFileTest';
137
138 $this->backend->setCache($cache);
139
140 $this->backend->set($entryIdentifier, $data);
141 }
142
143 /**
144 * @test
145 * @author Robert Lemke <robert@typo3.org>
146 * @author Ingo Renner <ingo@typo3.org>
147 */
148 public function setReallySavesToTheSpecifiedDirectory() {
149 $cacheIdentifier = 'UnitTestCache';
150 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
151 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
152 array(),
153 '',
154 FALSE
155 );
156
157 $cache->expects($this->atLeastOnce())
158 ->method('getIdentifier')
159 ->will($this->returnValue($cacheIdentifier));
160
161 $data = 'some data' . microtime();
162 $entryIdentifier = 'BackendFileTest';
163 $entryIdentifierHash = sha1($entryIdentifier);
164
165 $this->backend->setCache($cache);
166 $this->backend->set($entryIdentifier, $data);
167
168 $cacheDirectory = $this->backend->getCacheDirectory();
169 $pattern = $cacheDirectory
170 . 'data/'
171 . $cacheIdentifier . '/'
172 . $entryIdentifierHash[0] . '/'
173 . $entryIdentifierHash[1] . '/'
174 . t3lib_cache_backend_FileBackend::FILENAME_EXPIRYTIME_GLOB
175 . t3lib_cache_backend_FileBackend::SEPARATOR
176 . $entryIdentifier;
177 $filesFound = glob($pattern);
178 $this->assertTrue(is_array($filesFound), 'filesFound was no array.');
179
180 $retrievedData = file_get_contents(array_pop($filesFound));
181 $this->assertEquals(
182 $data,
183 $retrievedData,
184 'The original and the retrieved data don\'t match.'
185 );
186 }
187
188 /**
189 * @test
190 * @author Robert Lemke <robert@typo3.org>
191 * @author Ingo Renner <ingo@typo3.org>
192 */
193 public function setRemovesAnAlreadyExistingCacheEntryForTheSameIdentifier() {
194 $cacheIdentifier = 'UnitTestCache';
195 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
196 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
197 array(),
198 '',
199 FALSE
200 );
201 $cache->expects($this->atLeastOnce())
202 ->method('getIdentifier')
203 ->will($this->returnValue($cacheIdentifier));
204
205 $data1 = 'some data' . microtime();
206 $data2 = 'some data' . microtime();
207 $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
208 $entryIdentifierHash = sha1($entryIdentifier);
209
210 $this->backend->setCache($cache);
211 $this->backend->set($entryIdentifier, $data1, array(), 500);
212 // setting a second entry with the same identifier, but different
213 // data, this should _replace_ the existing one we set before
214 $this->backend->set($entryIdentifier, $data2, array(), 200);
215
216 $cacheDirectory = $this->backend->getCacheDirectory();
217 $pattern = $cacheDirectory
218 . 'data/'
219 . $cacheIdentifier . '/'
220 . $entryIdentifierHash[0] . '/'
221 . $entryIdentifierHash[1] . '/'
222 . t3lib_cache_backend_FileBackend::FILENAME_EXPIRYTIME_GLOB
223 . t3lib_cache_backend_FileBackend::SEPARATOR
224 . $entryIdentifier;
225 $filesFound = glob($pattern);
226 $this->assertEquals(1, count($filesFound), 'There was not exactly one cache entry.');
227 }
228
229 /**
230 * @test
231 * @author Robert Lemke <robert@typo3.org>
232 * @author Ingo Renner <ingo@typo3.org>
233 */
234 public function setReallySavesSpecifiedTags() {
235 $cacheIdentifier = 'UnitTestCache';
236 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
237 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
238 array(),
239 '',
240 FALSE
241 );
242 $cache->expects($this->atLeastOnce())
243 ->method('getIdentifier')
244 ->will($this->returnValue($cacheIdentifier));
245
246 $data = 'some data' . microtime();
247 $entryIdentifier = 'BackendFileTest';
248 $entryIdentifierHash = sha1($entryIdentifier);
249
250 $this->backend->setCache($cache);
251 $tagsDirectory = $this->backend->getCacheDirectory() . 'tags/';
252
253 $this->backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
254
255 $this->assertTrue(is_dir($tagsDirectory . 'UnitTestTag%tag1'), 'Tag directory UnitTestTag%tag1 does not exist.');
256 $this->assertTrue(is_dir($tagsDirectory . 'UnitTestTag%tag2'), 'Tag directory UnitTestTag%tag2 does not exist.');
257
258 $filename = $tagsDirectory . 'UnitTestTag%tag1/' . $cacheIdentifier . t3lib_cache_backend_FileBackend::SEPARATOR . $entryIdentifier;
259 $this->assertTrue(file_exists($filename), 'File "' . $filename . '" does not exist.');
260
261 $filename = $tagsDirectory . 'UnitTestTag%tag2/' . $cacheIdentifier . t3lib_cache_backend_FileBackend::SEPARATOR . $entryIdentifier;
262 $this->assertTrue(file_exists($filename), 'File "' . $filename . '" does not exist.');
263 }
264
265 /**
266 * @test
267 * @author Robert Lemke <robert@typo3.org>
268 * @author Ingo Renner <ingo@typo3.org>
269 */
270 public function getReturnsContentOfTheCorrectCacheFile() {
271 $cacheIdentifier = 'UnitTestCache';
272 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
273 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
274 array(),
275 '',
276 FALSE
277 );
278 $cache->expects($this->atLeastOnce())
279 ->method('getIdentifier')
280 ->will($this->returnValue($cacheIdentifier));
281
282 $this->backend->setCache($cache);
283
284 $entryIdentifier = 'BackendFileTest';
285
286 $data = 'some data' . microtime();
287 $this->backend->set($entryIdentifier, $data, array(), 500);
288
289 $data = 'some other data' . microtime();
290 $this->backend->set($entryIdentifier, $data, array(), 100);
291
292 $loadedData = $this->backend->get($entryIdentifier);
293
294 $this->assertEquals($data, $loadedData, 'The original and the retrieved data don\'t match.');
295 }
296
297 /**
298 * @test
299 * @author Robert Lemke <robert@typo3.org>
300 * @author Ingo Renner <ingo@typo3.org>
301 */
302 public function hasReturnsTheCorrectResult() {
303 $cacheIdentifier = 'UnitTestCache';
304 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
305 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
306 array(),
307 '',
308 FALSE
309 );
310 $cache->expects($this->atLeastOnce())
311 ->method('getIdentifier')
312 ->will($this->returnValue($cacheIdentifier));
313
314 $this->backend->setCache($cache);
315
316 $entryIdentifier = 'BackendFileTest';
317
318 $data = 'some data' . microtime();
319 $this->backend->set($entryIdentifier, $data);
320
321 $this->assertTrue($this->backend->has($entryIdentifier), 'has() did not return TRUE.');
322 $this->assertFalse($this->backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
323 }
324
325 /**
326 * @test
327 * @author Robert Lemke <robert@typo3.org>
328 * @author Ingo Renner <ingo@typo3.org>
329 */
330 public function removeReallyRemovesACacheEntry() {
331 $cacheIdentifier = 'UnitTestCache';
332 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
333 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
334 array(),
335 '',
336 FALSE
337 );
338 $cache->expects($this->atLeastOnce())
339 ->method('getIdentifier')
340 ->will($this->returnValue($cacheIdentifier));
341
342 $data = 'some data' . microtime();
343 $entryIdentifier = 'BackendFileRemovalTest';
344 $entryIdentifierHash = sha1($entryIdentifier);
345
346 $cacheDirectory = $this->backend->getCacheDirectory();
347 $this->backend->setCache($cache);
348
349 $pattern = $cacheDirectory
350 . 'data/'
351 . $cacheIdentifier . '/'
352 . $entryIdentifierHash[0] . '/'
353 . $entryIdentifierHash[1] . '/'
354 . t3lib_cache_backend_FileBackend::FILENAME_EXPIRYTIME_GLOB
355 . t3lib_cache_backend_FileBackend::SEPARATOR
356 . $entryIdentifier;
357 $this->backend->set($entryIdentifier, $data);
358 $filesFound = glob($pattern);
359 $this->assertTrue(is_array($filesFound) && count($filesFound) > 0, 'The cache entry does not exist.');
360
361 $this->backend->remove($entryIdentifier);
362 $filesFound = glob($pattern);
363 $this->assertTrue(count($filesFound) == 0, 'The cache entry still exists.');
364 }
365
366 /**
367 * @test
368 * @author Robert Lemke <robert@typo3.org>
369 * @author Karsten Dambekalns <karsten@typo3.org>
370 * @author Ingo Renner <ingo@typo3.org>
371 */
372 public function collectGarbageReallyRemovesAnExpiredCacheEntry() {
373 $cacheIdentifier = 'UnitTestCache';
374 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
375 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
376 array(),
377 '',
378 FALSE
379 );
380 $cache->expects($this->atLeastOnce())
381 ->method('getIdentifier')
382 ->will($this->returnValue($cacheIdentifier));
383
384 $data = 'some data' . microtime();
385 $entryIdentifier = 'BackendFileRemovalTest';
386 $entryIdentifierHash = sha1($entryIdentifier);
387
388 $cacheDirectory = $this->backend->getCacheDirectory();
389 $this->backend->setCache($cache);
390
391 $pattern = $cacheDirectory
392 . 'data/'
393 . $cacheIdentifier . '/'
394 . $entryIdentifierHash[0] . '/'
395 . $entryIdentifierHash[1] . '/'
396 . t3lib_cache_backend_FileBackend::FILENAME_EXPIRYTIME_GLOB
397 . t3lib_cache_backend_FileBackend::SEPARATOR
398 . $entryIdentifier;
399
400 $this->backend->set($entryIdentifier, $data, array(), 1);
401 $filesFound = glob($pattern);
402 $this->assertTrue(is_array($filesFound) && count($filesFound) > 0, 'The cache entry does not exist.');
403
404 sleep(2);
405
406 $this->backend->collectGarbage($entryIdentifier);
407 $filesFound = glob($pattern);
408 $this->assertTrue(count($filesFound) == 0, 'The cache entry still exists.');
409 }
410
411 /**
412 * @test
413 * @author Robert Lemke <robert@typo3.org>
414 * @author Karsten Dambekalns <karsten@typo3.org>
415 * @author Ingo Renner <ingo@typo3.org>
416 */
417 public function collectGarbageReallyRemovesAllExpiredCacheEntries() {
418 $cacheIdentifier = 'UnitTestCache';
419 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
420 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
421 array(),
422 '',
423 FALSE
424 );
425 $cache->expects($this->atLeastOnce())
426 ->method('getIdentifier')
427 ->will($this->returnValue($cacheIdentifier));
428
429 $data = 'some data' . microtime();
430 $entryIdentifier = 'BackendFileRemovalTest';
431
432 $cacheDirectory = $this->backend->getCacheDirectory();
433 $this->backend->setCache($cache);
434
435 $pattern = $cacheDirectory
436 . 'data/'
437 . $cacheIdentifier . '/*/*/'
438 . t3lib_cache_backend_FileBackend::FILENAME_EXPIRYTIME_GLOB
439 . t3lib_cache_backend_FileBackend::SEPARATOR
440 . $entryIdentifier
441 . '?';
442
443 $this->backend->set($entryIdentifier . 'A', $data, array(), 1);
444 $this->backend->set($entryIdentifier . 'B', $data, array(), 1);
445 $this->backend->set($entryIdentifier . 'C', $data, array(), 1);
446 $filesFound = glob($pattern);
447 $this->assertTrue(is_array($filesFound) && count($filesFound) > 0, 'The cache entries do not exist.');
448
449 sleep(2);
450
451 $this->backend->collectGarbage();
452 $filesFound = glob($pattern);
453 $this->assertTrue(count($filesFound) == 0, 'The cache entries still exist.');
454 }
455
456 /**
457 * @test
458 * @author Robert Lemke <robert@typo3.org>
459 * @author Ingo Renner <ingo@typo3.org>
460 */
461 public function removeReallyRemovesTagsOfRemovedEntry() {
462 $cacheIdentifier = 'UnitTestCache';
463 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
464 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
465 array(),
466 '',
467 FALSE
468 );
469 $cache->expects($this->atLeastOnce())
470 ->method('getIdentifier')
471 ->will($this->returnValue($cacheIdentifier));
472
473 $data = 'some data' . microtime();
474 $entryIdentifier = 'BackendFileTest';
475
476 $this->backend->setCache($cache);
477
478 $tagsDirectory = $this->backend->getCacheDirectory() . 'tags/';
479
480 $this->backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
481 $this->backend->remove($entryIdentifier);
482
483 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%tag1/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%tag1/' . $entryIdentifier . '" still exists.');
484 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%tag2/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%tag2/' . $entryIdentifier . '" still exists.');
485 }
486
487 /**
488 * @test
489 * @author Robert Lemke <robert@typo3.org>
490 * @author Ingo Renner <ingo@typo3.org>
491 */
492 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
493 $cacheIdentifier = 'UnitTestCache';
494 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
495 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
496 array(),
497 '',
498 FALSE
499 );
500 $cache->expects($this->atLeastOnce())
501 ->method('getIdentifier')
502 ->will($this->returnValue($cacheIdentifier));
503
504 $this->backend->setCache($cache);
505
506 $data = 'some data' . microtime();
507 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
508 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
509 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
510
511 $expectedEntry = 'BackendFileTest2';
512
513 $actualEntries = $this->backend->findIdentifiersByTag('UnitTestTag%special');
514 $this->assertTrue(is_array($actualEntries), 'actualEntries is not an array.');
515
516 $this->assertEquals($expectedEntry, array_pop($actualEntries));
517 }
518
519 /**
520 * @test
521 * @author Robert Lemke <robert@typo3.org>
522 * @author Ingo Renner <ingo@typo3.org>
523 */
524 public function flushRemovesAllCacheEntriesAndRelatedTags() {
525 $cacheIdentifier = 'UnitTestCache';
526 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
527 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
528 array(),
529 '',
530 FALSE
531 );
532 $cache->expects($this->atLeastOnce())
533 ->method('getIdentifier')
534 ->will($this->returnValue($cacheIdentifier));
535
536 $this->backend->setCache($cache);
537
538 $tagsDirectory = $this->backend->getCacheDirectory() . 'tags/';
539 $cacheDirectory = $this->backend->getCacheDirectory() . 'data/' . $cacheIdentifier . '/';
540
541 $data = 'some data' . microtime();
542 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test'));
543 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
544 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
545
546 $this->backend->flush();
547
548 $pattern = $cacheDirectory . '*/*/*';
549 $filesFound = glob($pattern);
550 $this->assertTrue(count($filesFound) == 0, 'Still files in the cache directory');
551
552 $entryIdentifier = 'BackendFileTest1';
553 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier . '" still exists.');
554 $entryIdentifier = 'BackendFileTest2';
555 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier . '" still exists.');
556 $entryIdentifier = 'BackendFileTest3';
557 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier . '" still exists.');
558 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%special/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%special/' . $entryIdentifier . '" still exists.');
559 }
560
561 /**
562 * @test
563 * @author Robert Lemke <robert@typo3.org>
564 * @author Ingo Renner <ingo@typo3.org>
565 */
566 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
567 $cacheIdentifier = 'UnitTestCache';
568 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
569 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
570 array(),
571 '',
572 FALSE
573 );
574 $cache->expects($this->atLeastOnce())
575 ->method('getIdentifier')
576 ->will($this->returnValue($cacheIdentifier));
577
578 $this->backend->setCache($cache);
579
580 $data = 'some data' . microtime();
581 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
582 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
583 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
584
585 $this->backend->flushByTag('UnitTestTag%special');
586
587 $this->assertTrue($this->backend->has('BackendFileTest1'), 'BackendFileTest1');
588 $this->assertFalse($this->backend->has('BackendFileTest2'), 'BackendFileTest2');
589 $this->assertTrue($this->backend->has('BackendFileTest3'), 'BackendFileTest3');
590 }
591
592
593 /**
594 * @test
595 * @author Robert Lemke <robert@typo3.org>
596 * @author Karsten Dambekalns <karsten@typo3.org>
597 * @author Ingo Renner <ingo@typo3.org>
598 */
599 public function hasReturnsTheCorrectResultForEntryWithExceededLifetime() {
600 $cacheIdentifier = 'UnitTestCache';
601 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
602 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
603 array(),
604 '',
605 FALSE
606 );
607 $cache->expects($this->atLeastOnce())
608 ->method('getIdentifier')
609 ->will($this->returnValue($cacheIdentifier));
610
611 $this->backend->setCache($cache);
612
613 $entryIdentifier = 'BackendFileTest';
614 $data = 'some data' . microtime();
615 $this->backend->set($entryIdentifier, $data);
616
617 $expiredEntryIdentifier = 'ExpiredBackendFileTest';
618 $expiredData = 'some old data' . microtime();
619 $this->backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
620
621 sleep(2);
622
623 $this->assertFalse($this->backend->has($expiredEntryIdentifier), 'has() did not return FALSE.');
624 }
625
626 /**
627 * @test
628 * @author Karsten Dambekalns <karsten@typo3.org>
629 * @author Ingo Renner <ingo@typo3.org>
630 */
631 public function getReturnsFalseForEntryWithExceededLifetime() {
632 $cacheIdentifier = 'UnitTestCache';
633 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
634 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
635 array(),
636 '',
637 FALSE
638 );
639 $cache->expects($this->atLeastOnce())
640 ->method('getIdentifier')
641 ->will($this->returnValue($cacheIdentifier));
642
643 $this->backend->setCache($cache);
644
645 $entryIdentifier = 'BackendFileTest';
646 $data = 'some data' . microtime();
647 $this->backend->set($entryIdentifier, $data);
648
649 $expiredEntryIdentifier = 'ExpiredBackendFileTest';
650 $expiredData = 'some old data' . microtime();
651 $this->backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
652
653 sleep(2);
654
655 $this->assertEquals($data, $this->backend->get($entryIdentifier), 'The original and the retrieved data don\'t match.');
656 $this->assertFalse($this->backend->get($expiredEntryIdentifier), 'The expired entry could be loaded.');
657 }
658
659 /**
660 * @test
661 * @author Karsten Dambekalns <karsten@typo3.org>
662 * @author Ingo Renner <ingo@typo3.org>
663 */
664 public function findIdentifiersByTagReturnsEmptyArrayForEntryWithExceededLifetime() {
665 $cacheIdentifier = 'UnitTestCache';
666 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
667 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
668 array(),
669 '',
670 FALSE
671 );
672 $cache->expects($this->atLeastOnce())
673 ->method('getIdentifier')
674 ->will($this->returnValue($cacheIdentifier));
675
676 $this->backend->setCache($cache);
677
678 $this->backend->set('BackendFileTest', 'some data', array('UnitTestTag%special'), 1);
679
680 sleep(2);
681
682 $this->assertEquals(array(), $this->backend->findIdentifiersByTag('UnitTestTag%special'));
683 }
684
685 /**
686 * @test
687 * @author Robert Lemke <robert@typo3.org>
688 * @author Karsten Dambekalns <karsten@typo3.org>
689 * @author Ingo Renner <ingo@typo3.org>
690 */
691 public function setWithUnlimitedLifetimeWritesCorrectEntry() {
692 $cacheIdentifier = 'UnitTestCache';
693 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
694 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
695 array(),
696 '',
697 FALSE
698 );
699 $cache->expects($this->atLeastOnce())
700 ->method('getIdentifier')
701 ->will($this->returnValue($cacheIdentifier));
702
703 $data = 'some data' . microtime();
704 $entryIdentifier = 'BackendFileTest';
705 $entryIdentifierHash = sha1($entryIdentifier);
706
707 $this->backend->setCache($cache);
708 $this->backend->set($entryIdentifier, $data, array(), 0);
709
710 $cacheDirectory = $this->backend->getCacheDirectory();
711
712 $pattern = $cacheDirectory
713 . 'data/'
714 . $cacheIdentifier . '/'
715 . $entryIdentifierHash[0] . '/'
716 . $entryIdentifierHash[1] . '/'
717 . t3lib_cache_backend_FileBackend::FILENAME_EXPIRYTIME_UNLIMITED
718 . t3lib_cache_backend_FileBackend::SEPARATOR
719 . $entryIdentifier;
720
721 $filesFound = glob($pattern);
722 $this->assertTrue(is_array($filesFound), 'filesFound was no array.');
723
724 $retrievedData = file_get_contents(array_pop($filesFound));
725 $this->assertEquals($data, $retrievedData, 'The original and the retrieved data don\'t match.');
726 }
727
728 /**
729 * @test
730 * @author Robert Lemke <robert@typo3.org>
731 * @author Ingo Renner <ingo@typo3.org>
732 */
733 public function tearDown() {
734 if (is_object($this->backend)) {
735 $directory = $this->backend->getCacheDirectory();
736 if (is_dir($directory)) {
737 t3lib_div::rmdir($directory, true);
738 }
739 }
740 }
741 }
742
743 ?>