TYPO3 API  SVNRelease
t3lib_registryTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2011 Bastian Waidelich <bastian@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 class t3lib_Registry
00028  *
00029  * @author  Bastian Waidelich <bastian@typo3.org>
00030  * @package TYPO3
00031  * @subpackage t3lib
00032  */
00033 class t3lib_RegistryTest extends tx_phpunit_testcase {
00034 
00035     /**
00036      * @var t3lib_Registry
00037      */
00038     protected $registry;
00039 
00040     /**
00041      * @var t3lib_DB
00042      */
00043     protected $typo3DbBackup;
00044 
00045     /**
00046      * Sets up this testcase
00047      */
00048     public function setUp() {
00049         $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
00050         $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array());
00051         $GLOBALS['TYPO3_DB']->expects($this->any())
00052             ->method('fullQuoteStr')
00053             ->will($this->onConsecutiveCalls('\'tx_phpunit\'', '\'someKey\'', '\'tx_phpunit\'', '\'someKey\''));
00054 
00055         $this->registry = new t3lib_Registry();
00056     }
00057 
00058     /**
00059      * Tears down this testcase
00060      */
00061     public function tearDown() {
00062         $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup;
00063     }
00064 
00065     /**
00066      * @test
00067      * @expectedException InvalidArgumentException
00068      */
00069     public function getThrowsExceptionForInvalidNamespaces() {
00070         $this->registry->get('invalidNamespace', 'someKey');
00071     }
00072 
00073     /**
00074      * @test
00075      */
00076     public function getRetrievesTheCorrectEntry() {
00077         $testKey = 't3lib_Registry_testcase.testData.getRetrievesTheCorrectEntry';
00078         $testValue = 'getRetrievesTheCorrectEntry';
00079 
00080         $GLOBALS['TYPO3_DB']->expects($this->once())
00081             ->method('exec_SELECTgetRows')
00082             ->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')
00083             ->will(
00084                 $this->returnValue(
00085                     array(
00086                         array('entry_key' => $testKey, 'entry_value' => serialize($testValue))
00087                     )
00088                 )
00089             );
00090 
00091         $this->assertEquals(
00092             $this->registry->get('tx_phpunit', $testKey),
00093             $testValue,
00094             'The actual data did not match the expected data.'
00095         );
00096     }
00097 
00098     /**
00099      * @test
00100      */
00101     public function getLazyLoadsEntriesOfOneNamespace() {
00102         $testKey1 = 't3lib_Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace1';
00103         $testValue1 = 'getLazyLoadsEntriesOfOneNamespace1';
00104         $testKey2 = 't3lib_Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace2';
00105         $testValue2 = 'getLazyLoadsEntriesOfOneNamespace2';
00106 
00107         $GLOBALS['TYPO3_DB']->expects($this->once())
00108             ->method('exec_SELECTgetRows')
00109             ->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')
00110             ->will(
00111                 $this->returnValue(
00112                     array(
00113                         array('entry_key' => $testKey1, 'entry_value' => serialize($testValue1)),
00114                         array('entry_key' => $testKey2, 'entry_value' => serialize($testValue2))
00115                     )
00116                 )
00117             );
00118 
00119         $this->assertEquals(
00120             $this->registry->get('tx_phpunit', $testKey1),
00121             $testValue1,
00122             'The actual data did not match the expected data.'
00123         );
00124         $this->assertEquals(
00125             $this->registry->get('tx_phpunit', $testKey2),
00126             $testValue2,
00127             'The actual data did not match the expected data.'
00128         );
00129     }
00130 
00131     /**
00132      * @test
00133      */
00134     public function getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound() {
00135         $defaultValue = 'getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound';
00136 
00137         $GLOBALS['TYPO3_DB']->expects($this->once())
00138             ->method('exec_SELECTgetRows')
00139             ->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')
00140             ->will(
00141                 $this->returnValue(
00142                     array(
00143                         array('entry_key' => 'foo', 'entry_value' => 'bar'),
00144                     )
00145                 )
00146             );
00147 
00148         $this->assertEquals(
00149             $defaultValue,
00150             $this->registry->get('tx_phpunit', 'someNonExistingKey', $defaultValue),
00151             'A value other than the default value was returned.'
00152         );
00153     }
00154 
00155     /**
00156      * @test
00157      * @expectedException InvalidArgumentException
00158      */
00159     public function setThrowsAnExceptionOnEmptyNamespace() {
00160         $this->registry->set('', 'someKey', 'someValue');
00161     }
00162 
00163     /**
00164      * @test
00165      * @expectedException InvalidArgumentException
00166      */
00167     public function setThrowsAnExceptionOnWrongNamespace() {
00168         $this->registry->set('invalidNamespace', 'someKey', 'someValue');
00169     }
00170 
00171     /**
00172      * @test
00173      */
00174     public function setAllowsValidNamespaces() {
00175         $this->registry->set('tx_thisIsValid', 'someKey', 'someValue');
00176         $this->registry->set('user_soIsThis', 'someKey', 'someValue');
00177         $this->registry->set('core', 'someKey', 'someValue');
00178     }
00179 
00180     /**
00181      * @test
00182      */
00183     public function setReallySavesTheGivenValueToTheDatabase() {
00184         $GLOBALS['TYPO3_DB']->expects($this->once())
00185             ->method('exec_INSERTquery')
00186             ->with(
00187                 'sys_registry',
00188                 array(
00189                     'entry_namespace' => 'tx_phpunit',
00190                     'entry_key' => 'someKey',
00191                     'entry_value' => serialize('someValue')
00192                 )
00193             );
00194 
00195         $this->registry->set('tx_phpunit', 'someKey', 'someValue');
00196     }
00197 
00198     /**
00199      * @test
00200      */
00201     public function setUpdatesExistingKeys() {
00202         $GLOBALS['TYPO3_DB']->expects($this->once())
00203             ->method('exec_SELECTquery')
00204             ->with(
00205                 'uid',
00206                 'sys_registry',
00207                 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\''
00208             )
00209             ->will(
00210                 $this->returnValue('DBResource')
00211             );
00212 
00213         $GLOBALS['TYPO3_DB']->expects($this->once())
00214             ->method('sql_num_rows')
00215             ->with('DBResource')
00216             ->will(
00217                 $this->returnValue(1)
00218             );
00219 
00220         $GLOBALS['TYPO3_DB']->expects($this->once())
00221             ->method('exec_UPDATEquery')
00222             ->with(
00223                 'sys_registry',
00224                 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'',
00225                 array(
00226                     'entry_value' => serialize('someValue')
00227                 )
00228             );
00229 
00230         $GLOBALS['TYPO3_DB']->expects($this->never())
00231             ->method('exec_INSERTquery');
00232 
00233         $this->registry->set('tx_phpunit', 'someKey', 'someValue');
00234     }
00235 
00236     /**
00237      * @test
00238      */
00239     public function setStoresValueInTheInternalEntriesCache() {
00240         $registry = $this->getMock('t3lib_Registry', array('loadEntriesByNamespace'));
00241         $registry->expects($this->never())
00242             ->method('loadEntriesByNamespace');
00243 
00244         $registry->set('tx_phpunit', 'someKey', 'someValue');
00245 
00246         $this->assertEquals(
00247             'someValue',
00248             $registry->get('tx_phpunit', 'someKey'),
00249             'The actual data did not match the expected data.'
00250         );
00251     }
00252 
00253     /**
00254      * @test
00255      * @expectedException InvalidArgumentException
00256      */
00257     public function removeThrowsAnExceptionOnWrongNamespace() {
00258         $this->registry->remove('coreInvalid', 'someKey');
00259     }
00260 
00261     /**
00262      * @test
00263      */
00264     public function removeReallyRemovesTheEntryFromTheDatabase() {
00265         $GLOBALS['TYPO3_DB']->expects($this->once())
00266             ->method('exec_DELETEquery')
00267             ->with(
00268                 'sys_registry',
00269                 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\''
00270             );
00271 
00272         $this->registry->remove('tx_phpunit', 'someKey');
00273     }
00274 
00275     /**
00276      * @test
00277      */
00278     public function removeUnsetsValueFromTheInternalEntriesCache() {
00279         $registry = $this->getMock('t3lib_Registry', array('loadEntriesByNamespace'));
00280         $registry->set('tx_phpunit', 'someKey', 'someValue');
00281         $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
00282         $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
00283         $registry->remove('tx_phpunit', 'someKey');
00284 
00285         $this->assertEquals(
00286             'defaultValue',
00287             $registry->get('tx_phpunit', 'someKey', 'defaultValue'),
00288             'A value other than the default value was returned, thus the entry was still present.'
00289         );
00290 
00291         $this->assertEquals(
00292             'someOtherValue',
00293             $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'),
00294             'A value other than the stored value was returned, thus the entry was removed.'
00295         );
00296 
00297         $this->assertEquals(
00298             'someValueInOtherNamespace',
00299             $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'),
00300             'A value other than the stored value was returned, thus the entry was removed.'
00301         );
00302     }
00303 
00304     /**
00305      * @test
00306      * @expectedException InvalidArgumentException
00307      */
00308     public function removeAllByNamespaceThrowsAnExceptionOnWrongNamespace() {
00309         $this->registry->removeAllByNamespace('');
00310     }
00311 
00312     /**
00313      * @test
00314      */
00315     public function removeAllByNamespaceReallyRemovesAllEntriesOfTheSpecifiedNamespaceFromTheDatabase() {
00316         $GLOBALS['TYPO3_DB']->expects($this->once())
00317             ->method('exec_DELETEquery')
00318             ->with(
00319                 'sys_registry',
00320                 'entry_namespace = \'tx_phpunit\''
00321             );
00322 
00323         $this->registry->removeAllByNamespace('tx_phpunit');
00324     }
00325 
00326     /**
00327      * @test
00328      */
00329     public function removeAllByNamespaceUnsetsValuesOfTheSpecifiedNamespaceFromTheInternalEntriesCache() {
00330         $registry = $this->getMock('t3lib_Registry', array('loadEntriesByNamespace'));
00331         $registry->set('tx_phpunit', 'someKey', 'someValue');
00332         $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
00333         $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
00334         $registry->removeAllByNamespace('tx_phpunit');
00335 
00336         $this->assertEquals(
00337             'defaultValue',
00338             $registry->get('tx_phpunit', 'someKey', 'defaultValue'),
00339             'A value other than the default value was returned, thus the entry was still present.'
00340         );
00341 
00342         $this->assertEquals(
00343             'defaultValue',
00344             $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'),
00345             'A value other than the default value was returned, thus the entry was still present.'
00346         );
00347 
00348         $this->assertEquals(
00349             'someValueInOtherNamespace',
00350             $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'),
00351             'A value other than the stored value was returned, thus the entry was removed.'
00352         );
00353     }
00354 }
00355 
00356 ?>