TYPO3 API  SVNRelease
t3lib_cache_frontend_abstractfrontendTest.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 abstract 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_abstractfrontendTest.php 10121 2011-01-18 20:15:30Z ohader $
00034  */
00035 class t3lib_cache_frontend_AbstractFrontendTest extends tx_phpunit_testcase {
00036 
00037     /**
00038      * @test
00039      * @author Robert Lemke <robert@typo3.org>
00040      * @author Ingo Renner <ingo@typo3.org>
00041      */
00042     public function theConstructorAcceptsValidIdentifiers() {
00043         $mockBackend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00044         foreach (array('x', 'someValue', '123fivesixseveneight', 'some&', 'ab_cd%', rawurlencode('resource://some/äöü$&% sadf'), str_repeat('x', 250)) as $identifier) {
00045             $abstractCache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'), array($identifier, $mockBackend));
00046         }
00047     }
00048 
00049     /**
00050      * @test
00051      * @author Robert Lemke <robert@typo3.org>
00052      * @author Ingo Renner <ingo@typo3.org>
00053      */
00054     public function theConstructorRejectsInvalidIdentifiers() {
00055         $mockBackend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00056         foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $identifier) {
00057             try {
00058                 $abstractCache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'), array($identifier, $mockBackend));
00059                 $this->fail('Identifier "' . $identifier . '" was not rejected.');
00060             } catch (InvalidArgumentException $exception) {
00061             }
00062         }
00063     }
00064 
00065     /**
00066      * @test
00067      * @author Karsten Dambekalns <karsten@typo3.org>
00068      * @author Ingo Renner <ingo@typo3.org>
00069      */
00070     public function flushCallsBackend() {
00071         $identifier = 'someCacheIdentifier';
00072         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00073         $backend->expects($this->once())->method('flush');
00074 
00075         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00076         $cache->flush();
00077     }
00078 
00079 
00080     /**
00081      * @test
00082      * @expectedException InvalidArgumentException
00083      * @author Robert Lemke <robert@typo3.org>
00084      * @author Ingo Renner <ingo@typo3.org>
00085      */
00086     public function flushByTagRejectsInvalidTags() {
00087         $identifier = 'someCacheIdentifier';
00088         $backend = $this->getMock('t3lib_cache_backend_Backend', array(), array(), '', FALSE);
00089         $backend->expects($this->never())->method('flushByTag');
00090 
00091         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00092         $cache->flushByTag('SomeInvalid\Tag');
00093     }
00094 
00095     /**
00096      * @test
00097      * @author Karsten Dambekalns <karsten@typo3.org>
00098      * @author Ingo Renner <ingo@typo3.org>
00099      */
00100     public function flushByTagCallsBackend() {
00101         $tag = 'sometag';
00102         $identifier = 'someCacheIdentifier';
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('flushByTag')->with($tag);
00105 
00106         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00107         $cache->flushByTag($tag);
00108     }
00109 
00110     /**
00111      * @test
00112      * @author Karsten Dambekalns <karsten@typo3.org>
00113      * @author Ingo Renner <ingo@typo3.org>
00114      */
00115     public function collectGarbageCallsBackend() {
00116         $identifier = 'someCacheIdentifier';
00117         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00118         $backend->expects($this->once())->method('collectGarbage');
00119 
00120         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00121         $cache->collectGarbage();
00122     }
00123 
00124     /**
00125      * @test
00126      * @author Robert Lemke <robert@typo3.org>
00127      * @author Ingo Renner <ingo@typo3.org>
00128      */
00129     public function getClassTagRendersTagWhichCanBeUsedToTagACacheEntryWithACertainClass() {
00130         $identifier = 'someCacheIdentifier';
00131         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
00132 
00133         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00134         $this->assertEquals('%CLASS%F3_Foo_Bar_Baz', $cache->getClassTag('F3\Foo\Bar\Baz'));
00135     }
00136 
00137     /**
00138      * @test
00139      * @author Robert Lemke <robert@typo3.org>
00140      * @author Ingo Renner <ingo@typo3.org>
00141      */
00142     public function invalidEntryIdentifiersAreRecognizedAsInvalid() {
00143         $identifier = 'someCacheIdentifier';
00144         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
00145         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00146         foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $entryIdentifier) {
00147             $this->assertFalse($cache->isValidEntryIdentifier($entryIdentifier), 'Invalid identifier "' . $entryIdentifier . '" was not rejected.');
00148         }
00149     }
00150 
00151     /**
00152      * @test
00153      * @author Robert Lemke <robert@typo3.org>
00154      * @author Ingo Renner <ingo@typo3.org>
00155      */
00156     public function validEntryIdentifiersAreRecognizedAsValid() {
00157         $identifier = 'someCacheIdentifier';
00158         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
00159         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00160         foreach (array('_', 'abcdef', 'foo', 'bar123', '3some', '_bl_a', 'some&', 'one%TWO', str_repeat('x', 250)) as $entryIdentifier) {
00161             $this->assertTrue($cache->isValidEntryIdentifier($entryIdentifier), 'Valid identifier "' . $entryIdentifier . '" was not accepted.');
00162         }
00163     }
00164 
00165     /**
00166      * @test
00167      * @author Robert Lemke <robert@typo3.org>
00168      * @author Ingo Renner <ingo@typo3.org>
00169      */
00170     public function invalidTagsAreRecognizedAsInvalid() {
00171         $identifier = 'someCacheIdentifier';
00172         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
00173         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00174         foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $tag) {
00175             $this->assertFalse($cache->isValidTag($tag), 'Invalid tag "' . $tag . '" was not rejected.');
00176         }
00177     }
00178 
00179     /**
00180      * @test
00181      * @author Robert Lemke <robert@typo3.org>
00182      * @author Ingo Renner <ingo@typo3.org>
00183      */
00184     public function validTagsAreRecognizedAsValid() {
00185         $identifier = 'someCacheIdentifier';
00186         $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
00187         $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
00188         foreach (array('abcdef', 'foo-bar', 'foo_baar', 'bar123', '3some', 'file%Thing', 'some&', '%x%', str_repeat('x', 250)) as $tag) {
00189             $this->assertTrue($cache->isValidTag($tag), 'Valid tag "' . $tag . '" was not accepted.');
00190         }
00191     }
00192 }
00193 
00194 ?>