TYPO3 CMS  TYPO3_7-6
RegistryTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
25  protected $registry;
26 
30  protected function setUp()
31  {
32  $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, []);
33  $GLOBALS['TYPO3_DB']->expects($this->any())->method('fullQuoteStr')->will($this->onConsecutiveCalls('\'tx_phpunit\'', '\'someKey\'', '\'tx_phpunit\'', '\'someKey\''));
34  $this->registry = new \TYPO3\CMS\Core\Registry();
35  }
36 
42  {
43  $this->registry->get('', 'someKey');
44  }
45 
51  {
52  $this->registry->get('t', 'someKey');
53  }
54 
58  public function getRetrievesTheCorrectEntry()
59  {
60  $testKey = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getRetrievesTheCorrectEntry';
61  $testValue = 'getRetrievesTheCorrectEntry';
62  $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue([
63  ['entry_key' => $testKey, 'entry_value' => serialize($testValue)]
64  ]));
65  $this->assertEquals($this->registry->get('tx_phpunit', $testKey), $testValue, 'The actual data did not match the expected data.');
66  }
67 
72  {
73  $testKey1 = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace1';
74  $testValue1 = 'getLazyLoadsEntriesOfOneNamespace1';
75  $testKey2 = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace2';
76  $testValue2 = 'getLazyLoadsEntriesOfOneNamespace2';
77  $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue([
78  ['entry_key' => $testKey1, 'entry_value' => serialize($testValue1)],
79  ['entry_key' => $testKey2, 'entry_value' => serialize($testValue2)]
80  ]));
81  $this->assertEquals($this->registry->get('tx_phpunit', $testKey1), $testValue1, 'The actual data did not match the expected data.');
82  $this->assertEquals($this->registry->get('tx_phpunit', $testKey2), $testValue2, 'The actual data did not match the expected data.');
83  }
84 
89  {
90  $defaultValue = 'getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound';
91  $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue([
92  ['entry_key' => 'foo', 'entry_value' => 'bar']
93  ]));
94  $this->assertEquals($defaultValue, $this->registry->get('tx_phpunit', 'someNonExistingKey', $defaultValue), 'A value other than the default value was returned.');
95  }
96 
102  {
103  $this->registry->set('', 'someKey', 'someValue');
104  }
105 
111  {
112  $this->registry->set('t', 'someKey', 'someValue');
113  }
114 
118  public function setAllowsValidNamespaces()
119  {
120  $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, ['loadEntriesByNamespace']);
121  $registry->set('tx_thisIsValid', 'someKey', 'someValue');
122  $registry->set('thisIsValid', 'someKey', 'someValue');
123  $registry->set('user_soIsThis', 'someKey', 'someValue');
124  $registry->set('core', 'someKey', 'someValue');
125  }
126 
131  {
132  $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with('sys_registry', [
133  'entry_namespace' => 'tx_phpunit',
134  'entry_key' => 'someKey',
135  'entry_value' => serialize('someValue')
136  ]);
137  $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, ['loadEntriesByNamespace']);
138  $registry->set('tx_phpunit', 'someKey', 'someValue');
139  }
140 
144  public function setUpdatesExistingKeys()
145  {
146  $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTquery')->with('uid', 'sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'')->will($this->returnValue('DBResource'));
147  $GLOBALS['TYPO3_DB']->expects($this->once())->method('sql_num_rows')->with('DBResource')->will($this->returnValue(1));
148  $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_UPDATEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'', [
149  'entry_value' => serialize('someValue')
150  ]);
151  $GLOBALS['TYPO3_DB']->expects($this->never())->method('exec_INSERTquery');
152  $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, ['loadEntriesByNamespace']);
153  $registry->set('tx_phpunit', 'someKey', 'someValue');
154  }
155 
161  {
162  $this->registry->remove('t', 'someKey');
163  }
164 
169  {
170  $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'');
171  $this->registry->remove('tx_phpunit', 'someKey');
172  }
173 
178  {
179  $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, ['loadEntriesByNamespace']);
180  $registry->set('tx_phpunit', 'someKey', 'someValue');
181  $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
182  $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
183  $registry->remove('tx_phpunit', 'someKey');
184  $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
185  $this->assertEquals('someOtherValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
186  $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
187  }
188 
194  {
195  $this->registry->removeAllByNamespace('');
196  }
197 
202  {
203  $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\'');
204  $this->registry->removeAllByNamespace('tx_phpunit');
205  }
206 
211  {
212  $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, ['loadEntriesByNamespace']);
213  $registry->set('tx_phpunit', 'someKey', 'someValue');
214  $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
215  $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
216  $registry->removeAllByNamespace('tx_phpunit');
217  $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
218  $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
219  $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
220  }
221 }
removeAllByNamespaceUnsetsValuesOfTheSpecifiedNamespaceFromTheInternalEntriesCache()
removeAllByNamespaceReallyRemovesAllEntriesOfTheSpecifiedNamespaceFromTheDatabase()
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']