|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2010-2011 Christian Kuhn <lolli@schwarzbu.ch> 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 PDO cache backend 00027 * 00028 * @author Christian Kuhn <lolli@schwarzbu.ch> 00029 * @package TYPO3 00030 * @subpackage tests 00031 * @version $Id$ 00032 */ 00033 class t3lib_cache_backend_PdoBackendTest extends tx_phpunit_testcase { 00034 00035 /** 00036 * Backup of global variable EXEC_TIME 00037 * 00038 * @var array 00039 */ 00040 protected $backupGlobalVariables; 00041 00042 /** 00043 * Sets up this testcase 00044 * 00045 * @author Christian Kuhn <lolli@schwarzbu.ch> 00046 */ 00047 public function setUp() { 00048 if (!extension_loaded('pdo_sqlite')) { 00049 $this->markTestSkipped('pdo_sqlite extension was not available'); 00050 } 00051 00052 $this->backupGlobalVariables = array( 00053 'EXEC_TIME' => $GLOBALS['EXEC_TIME'], 00054 ); 00055 } 00056 00057 /** 00058 * @test 00059 * @author Karsten Dambekalns <karsten@typo3.org> 00060 * @expectedException t3lib_cache_Exception 00061 */ 00062 public function setThrowsExceptionIfNoFrontEndHasBeenSet() { 00063 $backend = t3lib_div::makeInstance('t3lib_cache_backend_PdoBackend'); 00064 $data = 'Some data'; 00065 $identifier = 'MyIdentifier'; 00066 $backend->set($identifier, $data); 00067 } 00068 00069 /** 00070 * @test 00071 * @author Christian Jul Jensen <julle@typo3.org> 00072 */ 00073 public function itIsPossibleToSetAndCheckExistenceInCache() { 00074 $backend = $this->setUpBackend(); 00075 $data = 'Some data'; 00076 $identifier = 'MyIdentifier'; 00077 $backend->set($identifier, $data); 00078 $this->assertTrue($backend->has($identifier)); 00079 } 00080 00081 /** 00082 * @test 00083 * @author Christian Jul Jensen <julle@typo3.org> 00084 */ 00085 public function itIsPossibleToSetAndGetEntry() { 00086 $backend = $this->setUpBackend(); 00087 $data = 'Some data'; 00088 $identifier = 'MyIdentifier'; 00089 $backend->set($identifier, $data); 00090 $fetchedData = $backend->get($identifier); 00091 $this->assertEquals($data, $fetchedData); 00092 } 00093 00094 /** 00095 * @test 00096 * @author Christian Jul Jensen <julle@typo3.org> 00097 */ 00098 public function itIsPossibleToRemoveEntryFromCache() { 00099 $backend = $this->setUpBackend(); 00100 $data = 'Some data'; 00101 $identifier = 'MyIdentifier'; 00102 $backend->set($identifier, $data); 00103 $backend->remove($identifier); 00104 $this->assertFalse($backend->has($identifier)); 00105 } 00106 00107 /** 00108 * @test 00109 * @author Christian Jul Jensen <julle@typo3.org> 00110 */ 00111 public function itIsPossibleToOverwriteAnEntryInTheCache() { 00112 $backend = $this->setUpBackend(); 00113 $data = 'Some data'; 00114 $identifier = 'MyIdentifier'; 00115 $backend->set($identifier, $data); 00116 $otherData = 'some other data'; 00117 $backend->set($identifier, $otherData); 00118 $fetchedData = $backend->get($identifier); 00119 $this->assertEquals($otherData, $fetchedData); 00120 } 00121 00122 /** 00123 * @test 00124 * @author Karsten Dambekalns <karsten@typo3.org> 00125 */ 00126 public function findIdentifiersByTagFindsSetEntries() { 00127 $backend = $this->setUpBackend(); 00128 00129 $data = 'Some data'; 00130 $entryIdentifier = 'MyIdentifier'; 00131 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2')); 00132 00133 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1'); 00134 $this->assertEquals($entryIdentifier, $retrieved[0]); 00135 00136 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2'); 00137 $this->assertEquals($entryIdentifier, $retrieved[0]); 00138 } 00139 00140 /** 00141 * @test 00142 * @author Christian Kuhn <lolli@schwarzbu.ch> 00143 */ 00144 public function findIdentifiersByTagsFindsSetEntries() { 00145 $backend = $this->setUpBackend(); 00146 00147 $data = 'Some data'; 00148 $entryIdentifier = 'MyIdentifier'; 00149 $backend->set($entryIdentifier . 'A', $data, array('UnitTestTag%tag1')); 00150 $backend->set($entryIdentifier . 'B', $data, array('UnitTestTag%tag2')); 00151 $backend->set($entryIdentifier . 'C', $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2')); 00152 $backend->set($entryIdentifier . 'D', $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2', 'UnitTestTag%tag3')); 00153 00154 $retrieved = $backend->findIdentifiersByTags(array('UnitTestTag%tag1', 'UnitTestTag%tag2')); 00155 00156 $this->assertFalse(in_array($entryIdentifier . 'A', $retrieved)); 00157 $this->assertFalse(in_array($entryIdentifier . 'B', $retrieved)); 00158 $this->assertTrue(in_array($entryIdentifier . 'C', $retrieved)); 00159 $this->assertTrue(in_array($entryIdentifier . 'D', $retrieved)); 00160 } 00161 00162 /** 00163 * @test 00164 * @author Karsten Dambekalns <karsten@typo3.org> 00165 */ 00166 public function setRemovesTagsFromPreviousSet() { 00167 $backend = $this->setUpBackend(); 00168 00169 $data = 'Some data'; 00170 $entryIdentifier = 'MyIdentifier'; 00171 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2')); 00172 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag3')); 00173 00174 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2'); 00175 $this->assertEquals(array(), $retrieved); 00176 } 00177 00178 /** 00179 * @test 00180 * @author Christian Jul Jensen <julle@typo3.org> 00181 */ 00182 public function hasReturnsFalseIfTheEntryDoesntExist() { 00183 $backend = $this->setUpBackend(); 00184 $identifier = 'NonExistingIdentifier'; 00185 $this->assertFalse($backend->has($identifier)); 00186 } 00187 00188 /** 00189 * @test 00190 * @author Christian Jul Jensen <julle@typo3.org> 00191 */ 00192 public function removeReturnsFalseIfTheEntryDoesntExist() { 00193 $backend = $this->setUpBackend(); 00194 $identifier = 'NonExistingIdentifier'; 00195 $this->assertFalse($backend->remove($identifier)); 00196 } 00197 00198 /** 00199 * @test 00200 * @author Robert Lemke <robert@typo3.org> 00201 * @author Karsten Dambekalns <karsten@typo3.org> 00202 */ 00203 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() { 00204 $backend = $this->setUpBackend(); 00205 00206 $data = 'some data' . microtime(); 00207 $backend->set('PdoBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring')); 00208 $backend->set('PdoBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special')); 00209 $backend->set('PdoBackendTest3', $data, array('UnitTestTag%test')); 00210 00211 $backend->flushByTag('UnitTestTag%special'); 00212 00213 $this->assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1'); 00214 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2'); 00215 $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3'); 00216 } 00217 00218 /** 00219 * @test 00220 * @author Christian Kuhn <lolli@schwarzbu.ch> 00221 */ 00222 public function flushByTagsRemovesCacheEntriesWithSpecifiedTags() { 00223 $backend = $this->setUpBackend(); 00224 00225 $data = 'some data' . microtime(); 00226 $backend->set('PdoBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring')); 00227 $backend->set('PdoBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special1')); 00228 $backend->set('PdoBackendTest3', $data, array('UnitTestTag%test', 'UnitTestTag%special2')); 00229 $backend->set('PdoBackendTest4', $data, array('UnitTestTag%test', 'UnitTestTag%special2')); 00230 00231 $backend->flushByTags(array('UnitTestTag%special1','UnitTestTag%special2')); 00232 00233 $this->assertTrue($backend->has('PdoBackendTest1')); 00234 $this->assertFalse($backend->has('PdoBackendTest2')); 00235 $this->assertFalse($backend->has('PdoBackendTest3')); 00236 $this->assertFalse($backend->has('PdoBackendTest4')); 00237 } 00238 00239 /** 00240 * @test 00241 * @author Karsten Dambekalns <karsten@typo3.org> 00242 */ 00243 public function flushRemovesAllCacheEntries() { 00244 $backend = $this->setUpBackend(); 00245 00246 $data = 'some data' . microtime(); 00247 $backend->set('PdoBackendTest1', $data); 00248 $backend->set('PdoBackendTest2', $data); 00249 $backend->set('PdoBackendTest3', $data); 00250 00251 $backend->flush(); 00252 00253 $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1'); 00254 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2'); 00255 $this->assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3'); 00256 } 00257 00258 /** 00259 * @test 00260 * @author Karsten Dambekalns <karsten@typo3.org> 00261 */ 00262 public function flushRemovesOnlyOwnEntries() { 00263 $thisCache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE); 00264 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache')); 00265 $thisBackend = $this->setUpBackend(); 00266 $thisBackend->setCache($thisCache); 00267 00268 $thatCache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE); 00269 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache')); 00270 $thatBackend = $this->setUpBackend(); 00271 $thatBackend->setCache($thatCache); 00272 00273 $thisBackend->set('thisEntry', 'Hello'); 00274 $thatBackend->set('thatEntry', 'World!'); 00275 $thatBackend->flush(); 00276 00277 $this->assertEquals('Hello', $thisBackend->get('thisEntry')); 00278 $this->assertFalse($thatBackend->has('thatEntry')); 00279 } 00280 00281 /** 00282 * @test 00283 * @author Ingo Renner <ingo@typo3.org> 00284 * @author Christian Kuhn <lolli@schwarzbu.ch> 00285 */ 00286 public function collectGarbageReallyRemovesAnExpiredCacheEntry() { 00287 $backend = $this->setUpBackend(); 00288 00289 $data = 'some data' . microtime(); 00290 $entryIdentifier = 'BackendPDORemovalTest'; 00291 $backend->set($entryIdentifier, $data, array(), 1); 00292 00293 $this->assertTrue($backend->has($entryIdentifier)); 00294 00295 $GLOBALS['EXEC_TIME'] += 2; 00296 $backend->collectGarbage(); 00297 00298 $this->assertFalse($backend->has($entryIdentifier)); 00299 } 00300 00301 /** 00302 * @test 00303 * @author Ingo Renner <ingo@typo3.org> 00304 * @author Christian Kuhn <lolli@schwarzbu.ch> 00305 */ 00306 public function collectGarbageReallyRemovesAllExpiredCacheEntries() { 00307 $backend = $this->setUpBackend(); 00308 00309 $data = 'some data' . microtime(); 00310 $entryIdentifier = 'BackendPDORemovalTest'; 00311 00312 $backend->set($entryIdentifier . 'A', $data, array(), NULL); 00313 $backend->set($entryIdentifier . 'B', $data, array(), 10); 00314 $backend->set($entryIdentifier . 'C', $data, array(), 1); 00315 $backend->set($entryIdentifier . 'D', $data, array(), 1); 00316 00317 $this->assertTrue($backend->has($entryIdentifier . 'A')); 00318 $this->assertTrue($backend->has($entryIdentifier . 'B')); 00319 $this->assertTrue($backend->has($entryIdentifier . 'C')); 00320 $this->assertTrue($backend->has($entryIdentifier . 'D')); 00321 00322 $GLOBALS['EXEC_TIME'] += 2; 00323 $backend->collectGarbage(); 00324 00325 $this->assertTrue($backend->has($entryIdentifier . 'A')); 00326 $this->assertTrue($backend->has($entryIdentifier . 'B')); 00327 $this->assertFalse($backend->has($entryIdentifier . 'C')); 00328 $this->assertFalse($backend->has($entryIdentifier . 'D')); 00329 } 00330 00331 /** 00332 * Sets up the PDO backend used for testing 00333 * 00334 * @return t3lib_cache_backend_PdoBackend 00335 * @author Karsten Dambekalns <karsten@typo3.org> 00336 */ 00337 protected function setUpBackend() { 00338 $mockCache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE); 00339 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache')); 00340 00341 $backendOptions = array( 00342 'dataSourceName' => 'sqlite::memory:', 00343 'username' => '', 00344 'password' => '', 00345 ); 00346 $backend = t3lib_div::makeInstance('t3lib_cache_backend_PdoBackend', $backendOptions); 00347 $backend->setCache($mockCache); 00348 00349 return $backend; 00350 } 00351 00352 /** 00353 * Clean up after the tests 00354 * 00355 * @return void 00356 * @author Karsten Dambekalns <karsten@typo3.org> 00357 */ 00358 public function tearDown() { 00359 foreach ($this->backupGlobalVariables as $key => $data) { 00360 $GLOBALS[$key] = $data; 00361 } 00362 } 00363 } 00364 00365 ?>
1.8.0