86f5aa66f2d4e3417ae647ce19fda0301f2ea949
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / backend / class.t3lib_cache_backend_dbbackendTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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 * Testcase for the DB cache backend
27 *
28 * @author Ingo Renner <ingo@typo3.org>
29 * @package TYPO3
30 * @subpackage tests
31 */
32 class t3lib_cache_backend_DbBackendTest extends tx_phpunit_testcase {
33
34 /**
35 * Enable backup of global and system variables
36 *
37 * @var boolean
38 */
39 protected $backupGlobals = TRUE;
40
41 /**
42 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
43 * because resource types cannot be handled during serializing
44 *
45 * @var array
46 */
47 protected $backupGlobalsBlacklist = array('TYPO3_DB');
48
49 /**
50 * @var t3lib_DB Backup of original TYPO3_DB instance
51 */
52 protected $typo3DbBackup;
53
54 /**
55 * @var string Name of the testing data table
56 */
57 protected $testingCacheTable = 'cachingframework_Testing';
58
59 /**
60 * @var string Name of the testing tags table
61 */
62 protected $testingTagsTable = 'cachingframework_Testing_tags';
63
64 /**
65 * Set up testcases
66 */
67 public function setUp() {
68 $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
69 }
70
71 /**
72 * Sets up the backend used for testing
73 *
74 * @return void
75 * @author Ingo Renner <ingo@typo3.org>
76 * @author Christian Kuhn <lolli@schwarzbu.ch>
77 */
78 protected function setUpBackend(array $backendOptions = array()) {
79 $GLOBALS['TYPO3_DB']->sql_query('CREATE TABLE ' . $this->testingCacheTable . ' (
80 id int(11) unsigned NOT NULL auto_increment,
81 identifier varchar(250) DEFAULT \'\' NOT NULL,
82 expires int(11) unsigned DEFAULT \'0\' NOT NULL,
83 content mediumblob,
84 PRIMARY KEY (id),
85 KEY cache_id (identifier, expires)
86 ) ENGINE=InnoDB;
87 ');
88
89 $GLOBALS['TYPO3_DB']->sql_query('CREATE TABLE ' . $this->testingTagsTable . ' (
90 id int(11) unsigned NOT NULL auto_increment,
91 identifier varchar(250) DEFAULT \'\' NOT NULL,
92 tag varchar(250) DEFAULT \'\' NOT NULL,
93 PRIMARY KEY (id),
94 KEY cache_id (identifier),
95 KEY cache_tag (tag)
96 ) ENGINE=InnoDB;
97 ');
98
99 $backend = t3lib_div::makeInstance(
100 't3lib_cache_backend_DbBackend',
101 'Testing',
102 $backendOptions
103 );
104
105 return $backend;
106 }
107
108 /**
109 * Helper method to inject a mock frontend to backend instance
110 *
111 * @param t3lib_cache_backend_DbBackend $backend Current backend instance
112 * @return t3lib_cache_frontend_Frontend Mock frontend
113 */
114 protected function setUpMockFrontendOfBackend(t3lib_cache_backend_DbBackend $backend) {
115 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array(), array(), '', FALSE);
116 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('Testing'));
117 $backend->setCache($mockCache);
118
119 return $mockCache;
120 }
121
122 /**
123 * @author Ingo Renner <ingo@typo3.org>
124 */
125 public function tearDown() {
126 $GLOBALS['TYPO3_DB']->sql_query(
127 'DROP TABLE IF EXISTS ' . $this->testingCacheTable . ';'
128 );
129
130 $GLOBALS['TYPO3_DB']->sql_query(
131 'DROP TABLE IF EXISTS ' . $this->testingTagsTable . ';'
132 );
133
134 $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup;
135 }
136
137 /**
138 * @test
139 * @author Ingo Renner <ingo@typo3.org>
140 */
141 public function setCacheCalculatesCacheTableName() {
142 $backend = $this->setUpBackend();
143 $this->setUpMockFrontendOfBackend($backend);
144 $this->assertEquals($this->testingCacheTable, $backend->getCacheTable());
145 }
146
147 /**
148 * @test
149 */
150 public function setCacheCalculatesTagsTableName() {
151 $backend = $this->setUpBackend();
152 $this->setUpMockFrontendOfBackend($backend);
153
154 $this->assertEquals($this->testingTagsTable, $backend->getTagsTable());
155 }
156
157 /**
158 * @test
159 * @expectedException t3lib_cache_Exception
160 */
161 public function setThrowsExceptionIfFrontendWasNotSet() {
162 $backend = $this->setUpBackend();
163 $backend->set('identifier', 'data');
164 }
165
166 /**
167 * @test
168 * @expectedException t3lib_cache_exception_InvalidData
169 * @author Ingo Renner <ingo@typo3.org>
170 */
171 public function setThrowsExceptionIfDataIsNotAString() {
172 $backend = $this->setUpBackend();
173 $this->setUpMockFrontendOfBackend($backend);
174
175 $data = array('Some data');
176 $entryIdentifier = 'BackendDbTest';
177
178 $backend->set($entryIdentifier, $data);
179 }
180
181 /**
182 * @test
183 * @author Ingo Renner <ingo@typo3.org>
184 */
185 public function setInsertsEntryInTable() {
186 $backend = $this->setUpBackend();
187 $this->setUpMockFrontendOfBackend($backend);
188
189 $data = 'some data' . microtime();
190 $entryIdentifier = 'BackendDbTest';
191
192 $backend->set($entryIdentifier, $data);
193
194 $entryFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
195 '*',
196 $this->testingCacheTable,
197 'identifier = \'' . $entryIdentifier . '\''
198 );
199
200 $this->assertEquals($data, $entryFound['content']);
201 }
202
203 /**
204 * @test
205 * @author Ingo Renner <ingo@typo3.org>
206 */
207 public function setRemovesAnAlreadyExistingCacheEntryForTheSameIdentifier() {
208 $backend = $this->setUpBackend();
209 $this->setUpMockFrontendOfBackend($backend);
210
211 $data1 = 'some data' . microtime();
212 $data2 = $data1 . '_different';
213 $entryIdentifier = 'BackendDbRemoveBeforeSetTest';
214
215 $backend->set($entryIdentifier, $data1, array(), 500);
216 $backend->set($entryIdentifier, $data2, array(), 200);
217
218 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
219 '*',
220 $this->testingCacheTable,
221 'identifier = \'' . $entryIdentifier . '\''
222 );
223
224 $this->assertEquals(1, count($entriesFound));
225 }
226
227 /**
228 * @test
229 * @author Ingo Renner <ingo@typo3.org>
230 */
231 public function setReallySavesSpecifiedTags() {
232 $backend = $this->setUpBackend();
233 $this->setUpMockFrontendOfBackend($backend);
234
235 $data = 'some data' . microtime();
236 $entryIdentifier = 'BackendDbTest';
237
238 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
239
240 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
241 '*',
242 $this->testingTagsTable,
243 'identifier = \'' . $entryIdentifier . '\''
244 );
245
246 $tags = array();
247
248 foreach ($entriesFound as $entry) {
249 $tags[] = $entry['tag'];
250 }
251
252 $this->assertTrue(count($tags) > 0);
253 $this->assertTrue(in_array('UnitTestTag%tag1', $tags));
254 $this->assertTrue(in_array('UnitTestTag%tag2', $tags));
255 }
256
257 /**
258 * @test
259 * @author Christian Kuhn <lolli@schwarzbu.ch>
260 */
261 public function setSavesCompressedDataWithEnabledCompression() {
262 $backend = $this->setUpBackend(
263 array(
264 'compression' => TRUE,
265 )
266 );
267 $this->setUpMockFrontendOfBackend($backend);
268
269 $data = 'some data ' . microtime();
270 $entryIdentifier = 'BackendDbTest';
271
272 $backend->set($entryIdentifier, $data);
273
274 $entry = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
275 'content',
276 $this->testingCacheTable,
277 'identifier = \'' . $entryIdentifier . '\''
278 );
279
280 $this->assertEquals($data, @gzuncompress($entry['content']));
281 }
282
283 /**
284 * @test
285 * @author Christian Kuhn <lolli@schwarzbu.ch>
286 */
287 public function setSavesPlaintextDataWithEnabledCompressionAndCompressionLevel0() {
288 $backend = $this->setUpBackend(
289 array(
290 'compression' => TRUE,
291 'compressionLevel' => 0,
292 )
293 );
294 $this->setUpMockFrontendOfBackend($backend);
295
296 $data = 'some data ' . microtime();
297 $entryIdentifier = 'BackendDbTest';
298
299 $backend->set($entryIdentifier, $data);
300
301 $entry = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
302 'content',
303 $this->testingCacheTable,
304 'identifier = \'' . $entryIdentifier . '\''
305 );
306
307 $this->assertGreaterThan(0, substr_count($entry['content'], $data));
308 }
309
310 /**
311 * @test
312 * @expectedException t3lib_cache_Exception
313 */
314 public function getThrowsExceptionIfFrontendWasNotSet() {
315 $backend = $this->setUpBackend();
316 $backend->get('identifier');
317 }
318
319 /**
320 * @test
321 * @author Ingo Renner <ingo@typo3.org>
322 */
323 public function getReturnsContentOfTheCorrectCacheEntry() {
324 $backend = $this->setUpBackend();
325 $this->setUpMockFrontendOfBackend($backend);
326
327 $data = 'some data' . microtime();
328 $entryIdentifier = 'BackendDbTest';
329
330 $backend->set($entryIdentifier, $data, array(), 500);
331
332 $data = 'some other data' . microtime();
333 $backend->set($entryIdentifier, $data, array(), 100);
334
335 $loadedData = $backend->get($entryIdentifier);
336
337 $this->assertEquals($data, $loadedData);
338 }
339
340 /**
341 * @test
342 * @expectedException t3lib_cache_Exception
343 */
344 public function hasThrowsExceptionIfFrontendWasNotSet() {
345 $backend = $this->setUpBackend();
346 $backend->has('identifier');
347 }
348
349 /**
350 * @test
351 * @author Ingo Renner <ingo@typo3.org>
352 */
353 public function hasReturnsTheCorrectResult() {
354 $backend = $this->setUpBackend();
355 $this->setUpMockFrontendOfBackend($backend);
356
357 $data = 'some data' . microtime();
358 $entryIdentifier = 'BackendDbTest';
359
360 $backend->set($entryIdentifier, $data);
361
362 $this->assertTrue($backend->has($entryIdentifier));
363 $this->assertFalse($backend->has($entryIdentifier . 'Not'));
364 }
365
366 /**
367 * @test
368 * @expectedException t3lib_cache_Exception
369 */
370 public function removeThrowsExceptionIfFrontendWasNotSet() {
371 $backend = $this->setUpBackend();
372 $backend->remove('identifier');
373 }
374
375 /**
376 * @test
377 * @author Ingo Renner <ingo@typo3.org>
378 */
379 public function removeReallyRemovesACacheEntry() {
380 $backend = $this->setUpBackend();
381 $this->setUpMockFrontendOfBackend($backend);
382
383 $data = 'some data' . microtime();
384 $entryIdentifier = 'BackendDbRemovalTest';
385
386 $backend->set($entryIdentifier, $data);
387
388 $backend->remove($entryIdentifier);
389
390 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
391 '*',
392 $this->testingCacheTable,
393 'identifier = \'' . $entryIdentifier . '\''
394 );
395
396 $this->assertTrue(count($entriesFound) == 0);
397 }
398
399 /**
400 * @test
401 * @expectedException t3lib_cache_Exception
402 */
403 public function collectGarbageThrowsExceptionIfFrontendWasNotSet() {
404 $backend = $this->setUpBackend();
405 $backend->collectGarbage();
406 }
407
408 /**
409 * @test
410 * @author Ingo Renner <ingo@typo3.org>
411 */
412 public function collectGarbageReallyRemovesAnExpiredCacheEntry() {
413 $backend = $this->setUpBackend();
414 $mockCache = $this->setUpMockFrontendOfBackend($backend);
415
416 $data = 'some data' . microtime();
417 $entryIdentifier = 'BackendDbRemovalTest';
418
419 $backend->set($entryIdentifier, $data, array(), 1);
420
421 $GLOBALS['EXEC_TIME'] += 2;
422 // setCache calls initializeCommonReferences which recalculate expire statement
423 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
424 $backend->setCache($mockCache);
425
426 $backend->collectGarbage();
427
428 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
429 '*',
430 $this->testingCacheTable,
431 'identifier = \'' . $entryIdentifier . '\''
432 );
433
434 $this->assertTrue(count($entriesFound) == 0);
435 }
436
437 /**
438 * @test
439 * @author Ingo Renner <ingo@typo3.org>
440 */
441 public function collectGarbageReallyRemovesAllExpiredCacheEntries() {
442 $backend = $this->setUpBackend();
443 $mockCache = $this->setUpMockFrontendOfBackend($backend);
444
445 $data = 'some data' . microtime();
446 $entryIdentifier = 'BackendDbRemovalTest';
447
448 $backend->set($entryIdentifier . 'A', $data, array(), 1);
449 $backend->set($entryIdentifier . 'B', $data, array(), 1);
450 $backend->set($entryIdentifier . 'C', $data, array(), 1);
451
452 $GLOBALS['EXEC_TIME'] += 2;
453 // setCache calls initializeCommonReferences which recalculate expire statement
454 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
455 $backend->setCache($mockCache);
456
457 $backend->collectGarbage();
458
459 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
460 '*',
461 $this->testingCacheTable,
462 ''
463 );
464
465 $this->assertTrue(count($entriesFound) == 0);
466 }
467
468 /**
469 * @test
470 * @author Ingo Renner <ingo@typo3.org>
471 */
472 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
473 $backend = $this->setUpBackend();
474 $this->setUpMockFrontendOfBackend($backend);
475
476 $data = 'some data' . microtime();
477 $backend->set('BackendDbTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
478 $backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
479 $backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
480
481 $expectedEntry = 'BackendDbTest2';
482
483 $actualEntries = $backend->findIdentifiersByTag('UnitTestTag%special');
484 $this->assertTrue(is_array($actualEntries));
485 $this->assertEquals($expectedEntry, array_pop($actualEntries));
486 }
487
488 /**
489 * @test
490 * @expectedException t3lib_cache_Exception
491 */
492 public function flushThrowsExceptionIfFrontendWasNotSet() {
493 $backend = $this->setUpBackend();
494 $backend->flush();
495 }
496
497 /**
498 * @test
499 * @author Ingo Renner <ingo@typo3.org>
500 */
501 public function flushRemovesAllCacheEntries() {
502 $backend = $this->setUpBackend();
503 $this->setUpMockFrontendOfBackend($backend);
504
505 $data = 'some data' . microtime();
506 $backend->set('BackendDbTest1', $data, array('UnitTestTag%test'));
507 $backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
508 $backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
509
510 $backend->flush();
511
512 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
513 '*',
514 $this->testingCacheTable,
515 ''
516 );
517
518 $this->assertTrue(count($entriesFound) == 0);
519 }
520
521 /**
522 * @test
523 */
524 public function flushDropsDataTable() {
525 $backend = $this->setUpBackend();
526 $this->setUpMockFrontendOfBackend($backend);
527
528 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_TRUNCATEquery', 'INSERTquery', 'sql_query', 'admin_query'));
529 $GLOBALS['TYPO3_DB']->expects($this->at(2))
530 ->method('admin_query')
531 ->with('DROP TABLE IF EXISTS cachingframework_Testing');
532
533 $backend->flush();
534 }
535
536 /**
537 * @test
538 */
539 public function flushDropsTagsTable() {
540 $backend = $this->setUpBackend();
541 $this->setUpMockFrontendOfBackend($backend);
542
543 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_TRUNCATEquery', 'INSERTquery', 'sql_query', 'admin_query'));
544 $GLOBALS['TYPO3_DB']->expects($this->at(3))
545 ->method('admin_query')
546 ->with('DROP TABLE IF EXISTS cachingframework_Testing_tags');
547
548 $backend->flush();
549 }
550
551 /**
552 * @test
553 */
554 public function flushCreatesDataTable() {
555 $backend = $this->setUpBackend();
556 $this->setUpMockFrontendOfBackend($backend);
557
558 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_TRUNCATEquery', 'INSERTquery', 'sql_query', 'admin_query'));
559 $GLOBALS['TYPO3_DB']->expects($this->at(4))
560 ->method('admin_query')
561 ->will($this->returnCallback(array($this, flushCreatesDataTableCallback)));
562
563 $backend->flush();
564 }
565
566 /**
567 * Callback of flushCreatesDataTable to check if data table is created
568 *
569 * @param string $sql SQL of admin_query
570 * @return void
571 */
572 public function flushCreatesDataTableCallback($sql) {
573 $startOfStatement = 'CREATE TABLE cachingframework_Testing (';
574 $this->assertEquals($startOfStatement, substr($sql, 0, strlen($startOfStatement)));
575 }
576
577 /**
578 * @test
579 */
580 public function flushCreatesTagsTable() {
581 $backend = $this->setUpBackend();
582 $this->setUpMockFrontendOfBackend($backend);
583
584 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_TRUNCATEquery', 'INSERTquery', 'sql_query', 'admin_query'));
585 $GLOBALS['TYPO3_DB']->expects($this->at(5))
586 ->method('admin_query')
587 ->will($this->returnCallback(array($this, flushCreatesTagsTableCallback)));
588
589 $backend->flush();
590 }
591
592 /**
593 * Callback of flushCreatesTagsTable to check if tags table is created
594 *
595 * @param string $sql SQL of admin_query
596 * @return void
597 */
598 public function flushCreatesTagsTableCallback($sql) {
599 $startOfStatement = 'CREATE TABLE cachingframework_Testing_tags (';
600 $this->assertEquals($startOfStatement, substr($sql, 0, strlen($startOfStatement)));
601 }
602
603 /**
604 * @test
605 * @expectedException t3lib_cache_Exception
606 */
607 public function flushByTagThrowsExceptionIfFrontendWasNotSet() {
608 $backend = $this->setUpBackend();
609 $backend->flushByTag(array());
610 }
611
612 /**
613 * @test
614 * @author Ingo Renner <ingo@typo3.org>
615 */
616 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
617 $backend = $this->setUpBackend();
618 $this->setUpMockFrontendOfBackend($backend);
619
620 $data = 'some data' . microtime();
621 $backend->set('BackendDbTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
622 $backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
623 $backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
624
625 $backend->flushByTag('UnitTestTag%special');
626
627 $this->assertTrue($backend->has('BackendDbTest1'), 'BackendDbTest1 does not exist anymore.');
628 $this->assertFalse($backend->has('BackendDbTest2'), 'BackendDbTest2 still exists.');
629 $this->assertTrue($backend->has('BackendDbTest3'), 'BackendDbTest3 does not exist anymore.');
630
631 $tagEntriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
632 '*',
633 $this->testingTagsTable,
634 'tag = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr('UnitTestTag%special', $this->testingTagsTable)
635 );
636 $this->assertEquals(0, count($tagEntriesFound));
637 }
638
639
640 /**
641 * @test
642 * @author Ingo Renner <ingo@typo3.org>
643 */
644 public function hasReturnsTheCorrectResultForEntryWithExceededLifetime() {
645 $backend = $this->setUpBackend();
646 $mockCache = $this->setUpMockFrontendOfBackend($backend);
647
648 $data = 'some data' . microtime();
649 $entryIdentifier = 'BackendDbTest';
650
651 $backend->set($entryIdentifier, $data);
652
653 $expiredEntryIdentifier = 'ExpiredBackendDbTest';
654 $expiredData = 'some old data' . microtime();
655 $backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
656
657 $GLOBALS['EXEC_TIME'] += 2;
658 // setCache calls initializeCommonReferences which recalculate expire statement
659 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
660 $backend->setCache($mockCache);
661
662 $this->assertFalse($backend->has($expiredEntryIdentifier));
663 }
664
665 /**
666 * @test
667 * @author Christian Kuhn <lolli@schwarzbu.ch>
668 */
669 public function hasReturnsTrueForEntryWithUnlimitedLifetime() {
670 $backend = $this->setUpBackend();
671 $mockCache = $this->setUpMockFrontendOfBackend($backend);
672
673 $entryIdentifier = 'BackendDbTest';
674
675 $backend->set($entryIdentifier, 'data', array(), 0);
676
677 $GLOBALS['EXEC_TIME'] += 1;
678 // setCache calls initializeCommonReferences which recalculate expire statement
679 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
680 $backend->setCache($mockCache);
681
682 $this->assertTrue($backend->has($entryIdentifier));
683 }
684
685 /**
686 * @test
687 * @author Ingo Renner <ingo@typo3.org>
688 */
689 public function getReturnsFalseForEntryWithExceededLifetime() {
690 $backend = $this->setUpBackend();
691 $mockCache = $this->setUpMockFrontendOfBackend($backend);
692
693 $data = 'some data' . microtime();
694 $entryIdentifier = 'BackendDbTest';
695
696 $backend->set($entryIdentifier, $data);
697
698 $expiredEntryIdentifier = 'ExpiredBackendDbTest';
699 $expiredData = 'some old data' . microtime();
700 $backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
701
702 $GLOBALS['EXEC_TIME'] += 2;
703 // setCache calls initializeCommonReferences which recalculate expire statement
704 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
705 $backend->setCache($mockCache);
706
707 $this->assertEquals($data, $backend->get($entryIdentifier));
708 $this->assertFalse($backend->get($expiredEntryIdentifier));
709 }
710
711 /**
712 * @test
713 * @expectedException t3lib_cache_Exception
714 */
715 public function findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet() {
716 $backend = $this->setUpBackend();
717 $backend->findIdentifiersByTag('identifier');
718 }
719
720 /**
721 * @test
722 * @author Ingo Renner <ingo@typo3.org>
723 */
724 public function findIdentifiersByTagReturnsEmptyArrayForEntryWithExceededLifetime() {
725 $backend = $this->setUpBackend();
726 $mockCache = $this->setUpMockFrontendOfBackend($backend);
727
728 $backend->set('BackendDbTest', 'some data', array('UnitTestTag%special'), 1);
729
730 $GLOBALS['EXEC_TIME'] += 2;
731 // setCache calls initializeCommonReferences which recalculate expire statement
732 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
733 $backend->setCache($mockCache);
734
735 $this->assertEquals(array(), $backend->findIdentifiersByTag('UnitTestTag%special'));
736 }
737
738 /**
739 * @test
740 * @author Ingo Renner <ingo@typo3.org>
741 */
742 public function setWithUnlimitedLifetimeWritesCorrectEntry() {
743 $backend = $this->setUpBackend();
744 $this->setUpMockFrontendOfBackend($backend);
745
746 $data = 'some data' . microtime();
747 $entryIdentifier = 'BackendFileTest';
748
749 $backend->set($entryIdentifier, $data, array(), 0);
750
751 $entryFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
752 '*',
753 $this->testingCacheTable,
754 ''
755 );
756
757 $this->assertTrue(is_array($entryFound));
758
759 $retrievedData = $entryFound['content'];
760 $this->assertEquals($data, $retrievedData);
761 }
762 }
763
764 ?>