TYPO3 API  SVNRelease
t3lib_cache_frontend_stringfrontendTest.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 string cache frontend
00027  *
00028  * This file is a backport from FLOW3
00029  *
00030  * @author  Ingo Renner <ingo@typo3.org>
00031  * @package TYPO3
00032  * @subpackage tests
00033  * @version $Id: t3lib_cache_frontend_stringfrontendTest.php 10121 2011-01-18 20:15:30Z ohader $
00034  */
00035 class t3lib_cache_frontend_StringFrontendTest extends tx_phpunit_testcase {
00036 
00037     /**
00038      * @expectedException InvalidArgumentException
00039      * @test
00040      * @author Robert Lemke <robert@typo3.org>
00041      * @author Ingo Renner <ingo@typo3.org>
00042      */
00043     public function setChecksIfTheIdentifierIsValid() {
00044         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('isValidEntryIdentifier'), array(), '', FALSE);
00045         $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(FALSE));
00046         $cache->set('foo', 'bar');
00047     }
00048 
00049     /**
00050      * @test
00051      * @author Karsten Dambekalns <karsten@typo3.org>
00052      * @author Ingo Renner <ingo@typo3.org>
00053      */
00054     public function setPassesStringToBackend() {
00055         $theString = 'Just some value';
00056         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00057         $backend->expects($this->once())->method('set')->with($this->equalTo('StringCacheTest'), $this->equalTo($theString));
00058 
00059         $cache = new t3lib_cache_frontend_StringFrontend('StringFrontend', $backend);
00060         $cache->set('StringCacheTest', $theString);
00061     }
00062 
00063     /**
00064      * @test
00065      * @author Karsten Dambekalns <karsten@typo3.org>
00066      * @author Ingo Renner <ingo@typo3.org>
00067      */
00068     public function setPassesLifetimeToBackend() {
00069         $theString = 'Just some value';
00070         $theLifetime = 1234;
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('StringCacheTest'), $this->equalTo($theString), $this->equalTo(array()), $this->equalTo($theLifetime));
00073 
00074         $cache = new t3lib_cache_frontend_StringFrontend('StringFrontend', $backend);
00075         $cache->set('StringCacheTest', $theString, array(), $theLifetime);
00076     }
00077 
00078     /**
00079      * @test
00080      * @author Karsten Dambekalns <karsten@typo3.org>
00081      * @author Ingo Renner <ingo@typo3.org>
00082      * @expectedException t3lib_cache_exception_InvalidData
00083      */
00084     public function setThrowsInvalidDataExceptionOnNonStringValues() {
00085         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00086 
00087         $cache = new t3lib_cache_frontend_StringFrontend('StringFrontend', $backend);
00088         $cache->set('StringCacheTest', array());
00089     }
00090 
00091     /**
00092      * @test
00093      * @author Robert Lemke <robert@typo3.org>
00094      * @author Karsten Dambekalns <karsten@typo3.org>
00095      * @author Ingo Renner <ingo@typo3.org>
00096      */
00097     public function getFetchesStringValueFromBackend() {
00098         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00099         $backend->expects($this->once())->method('get')->will($this->returnValue('Just some value'));
00100 
00101         $cache = new t3lib_cache_frontend_StringFrontend('StringFrontend', $backend);
00102         $this->assertEquals('Just some value', $cache->get('StringCacheTest'), 'The returned value was not the expected string.');
00103     }
00104 
00105     /**
00106      * @test
00107      * @author Robert Lemke <robert@typo3.org>
00108      * @author Ingo Renner <ingo@typo3.org>
00109      */
00110     public function hasReturnsResultFromBackend() {
00111         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00112         $backend->expects($this->once())->method('has')->with($this->equalTo('StringCacheTest'))->will($this->returnValue(TRUE));
00113 
00114         $cache = new t3lib_cache_frontend_StringFrontend('StringFrontend', $backend);
00115         $this->assertTrue($cache->has('StringCacheTest'), 'has() did not return TRUE.');
00116     }
00117 
00118     /**
00119      * @test
00120      * @author Sebastian Kurfürst <sebastian@typo3.org>
00121      * @author Ingo Renner <ingo@typo3.org>
00122      */
00123     public function removeCallsBackend() {
00124         $cacheIdentifier = 'someCacheIdentifier';
00125         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00126 
00127         $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(TRUE));
00128 
00129         $cache = new t3lib_cache_frontend_StringFrontend('StringFrontend', $backend);
00130         $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
00131     }
00132 
00133     /**
00134      * @test
00135      * @expectedException InvalidArgumentException
00136      * @author Karsten Dambekalns <karsten@typo3.org>
00137      * @author Ingo Renner <ingo@typo3.org>
00138      */
00139     public function getByTagRejectsInvalidTags() {
00140         $backend = $this->getMock('t3lib_cache_backend_Backend', array(), array(), '', FALSE);
00141         $backend->expects($this->never())->method('getByTag');
00142 
00143         $cache = new t3lib_cache_frontend_StringFrontend('StringFrontend', $backend);
00144         $cache->getByTag('SomeInvalid\Tag');
00145     }
00146 
00147     /**
00148      * @test
00149      * @author Karsten Dambekalns <karsten@typo3.org>
00150      * @author Ingo Renner <ingo@typo3.org>
00151      */
00152     public function getByTagCallsBackend() {
00153         $tag = 'sometag';
00154         $identifiers = array('one', 'two');
00155         $entries = array('one value', 'two value');
00156         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00157 
00158         $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
00159         $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls('one value', 'two value'));
00160 
00161         $cache = new t3lib_cache_frontend_StringFrontend('StringFrontend', $backend);
00162         $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
00163     }
00164 }
00165 
00166 ?>