|
TYPO3 API
SVNRelease
|
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 /** 00027 * Testcase for the variable cache frontend 00028 * 00029 * This file is a backport from FLOW3 00030 * 00031 * @author Ingo Renner <ingo@typo3.org> 00032 * @package TYPO3 00033 * @subpackage tests 00034 * @version $Id: t3lib_cache_frontend_variablefrontendTest.php 10121 2011-01-18 20:15:30Z ohader $ 00035 */ 00036 class t3lib_cache_frontend_VariableFrontendTest extends tx_phpunit_testcase { 00037 00038 /** 00039 * @expectedException InvalidArgumentException 00040 * @test 00041 * @author Robert Lemke <robert@typo3.org> 00042 * @author Ingo Renner <ingo@typo3.org> 00043 */ 00044 public function setChecksIfTheIdentifierIsValid() { 00045 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('isValidEntryIdentifier'), array(), '', FALSE); 00046 $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(FALSE)); 00047 $cache->set('foo', 'bar'); 00048 } 00049 00050 /** 00051 * @test 00052 * @author Robert Lemke <robert@typo3.org> 00053 * @author Ingo Renner <ingo@typo3.org> 00054 */ 00055 public function setPassesSerializedStringToBackend() { 00056 $theString = 'Just some value'; 00057 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00058 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString))); 00059 00060 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00061 $cache->set('VariableCacheTest', $theString); 00062 } 00063 00064 /** 00065 * @test 00066 * @author Robert Lemke <robert@typo3.org> 00067 * @author Ingo Renner <ingo@typo3.org> 00068 */ 00069 public function setPassesSerializedArrayToBackend() { 00070 $theArray = array('Just some value', 'and another one.'); 00071 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00072 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theArray))); 00073 00074 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00075 $cache->set('VariableCacheTest', $theArray); 00076 } 00077 00078 /** 00079 * @test 00080 * @author Karsten Dambekalns <karsten@typo3.org> 00081 * @author Ingo Renner <ingo@typo3.org> 00082 */ 00083 public function setPassesLifetimeToBackend() { 00084 $theString = 'Just some value'; 00085 $theLifetime = 1234; 00086 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00087 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)), $this->equalTo(array()), $this->equalTo($theLifetime)); 00088 00089 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00090 $cache->set('VariableCacheTest', $theString, array(), $theLifetime); 00091 } 00092 00093 /** 00094 * @test 00095 * @author Robert Lemke <robert@typo3.org> 00096 */ 00097 public function setUsesIgBinarySerializeIfAvailable() { 00098 if (!extension_loaded('igbinary')) { 00099 $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.'); 00100 } 00101 00102 $theString = 'Just some value'; 00103 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00104 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(igbinary_serialize($theString))); 00105 00106 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00107 $cache->set('VariableCacheTest', $theString); 00108 } 00109 00110 /** 00111 * @test 00112 * @author Robert Lemke <robert@typo3.org> 00113 * @author Ingo Renner <ingo@typo3.org> 00114 */ 00115 public function getFetchesStringValueFromBackend() { 00116 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00117 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize('Just some value'))); 00118 00119 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00120 $this->assertEquals('Just some value', $cache->get('VariableCacheTest'), 'The returned value was not the expected string.'); 00121 } 00122 00123 /** 00124 * @test 00125 * @author Robert Lemke <robert@typo3.org> 00126 * @author Ingo Renner <ingo@typo3.org> 00127 */ 00128 public function getFetchesArrayValueFromBackend() { 00129 $theArray = array('Just some value', 'and another one.'); 00130 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00131 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize($theArray))); 00132 00133 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00134 $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.'); 00135 } 00136 00137 /** 00138 * @test 00139 * @author Robert Lemke <robert@typo3.org> 00140 * @author Ingo Renner <ingo@typo3.org> 00141 */ 00142 public function getFetchesFalseBooleanValueFromBackend() { 00143 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00144 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize(FALSE))); 00145 00146 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00147 $this->assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.'); 00148 } 00149 00150 /** 00151 * @test 00152 * @author Robert Lemke <robert@typo3.org> 00153 */ 00154 public function getUsesIgBinaryIfAvailable() { 00155 if (!extension_loaded('igbinary')) { 00156 $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.'); 00157 } 00158 00159 $theArray = array('Just some value', 'and another one.'); 00160 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00161 $backend->expects($this->once())->method('get')->will($this->returnValue(igbinary_serialize($theArray))); 00162 00163 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00164 00165 $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.'); 00166 } 00167 00168 /** 00169 * @test 00170 * @author Robert Lemke <robert@typo3.org> 00171 * @author Ingo Renner <ingo@typo3.org> 00172 */ 00173 public function hasReturnsResultFromBackend() { 00174 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00175 $backend->expects($this->once())->method('has')->with($this->equalTo('VariableCacheTest'))->will($this->returnValue(TRUE)); 00176 00177 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00178 $this->assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.'); 00179 } 00180 00181 /** 00182 * @test 00183 * @author Sebastian Kurfürst <sebastian@typo3.org> 00184 * @author Ingo Renner <ingo@typo3.org> 00185 */ 00186 public function removeCallsBackend() { 00187 $cacheIdentifier = 'someCacheIdentifier'; 00188 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00189 00190 $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(TRUE)); 00191 00192 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00193 $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE'); 00194 } 00195 00196 /** 00197 * @test 00198 * @expectedException InvalidArgumentException 00199 * @author Karsten Dambekalns <karsten@typo3.org> 00200 * @author Ingo Renner <ingo@typo3.org> 00201 */ 00202 public function getByTagRejectsInvalidTags() { 00203 $backend = $this->getMock('t3lib_cache_backend_Backend', array(), array(), '', FALSE); 00204 $backend->expects($this->never())->method('getByTag'); 00205 00206 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00207 $cache->getByTag('SomeInvalid\Tag'); 00208 } 00209 00210 /** 00211 * @test 00212 * @author Karsten Dambekalns <karsten@typo3.org> 00213 * @author Ingo Renner <ingo@typo3.org> 00214 */ 00215 public function getByTagCallsBackend() { 00216 $tag = 'sometag'; 00217 $identifiers = array('one', 'two'); 00218 $entries = array('one value', 'two value'); 00219 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00220 00221 $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers)); 00222 $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(serialize('one value'), serialize('two value'))); 00223 00224 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00225 $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries'); 00226 } 00227 00228 /** 00229 * @test 00230 * @author Robert Lemke <robert@typo3.org> 00231 */ 00232 public function getByTagUsesIgBinaryIfAvailable() { 00233 if (!extension_loaded('igbinary')) { 00234 $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.'); 00235 } 00236 00237 $tag = 'sometag'; 00238 $identifiers = array('one', 'two'); 00239 $entries = array('one value', 'two value'); 00240 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE); 00241 00242 $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers)); 00243 $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(igbinary_serialize('one value'), igbinary_serialize('two value'))); 00244 00245 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend); 00246 $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries'); 00247 } 00248 } 00249 00250 ?>
1.8.0