TYPO3 API  SVNRelease
t3lib_cache_backend_apcbackendTest.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 APC cache backend
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_backend_apcbackendTest.php 10121 2011-01-18 20:15:30Z ohader $
00034  */
00035 class t3lib_cache_backend_ApcBackendTest extends tx_phpunit_testcase {
00036 
00037     /**
00038      * Sets up this testcase
00039      *
00040      * @return void
00041      * @author Karsten Dambekalns <karsten@typo3.org>
00042      * @author Ingo Renner <ingo@typo3.org>
00043      */
00044     public function setUp() {
00045         if (!extension_loaded('apc')) {
00046             $this->markTestSkipped('APC extension was not available');
00047         }
00048 
00049         if (ini_get('apc.slam_defense') == 1) {
00050             $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = Off');
00051         }
00052     }
00053 
00054     /**
00055      * @test
00056      * @author Christian Jul Jensen <julle@typo3.org>
00057      * @author Karsten Dambekalns <karsten@typo3.org>
00058      * @expectedException t3lib_cache_Exception
00059      */
00060     public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
00061         $backend = new t3lib_cache_backend_ApcBackend();
00062         $data = 'Some data';
00063         $identifier = uniqid('MyIdentifier');
00064         $backend->set($identifier, $data);
00065     }
00066 
00067     /**
00068      * @test
00069      * @author Christian Jul Jensen <julle@typo3.org>
00070      */
00071     public function itIsPossibleToSetAndCheckExistenceInCache() {
00072         $backend = $this->setUpBackend();
00073         $data = 'Some data';
00074         $identifier = uniqid('MyIdentifier');
00075         $backend->set($identifier, $data);
00076         $inCache = $backend->has($identifier);
00077         $this->assertTrue($inCache, 'APC backend failed to set and check entry');
00078     }
00079 
00080     /**
00081      * @test
00082      * @author Christian Jul Jensen <julle@typo3.org>
00083      */
00084     public function itIsPossibleToSetAndGetEntry() {
00085         $backend = $this->setUpBackend();
00086         $data = 'Some data';
00087         $identifier = uniqid('MyIdentifier');
00088         $backend->set($identifier, $data);
00089         $fetchedData = $backend->get($identifier);
00090         $this->assertEquals($data, $fetchedData, 'APC backend failed to set and retrieve data');
00091     }
00092 
00093     /**
00094      * @test
00095      * @author Christian Jul Jensen <julle@typo3.org>
00096      */
00097     public function itIsPossibleToRemoveEntryFromCache() {
00098         $backend = $this->setUpBackend();
00099         $data = 'Some data';
00100         $identifier = uniqid('MyIdentifier');
00101         $backend->set($identifier, $data);
00102         $backend->remove($identifier);
00103         $inCache = $backend->has($identifier);
00104         $this->assertFalse($inCache, 'Failed to set and remove data from APC backend');
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 = uniqid('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, 'APC backend failed to overwrite and retrieve data');
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         $identifier = uniqid('MyIdentifier');
00131         $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
00132 
00133         $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
00134         $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
00135 
00136         $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
00137         $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
00138     }
00139 
00140     /**
00141      * @test
00142      * @author Karsten Dambekalns <karsten@typo3.org>
00143      */
00144     public function setRemovesTagsFromPreviousSet() {
00145         $backend = $this->setUpBackend();
00146 
00147         $data = 'Some data';
00148         $identifier = uniqid('MyIdentifier');
00149         $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
00150         $backend->set($identifier, $data, array('UnitTestTag%tag3'));
00151 
00152         $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
00153         $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
00154     }
00155 
00156     /**
00157      * @test
00158      * @author Christian Jul Jensen <julle@typo3.org>
00159      */
00160     public function hasReturnsFalseIfTheEntryDoesntExist() {
00161         $backend = $this->setUpBackend();
00162         $identifier = uniqid('NonExistingIdentifier');
00163         $inCache = $backend->has($identifier);
00164         $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
00165     }
00166 
00167     /**
00168      * @test
00169      * @author Christian Jul Jensen <julle@typo3.org>
00170      */
00171     public function removeReturnsFalseIfTheEntryDoesntExist() {
00172         $backend = $this->setUpBackend();
00173         $identifier = uniqid('NonExistingIdentifier');
00174         $inCache = $backend->remove($identifier);
00175         $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
00176     }
00177 
00178     /**
00179      * @test
00180      * @author Robert Lemke <robert@typo3.org>
00181      * @author Karsten Dambekalns <karsten@typo3.org>
00182      */
00183     public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
00184         $backend = $this->setUpBackend();
00185 
00186         $data = 'some data' . microtime();
00187         $backend->set('BackendAPCTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
00188         $backend->set('BackendAPCTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
00189         $backend->set('BackendAPCTest3', $data, array('UnitTestTag%test'));
00190 
00191         $backend->flushByTag('UnitTestTag%special');
00192 
00193         $this->assertTrue($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
00194         $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
00195         $this->assertTrue($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
00196     }
00197 
00198     /**
00199      * @test
00200      * @author Karsten Dambekalns <karsten@typo3.org>
00201      */
00202     public function flushRemovesAllCacheEntries() {
00203         $backend = $this->setUpBackend();
00204 
00205         $data = 'some data' . microtime();
00206         $backend->set('BackendAPCTest1', $data);
00207         $backend->set('BackendAPCTest2', $data);
00208         $backend->set('BackendAPCTest3', $data);
00209 
00210         $backend->flush();
00211 
00212         $this->assertFalse($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
00213         $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
00214         $this->assertFalse($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
00215     }
00216 
00217     /**
00218      * @test
00219      * @author Karsten Dambekalns <karsten@typo3.org>
00220      * @author Ingo Renner <ingo@typo3.org>
00221      */
00222     public function flushRemovesOnlyOwnEntries() {
00223         $thisCache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
00224         $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
00225         $thisBackend = new t3lib_cache_backend_ApcBackend();
00226         $thisBackend->setCache($thisCache);
00227 
00228         $thatCache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
00229         $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
00230         $thatBackend = new t3lib_cache_backend_ApcBackend();
00231         $thatBackend->setCache($thatCache);
00232 
00233         $thisBackend->set('thisEntry', 'Hello');
00234         $thatBackend->set('thatEntry', 'World!');
00235         $thatBackend->flush();
00236 
00237         $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
00238         $this->assertFalse($thatBackend->has('thatEntry'));
00239     }
00240 
00241     /**
00242      * Check if we can store ~5 MB of data
00243      *
00244      * @test
00245      * @author Karsten Dambekalns <karsten@typo3.org>
00246      */
00247     public function largeDataIsStored() {
00248         $backend = $this->setUpBackend();
00249 
00250         $data = str_repeat('abcde', 1024 * 1024);
00251         $identifier = uniqid('tooLargeData');
00252         $backend->set($identifier, $data);
00253 
00254         $this->assertTrue($backend->has($identifier));
00255         $this->assertEquals($backend->get($identifier), $data);
00256     }
00257 
00258     /**
00259      * Sets up the APC backend used for testing
00260      *
00261      * @param array $backendOptions Options for the APC backend
00262      * @return t3lib_cache_backend_ApcBackend
00263      * @author Karsten Dambekalns <karsten@typo3.org>
00264      * @author Ingo Renner <ingo@typo3.org>
00265      */
00266     protected function setUpBackend() {
00267         $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
00268         $backend = new t3lib_cache_backend_ApcBackend();
00269         $backend->setCache($cache);
00270 
00271         return $backend;
00272     }
00273 }
00274 
00275 ?>