TYPO3 API  SVNRelease
t3lib_cache_backend_dbbackendTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2011 Ingo Renner <ingo@typo3.org>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 /**
00026  * Testcase for the DB cache backend
00027  *
00028  * @author  Ingo Renner <ingo@typo3.org>
00029  * @package TYPO3
00030  * @subpackage tests
00031  * @version $Id: t3lib_cache_backend_dbbackendTest.php 10128 2011-01-18 21:56:30Z lolli $
00032  */
00033 class t3lib_cache_backend_DbBackendTest extends tx_phpunit_testcase {
00034 
00035     /**
00036      * If set, the tearDown() method will clean up the cache table used by this unit test.
00037      *
00038      * @var t3lib_cache_backend_DbBackend
00039      */
00040     protected $backend;
00041 
00042     protected $testingCacheTable;
00043     protected $testingTagsTable;
00044 
00045     /**
00046      * Sets up the backend used for testing
00047      *
00048      * @return void
00049      * @author Ingo Renner <ingo@typo3.org>
00050      * @author Christian Kuhn <lolli@schwarzbu.ch>
00051      */
00052     public function setUpBackend(array $backendOptions = array()) {
00053         $defaultTestingCacheTable = 'test_cache_dbbackend';
00054         $defaultTestingTagsTable = 'test_cache_dbbackend_tags';
00055 
00056         $backendOptions = array_merge(
00057             array(
00058                 'cacheTable' => $defaultTestingCacheTable,
00059                 'tagsTable' => $defaultTestingTagsTable,
00060             ),
00061             $backendOptions
00062         );
00063 
00064         $this->testingCacheTable = $backendOptions['cacheTable'];
00065         $this->testingTagsTable = $backendOptions['tagsTable'];
00066 
00067         $GLOBALS['TYPO3_DB']->sql_query('CREATE TABLE ' . $this->testingCacheTable . ' (
00068             id int(11) unsigned NOT NULL auto_increment,
00069             identifier varchar(128) DEFAULT \'\' NOT NULL,
00070             crdate int(11) unsigned DEFAULT \'0\' NOT NULL,
00071             content mediumblob,
00072             lifetime int(11) unsigned DEFAULT \'0\' NOT NULL,
00073             PRIMARY KEY (id),
00074             KEY cache_id (identifier)
00075         ) ENGINE=InnoDB;
00076         ');
00077 
00078         $GLOBALS['TYPO3_DB']->sql_query('CREATE TABLE ' . $this->testingTagsTable . ' (
00079             id int(11) unsigned NOT NULL auto_increment,
00080             identifier varchar(128) DEFAULT \'\' NOT NULL,
00081             tag varchar(128) DEFAULT \'\' NOT NULL,
00082             PRIMARY KEY (id),
00083             KEY cache_id (identifier),
00084             KEY cache_tag (tag)
00085         ) ENGINE=InnoDB;
00086         ');
00087 
00088         $this->backend = t3lib_div::makeInstance(
00089             't3lib_cache_backend_DbBackend',
00090             $backendOptions
00091         );
00092     }
00093 
00094     /**
00095      * @author Ingo Renner <ingo@typo3.org>
00096      */
00097     public function tearDown() {
00098         $GLOBALS['TYPO3_DB']->sql_query(
00099             'DROP TABLE ' . $this->testingCacheTable . ';'
00100         );
00101 
00102         $GLOBALS['TYPO3_DB']->sql_query(
00103             'DROP TABLE ' . $this->testingTagsTable . ';'
00104         );
00105     }
00106 
00107     /**
00108      * @test
00109      * @author Ingo Renner <ingo@typo3.org>
00110      */
00111     public function getCacheTableReturnsThePreviouslySetTable() {
00112         $this->setUpBackend();
00113         $this->backend->setCacheTable($this->testingCacheTable);
00114         $this->assertEquals($this->testingCacheTable, $this->backend->getCacheTable(), 'getCacheTable() did not return the expected value.');
00115     }
00116 
00117     /**
00118      * @test
00119      * @expectedException t3lib_cache_exception_InvalidData
00120      * @author Ingo Renner <ingo@typo3.org>
00121      */
00122     public function setThrowsExceptionIfDataIsNotAString() {
00123         $this->setUpBackend();
00124         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00125             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00126             array(),
00127             '',
00128             FALSE
00129         );
00130 
00131         $data = array('Some data');
00132         $entryIdentifier = 'BackendDbTest';
00133 
00134         $this->backend->setCache($cache);
00135 
00136         $this->backend->set($entryIdentifier, $data);
00137     }
00138 
00139     /**
00140      * @test
00141      * @author Ingo Renner <ingo@typo3.org>
00142      */
00143     public function setReallySavesToTheSpecifiedTable() {
00144         $this->setUpBackend();
00145         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00146             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00147             array(),
00148             '',
00149             FALSE
00150         );
00151 
00152         $data            = 'some data' . microtime();
00153         $entryIdentifier = 'BackendDbTest';
00154 
00155         $this->backend->setCache($cache);
00156         $this->backend->set($entryIdentifier, $data);
00157 
00158         $entryFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
00159             '*',
00160             $this->testingCacheTable,
00161             'identifier = \'' . $entryIdentifier . '\''
00162         );
00163 
00164         $this->assertEquals(
00165             $data,
00166             $entryFound['content'],
00167             'The original and the retrieved data don\'t match.'
00168         );
00169     }
00170 
00171     /**
00172      * @test
00173      * @author Ingo Renner <ingo@typo3.org>
00174      */
00175     public function setRemovesAnAlreadyExistingCacheEntryForTheSameIdentifier() {
00176         $this->setUpBackend();
00177         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00178             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00179             array(),
00180             '',
00181             FALSE
00182         );
00183 
00184         $data1 = 'some data' . microtime();
00185         $data2 = 'some data' . microtime();
00186         $entryIdentifier = 'BackendDbRemoveBeforeSetTest';
00187 
00188         $this->backend->setCache($cache);
00189         $this->backend->set($entryIdentifier, $data1, array(), 500);
00190             // setting a second entry with the same identifier, but different
00191             // data, this should _replace_ the existing one we set before
00192         $this->backend->set($entryIdentifier, $data2, array(), 200);
00193 
00194         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00195             '*',
00196             $this->testingCacheTable,
00197             'identifier = \'' . $entryIdentifier . '\''
00198         );
00199 
00200         $this->assertEquals(1, count($entriesFound), 'There was not exactly one cache entry.');
00201     }
00202 
00203     /**
00204      * @test
00205      * @author Ingo Renner <ingo@typo3.org>
00206      */
00207     public function setReallySavesSpecifiedTags() {
00208         $this->setUpBackend();
00209         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00210             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00211             array(),
00212             '',
00213             FALSE
00214         );
00215 
00216         $data = 'some data' . microtime();
00217         $entryIdentifier = 'BackendDbTest';
00218 
00219         $this->backend->setCache($cache);
00220         $this->backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
00221 
00222         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00223             '*',
00224             $this->testingTagsTable,
00225             'identifier = \'' . $entryIdentifier . '\''
00226         );
00227 
00228         $tags = array();
00229 
00230         foreach ($entriesFound as $entry) {
00231             $tags[] = $entry['tag'];
00232         }
00233 
00234         $this->assertTrue(count($tags) > 0, 'The tags do not exist.');
00235         $this->assertTrue(in_array('UnitTestTag%tag1', $tags), 'Tag UnitTestTag%tag1 does not exist.');
00236         $this->assertTrue(in_array('UnitTestTag%tag2', $tags), 'Tag UnitTestTag%tag2 does not exist.');
00237     }
00238 
00239     /**
00240      * @test
00241      * @author Christian Kuhn <lolli@schwarzbu.ch>
00242      */
00243     public function setSavesCompressedDataWithEnabledCompression() {
00244         $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array(), array(), '', FALSE);
00245         $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
00246         $this->setUpBackend(
00247             array(
00248                 'compression' => TRUE,
00249             )
00250         );
00251         $this->backend->setCache($mockCache);
00252 
00253         $data = 'some data ' . microtime();
00254 
00255         $entryIdentifier = 'BackendDbTest';
00256 
00257         $this->backend->set($entryIdentifier, $data);
00258 
00259         $entry = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
00260             'content',
00261             $this->testingCacheTable,
00262             'identifier = \'' . $entryIdentifier . '\''
00263         );
00264 
00265         $this->assertEquals($data, @gzuncompress($entry['content']), 'Original and compressed data don\'t match');
00266     }
00267 
00268     /**
00269      * @test
00270      * @author Christian Kuhn <lolli@schwarzbu.ch>
00271      */
00272     public function setSavesPlaintextDataWithEnabledCompressionAndCompressionLevel0() {
00273         $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array(), array(), '', FALSE);
00274         $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
00275         $this->setUpBackend(
00276             array(
00277                 'compression' => TRUE,
00278                 'compressionLevel' => 0,
00279             )
00280         );
00281         $this->backend->setCache($mockCache);
00282 
00283         $data = 'some data ' . microtime();
00284 
00285         $entryIdentifier = 'BackendDbTest';
00286 
00287         $this->backend->set($entryIdentifier, $data);
00288 
00289         $entry = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
00290             'content',
00291             $this->testingCacheTable,
00292             'identifier = \'' . $entryIdentifier . '\''
00293         );
00294 
00295         $this->assertGreaterThan(0, substr_count($entry['content'], $data), 'Plaintext data not found');
00296     }
00297 
00298     /**
00299      * @test
00300      * @author Ingo Renner <ingo@typo3.org>
00301      */
00302     public function getReturnsContentOfTheCorrectCacheEntry() {
00303         $this->setUpBackend();
00304         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00305             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00306             array(),
00307             '',
00308             FALSE
00309         );
00310 
00311         $data = 'some data' . microtime();
00312         $entryIdentifier = 'BackendDbTest';
00313 
00314         $this->backend->setCache($cache);
00315         $this->backend->set($entryIdentifier, $data, array(), 500);
00316 
00317         $data = 'some other data' . microtime();
00318         $this->backend->set($entryIdentifier, $data, array(), 100);
00319 
00320         $loadedData = $this->backend->get($entryIdentifier);
00321 
00322         $this->assertEquals($data, $loadedData, 'The original and the retrieved data don\'t match.');
00323     }
00324 
00325     /**
00326      * @test
00327      * @author Ingo Renner <ingo@typo3.org>
00328      */
00329     public function hasReturnsTheCorrectResult() {
00330         $this->setUpBackend();
00331         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00332             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00333             array(),
00334             '',
00335             FALSE
00336         );
00337 
00338         $data = 'some data' . microtime();
00339         $entryIdentifier = 'BackendDbTest';
00340 
00341         $this->backend->setCache($cache);
00342         $this->backend->set($entryIdentifier, $data);
00343 
00344         $this->assertTrue($this->backend->has($entryIdentifier), 'has() did not return TRUE.');
00345         $this->assertFalse($this->backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
00346     }
00347 
00348     /**
00349      * @test
00350      * @author Ingo Renner <ingo@typo3.org>
00351      */
00352     public function removeReallyRemovesACacheEntry() {
00353         $this->setUpBackend();
00354         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00355             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00356             array(),
00357             '',
00358             FALSE
00359         );
00360 
00361         $data = 'some data' . microtime();
00362         $entryIdentifier = 'BackendDbRemovalTest';
00363 
00364         $this->backend->setCache($cache);
00365         $this->backend->set($entryIdentifier, $data);
00366 
00367         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00368             '*',
00369             $this->testingCacheTable,
00370             'identifier = \'' . $entryIdentifier . '\''
00371         );
00372 
00373         $this->assertTrue(is_array($entriesFound) && count($entriesFound) > 0, 'The cache entry does not exist.');
00374 
00375         $this->backend->remove($entryIdentifier);
00376 
00377         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00378             '*',
00379             $this->testingCacheTable,
00380             'identifier = \'' . $entryIdentifier . '\''
00381         );
00382 
00383         $this->assertTrue(count($entriesFound) == 0, 'The cache entry still exists.');
00384     }
00385 
00386     /**
00387      * @test
00388      * @author Ingo Renner <ingo@typo3.org>
00389      */
00390     public function collectGarbageReallyRemovesAnExpiredCacheEntry() {
00391         $this->setUpBackend();
00392         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00393             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00394             array(),
00395             '',
00396             FALSE
00397         );
00398 
00399         $data = 'some data' . microtime();
00400         $entryIdentifier = 'BackendDbRemovalTest';
00401 
00402         $this->backend->setCache($cache);
00403         $this->backend->set($entryIdentifier, $data, array(), 1);
00404 
00405         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00406             '*',
00407             $this->testingCacheTable,
00408             'identifier = \'' . $entryIdentifier . '\''
00409         );
00410 
00411         $this->assertTrue(is_array($entriesFound) && count($entriesFound) > 0, 'The cache entry does not exist.');
00412 
00413         sleep(2);
00414         $GLOBALS['EXEC_TIME'] += 2;
00415         $this->backend->collectGarbage();
00416 
00417         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00418             '*',
00419             $this->testingCacheTable,
00420             'identifier = \'' . $entryIdentifier . '\''
00421         );
00422 
00423         $this->assertTrue(count($entriesFound) == 0, 'The cache entry still exists.');
00424     }
00425 
00426     /**
00427      * @test
00428      * @author Ingo Renner <ingo@typo3.org>
00429      */
00430     public function collectGarbageReallyRemovesAllExpiredCacheEntries() {
00431         $this->setUpBackend();
00432         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00433             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00434             array(),
00435             '',
00436             FALSE
00437         );
00438 
00439         $data = 'some data' . microtime();
00440         $entryIdentifier = 'BackendDbRemovalTest';
00441 
00442         $this->backend->setCache($cache);
00443 
00444         $this->backend->set($entryIdentifier . 'A', $data, array(), 1);
00445         $this->backend->set($entryIdentifier . 'B', $data, array(), 1);
00446         $this->backend->set($entryIdentifier . 'C', $data, array(), 1);
00447 
00448         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00449             '*',
00450             $this->testingCacheTable,
00451             ''
00452         );
00453 
00454         $this->assertTrue(is_array($entriesFound) && count($entriesFound) > 0, 'The cache entries do not exist.');
00455 
00456         sleep(2);
00457         $GLOBALS['EXEC_TIME'] += 2;
00458         $this->backend->collectGarbage();
00459 
00460         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00461             '*',
00462             $this->testingCacheTable,
00463             ''
00464         );
00465 
00466         $this->assertTrue(count($entriesFound) == 0, 'The cache entries still exist.');
00467     }
00468 
00469     /**
00470      * @test
00471      * @author Ingo Renner <ingo@typo3.org>
00472      */
00473     public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
00474         $this->setUpBackend();
00475         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00476             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00477             array(),
00478             '',
00479             FALSE
00480         );
00481 
00482         $this->backend->setCache($cache);
00483 
00484         $data = 'some data' . microtime();
00485         $this->backend->set('BackendDbTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
00486         $this->backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
00487         $this->backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
00488 
00489         $expectedEntry = 'BackendDbTest2';
00490 
00491         $actualEntries = $this->backend->findIdentifiersByTag('UnitTestTag%special');
00492         $this->assertTrue(is_array($actualEntries), 'actualEntries is not an array.');
00493 
00494         $this->assertEquals($expectedEntry, array_pop($actualEntries));
00495     }
00496 
00497     /**
00498      * @test
00499      * @author Ingo Renner <ingo@typo3.org>
00500      */
00501     public function flushRemovesAllCacheEntries() {
00502         $this->setUpBackend();
00503         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00504             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00505             array(),
00506             '',
00507             FALSE
00508         );
00509 
00510         $this->backend->setCache($cache);
00511 
00512         $data = 'some data' . microtime();
00513         $this->backend->set('BackendDbTest1', $data, array('UnitTestTag%test'));
00514         $this->backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
00515         $this->backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
00516 
00517         $this->backend->flush();
00518 
00519         $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00520             '*',
00521             $this->testingCacheTable,
00522             ''
00523         );
00524 
00525         $this->assertTrue(count($entriesFound) == 0, 'Still entries in the cache table');
00526     }
00527 
00528     /**
00529      * @test
00530      * @author Ingo Renner <ingo@typo3.org>
00531      */
00532     public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
00533         $this->setUpBackend();
00534         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00535             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00536             array(),
00537             '',
00538             FALSE
00539         );
00540 
00541         $this->backend->setCache($cache);
00542 
00543         $data = 'some data' . microtime();
00544         $this->backend->set('BackendDbTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
00545         $this->backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
00546         $this->backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
00547 
00548         $this->backend->flushByTag('UnitTestTag%special');
00549 
00550         $this->assertTrue($this->backend->has('BackendDbTest1'), 'BackendDbTest1 does not exist anymore.');
00551         $this->assertFalse($this->backend->has('BackendDbTest2'), 'BackendDbTest2 still exists.');
00552         $this->assertTrue($this->backend->has('BackendDbTest3'), 'BackendDbTest3 does not exist anymore.');
00553 
00554         $tagEntriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00555             '*',
00556             $this->testingTagsTable,
00557             'tag = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr('UnitTestTag%special', $this->testingTagsTable)
00558         );
00559         $this->assertEquals(0, count($tagEntriesFound), 'UnitTestTag%special still exists in tags table');
00560     }
00561 
00562 
00563     /**
00564      * @test
00565      * @author Ingo Renner <ingo@typo3.org>
00566      */
00567     public function hasReturnsTheCorrectResultForEntryWithExceededLifetime() {
00568         $this->setUpBackend();
00569         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00570             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00571             array(),
00572             '',
00573             FALSE
00574         );
00575 
00576         $data = 'some data' . microtime();
00577         $entryIdentifier = 'BackendDbTest';
00578 
00579         $this->backend->setCache($cache);
00580         $this->backend->set($entryIdentifier, $data);
00581 
00582         $expiredEntryIdentifier = 'ExpiredBackendDbTest';
00583         $expiredData = 'some old data' . microtime();
00584         $this->backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
00585 
00586         sleep(2);
00587         $GLOBALS['EXEC_TIME'] += 2;
00588 
00589         $this->assertFalse($this->backend->has($expiredEntryIdentifier), 'has() did not return FALSE.');
00590     }
00591 
00592     /**
00593      * @test
00594      * @author Christian Kuhn <lolli@schwarzbu.ch>
00595      */
00596     public function hasReturnsTrueForEntryWithUnlimitedLifetime() {
00597         $this->setUpBackend();
00598         $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array(), array(), '', FALSE);
00599         $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('UnitTestCache'));
00600 
00601         $entryIdentifier = 'BackendDbTest';
00602 
00603         $this->backend->setCache($mockCache);
00604         $this->backend->set($entryIdentifier, 'data', array(), 0);
00605 
00606         $GLOBALS['EXEC_TIME'] += 1;
00607         $this->assertTrue($this->backend->has($entryIdentifier));
00608     }
00609 
00610     /**
00611      * @test
00612      * @author Ingo Renner <ingo@typo3.org>
00613      */
00614     public function getReturnsFalseForEntryWithExceededLifetime() {
00615         $this->setUpBackend();
00616         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00617             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00618             array(),
00619             '',
00620             FALSE
00621         );
00622 
00623         $data = 'some data' . microtime();
00624         $entryIdentifier = 'BackendDbTest';
00625 
00626         $this->backend->setCache($cache);
00627         $this->backend->set($entryIdentifier, $data);
00628 
00629         $expiredEntryIdentifier = 'ExpiredBackendDbTest';
00630         $expiredData = 'some old data' . microtime();
00631         $this->backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
00632 
00633         sleep(2);
00634         $GLOBALS['EXEC_TIME'] += 2;
00635 
00636         $this->assertEquals($data, $this->backend->get($entryIdentifier), 'The original and the retrieved data don\'t match.');
00637         $this->assertFalse($this->backend->get($expiredEntryIdentifier), 'The expired entry could be loaded.');
00638     }
00639 
00640     /**
00641      * @test
00642      * @author Ingo Renner <ingo@typo3.org>
00643      */
00644     public function findIdentifiersByTagReturnsEmptyArrayForEntryWithExceededLifetime() {
00645         $this->setUpBackend();
00646         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00647             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00648             array(),
00649             '',
00650             FALSE
00651         );
00652 
00653         $this->backend->setCache($cache);
00654         $this->backend->set('BackendDbTest', 'some data', array('UnitTestTag%special'), 1);
00655 
00656         sleep(2);
00657         $GLOBALS['EXEC_TIME'] += 2;
00658             // Not required, but used to update the pre-calculated queries:
00659         $this->backend->setTagsTable($this->testingTagsTable);
00660 
00661         $this->assertEquals(array(), $this->backend->findIdentifiersByTag('UnitTestTag%special'));
00662     }
00663 
00664     /**
00665      * @test
00666      * @author Ingo Renner <ingo@typo3.org>
00667      */
00668     public function setWithUnlimitedLifetimeWritesCorrectEntry() {
00669         $this->setUpBackend();
00670         $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
00671             array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
00672             array(),
00673             '',
00674             FALSE
00675         );
00676 
00677         $data = 'some data' . microtime();
00678         $entryIdentifier = 'BackendFileTest';
00679 
00680         $this->backend->setCache($cache);
00681         $this->backend->set($entryIdentifier, $data, array(), 0);
00682 
00683         $entryFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
00684             '*',
00685             $this->testingCacheTable,
00686             ''
00687         );
00688 
00689         $this->assertTrue(is_array($entryFound), 'entriesFound is not an array.');
00690 
00691         $retrievedData = $entryFound['content'];
00692         $this->assertEquals($data, $retrievedData, 'The original and the retrieved data don\'t match.');
00693     }
00694 }
00695 
00696 ?>