TYPO3 API  SVNRelease
t3lib_cache_backend_memcachedbackendTest.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 cache to memcached 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_memcachedbackendTest.php 10121 2011-01-18 20:15:30Z ohader $
00034  */
00035 class t3lib_cache_backend_MemcachedBackendTest extends tx_phpunit_testcase {
00036 
00037     /**
00038      * Sets up this testcase
00039      *
00040      * @return void
00041      * @author Christian Jul Jensen <julle@typo3.org>
00042      * @author Ingo Renner <ingo@typo3.org>
00043      */
00044     public function setUp() {
00045         if (!extension_loaded('memcache')) {
00046             $this->markTestSkipped('memcache extension was not available');
00047         }
00048 
00049         try {
00050             if (!fsockopen('localhost', 11211)) {
00051                 $this->markTestSkipped('memcached not reachable');
00052             }
00053         } catch (Exception $e) {
00054             $this->markTestSkipped('memcached not reachable');
00055         }
00056     }
00057 
00058     /**
00059      * @test
00060      * @author Christian Jul Jensen <julle@typo3.org>
00061      * @author Ingo Renner <ingo@typo3.org>
00062      * @expectedException t3lib_cache_Exception
00063      */
00064     public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
00065         $backendOptions = array('servers' => array('localhost:11211'));
00066         $backend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
00067 
00068         $data = 'Some data';
00069         $identifier = uniqid('MyIdentifier');
00070         $backend->set($identifier, $data);
00071     }
00072 
00073     /**
00074      * @test
00075      * @author Robert Lemke <robert@typo3.org>
00076      * @author Ingo Renner <ingo@typo3.org>
00077      * @expectedException t3lib_cache_Exception
00078      */
00079     public function constructorThrowsExceptionIfNoMemcacheServerIsConfigured() {
00080         $backend = new t3lib_cache_backend_MemcachedBackend();
00081     }
00082 
00083     /**
00084      * @test
00085      * @author Christian Jul Jensen <julle@typo3.org>
00086      * @author Ingo Renner <ingo@typo3.org>
00087      * @expectedException t3lib_cache_Exception
00088      */
00089     public function setThrowsExceptionIfConfiguredServersAreUnreachable() {
00090         $backend = $this->setUpBackend(array('servers' => array('julle.did.this:1234')));
00091         $data = 'Somedata';
00092         $identifier = uniqid('MyIdentifier');
00093         $backend->set($identifier, $data);
00094     }
00095 
00096     /**
00097      * @test
00098      * @author Christian Jul Jensen <julle@typo3.org>
00099      */
00100     public function itIsPossibleToSetAndCheckExistenceInCache() {
00101         $backend = $this->setUpBackend();
00102         $data = 'Some data';
00103         $identifier = uniqid('MyIdentifier');
00104         $backend->set($identifier, $data);
00105         $inCache = $backend->has($identifier);
00106         $this->assertTrue($inCache, 'Memcache failed to set and check entry');
00107     }
00108 
00109     /**
00110      * @test
00111      * @author Christian Jul Jensen <julle@typo3.org>
00112      */
00113     public function itIsPossibleToSetAndGetEntry() {
00114         $backend = $this->setUpBackend();
00115         $data = 'Some data';
00116         $identifier = uniqid('MyIdentifier');
00117         $backend->set($identifier, $data);
00118         $fetchedData = $backend->get($identifier);
00119         $this->assertEquals($data, $fetchedData, 'Memcache failed to set and retrieve data');
00120     }
00121 
00122     /**
00123      * @test
00124      * @author Christian Jul Jensen <julle@typo3.org>
00125      */
00126     public function itIsPossibleToRemoveEntryFromCache() {
00127         $backend = $this->setUpBackend();
00128         $data = 'Some data';
00129         $identifier = uniqid('MyIdentifier');
00130         $backend->set($identifier, $data);
00131         $backend->remove($identifier);
00132         $inCache = $backend->has($identifier);
00133         $this->assertFalse($inCache, 'Failed to set and remove data from Memcache');
00134     }
00135 
00136     /**
00137      * @test
00138      * @author Christian Jul Jensen <julle@typo3.org>
00139      */
00140     public function itIsPossibleToOverwriteAnEntryInTheCache() {
00141         $backend = $this->setUpBackend();
00142         $data = 'Some data';
00143         $identifier = uniqid('MyIdentifier');
00144         $backend->set($identifier, $data);
00145         $otherData = 'some other data';
00146         $backend->set($identifier, $otherData);
00147         $fetchedData = $backend->get($identifier);
00148         $this->assertEquals($otherData, $fetchedData, 'Memcache failed to overwrite and retrieve data');
00149     }
00150 
00151     /**
00152      * @test
00153      * @author Karsten Dambekalns <karsten@typo3.org>
00154      */
00155     public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
00156         $backend = $this->setUpBackend();
00157 
00158         $data = 'Some data';
00159         $identifier = uniqid('MyIdentifier');
00160         $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
00161 
00162         $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
00163         $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
00164 
00165         $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
00166         $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
00167     }
00168 
00169     /**
00170      * @test
00171      * @author Karsten Dambekalns <karsten@typo3.org>
00172      */
00173     public function setRemovesTagsFromPreviousSet() {
00174         $backend = $this->setUpBackend();
00175 
00176         $data = 'Some data';
00177         $identifier = uniqid('MyIdentifier');
00178         $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
00179         $backend->set($identifier, $data, array('UnitTestTag%tag3'));
00180 
00181         $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
00182         $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
00183     }
00184 
00185     /**
00186      * @test
00187      * @author Christian Jul Jensen <julle@typo3.org>
00188      */
00189     public function hasReturnsFalseIfTheEntryDoesntExist() {
00190         $backend = $this->setUpBackend();
00191         $identifier = uniqid('NonExistingIdentifier');
00192         $inCache = $backend->has($identifier);
00193         $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
00194     }
00195 
00196     /**
00197      * @test
00198      * @author Christian Jul Jensen <julle@typo3.org>
00199      */
00200     public function removeReturnsFalseIfTheEntryDoesntExist() {
00201         $backend = $this->setUpBackend();
00202         $identifier = uniqid('NonExistingIdentifier');
00203         $inCache = $backend->remove($identifier);
00204         $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
00205     }
00206 
00207     /**
00208      * @test
00209      * @author Robert Lemke <robert@typo3.org>
00210      * @author Karsten Dambekalns <karsten@typo3.org>
00211      */
00212     public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
00213         $backend = $this->setUpBackend();
00214 
00215         $data = 'some data' . microtime();
00216         $backend->set('BackendMemcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
00217         $backend->set('BackendMemcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
00218         $backend->set('BackendMemcacheTest3', $data, array('UnitTestTag%test'));
00219 
00220         $backend->flushByTag('UnitTestTag%special');
00221 
00222         $this->assertTrue($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
00223         $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
00224         $this->assertTrue($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
00225     }
00226 
00227     /**
00228      * @test
00229      * @author Karsten Dambekalns <karsten@typo3.org>
00230      */
00231     public function flushRemovesAllCacheEntries() {
00232         $backend = $this->setUpBackend();
00233 
00234         $data = 'some data' . microtime();
00235         $backend->set('BackendMemcacheTest1', $data);
00236         $backend->set('BackendMemcacheTest2', $data);
00237         $backend->set('BackendMemcacheTest3', $data);
00238 
00239         $backend->flush();
00240 
00241         $this->assertFalse($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
00242         $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
00243         $this->assertFalse($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
00244     }
00245 
00246     /**
00247      * @test
00248      * @author Karsten Dambekalns <karsten@typo3.org>
00249      * @author Ingo Renner <ingo@typo3.org>
00250      */
00251     public function flushRemovesOnlyOwnEntries() {
00252         $backendOptions = array('servers' => array('localhost:11211'));
00253 
00254         $thisCache = $this->getMock(
00255             't3lib_cache_frontend_AbstractFrontend',
00256             array(),
00257             array(),
00258             '',
00259             FALSE
00260         );
00261         $thisCache->expects($this->any())
00262             ->method('getIdentifier')
00263             ->will($this->returnValue('thisCache'));
00264         $thisBackend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
00265         $thisBackend->setCache($thisCache);
00266 
00267         $thatCache = $this->getMock(
00268             't3lib_cache_frontend_AbstractFrontend',
00269             array(),
00270             array(),
00271             '',
00272             FALSE
00273         );
00274         $thatCache->expects($this->any())
00275             ->method('getIdentifier')
00276             ->will($this->returnValue('thatCache'));
00277         $thatBackend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
00278         $thatBackend->setCache($thatCache);
00279 
00280         $thisBackend->set('thisEntry', 'Hello');
00281         $thatBackend->set('thatEntry', 'World!');
00282         $thatBackend->flush();
00283 
00284         $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
00285         $this->assertFalse($thatBackend->has('thatEntry'));
00286     }
00287 
00288     /**
00289      * Check if we can store ~5 MB of data, this gives some headroom for the
00290      * reflection data.
00291      *
00292      * @test
00293      * @author Karsten Dambekalns <karsten@typo3.org>
00294      */
00295     public function largeDataIsStored() {
00296         $backend = $this->setUpBackend();
00297 
00298         $data = str_repeat('abcde', 1024 * 1024);
00299         $backend->set('tooLargeData', $data);
00300 
00301         $this->assertTrue($backend->has('tooLargeData'));
00302         $this->assertEquals($backend->get('tooLargeData'), $data);
00303     }
00304 
00305     /**
00306      * Sets up the memcached backend used for testing
00307      *
00308      * @param   array   $backendOptions Options for the memcache backend
00309      * @return t3lib_cache_backend_MemcachedBackend
00310      * @author Christian Jul Jensen <julle@typo3.org>
00311      * @author Karsten Dambekalns <karsten@typo3.org>
00312      * @author Ingo Renner <ingo@typo3.org>
00313      */
00314     protected function setUpBackend(array $backendOptions = array()) {
00315         $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
00316         if (empty($backendOptions)) {
00317             $backendOptions = array('servers' => array('localhost:11211'));
00318         }
00319 
00320         $backend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
00321         $backend->setCache($cache);
00322 
00323         return $backend;
00324     }
00325 }
00326 
00327 ?>