[BUGFIX] Better positionName extraction in executePositionedStringInsertion
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / RegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit;
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
17 /**
18 * Testcase for TYPO3\CMS\Core\Registry
19 */
20 class RegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Core\Registry
24 */
25 protected $registry;
26
27 /**
28 * Sets up this testcase
29 */
30 protected function setUp()
31 {
32 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array());
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
37 /**
38 * @test
39 * @expectedException \InvalidArgumentException
40 */
41 public function getThrowsExceptionForInvalidNamespacesUsingNoNamespace()
42 {
43 $this->registry->get('', 'someKey');
44 }
45
46 /**
47 * @test
48 * @expectedException \InvalidArgumentException
49 */
50 public function getThrowsExceptionForInvalidNamespacesUsingTooShortNamespace()
51 {
52 $this->registry->get('t', 'someKey');
53 }
54
55 /**
56 * @test
57 */
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(array(
63 array('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
68 /**
69 * @test
70 */
71 public function getLazyLoadsEntriesOfOneNamespace()
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(array(
78 array('entry_key' => $testKey1, 'entry_value' => serialize($testValue1)),
79 array('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
85 /**
86 * @test
87 */
88 public function getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound()
89 {
90 $defaultValue = 'getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound';
91 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
92 array('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
97 /**
98 * @test
99 * @expectedException \InvalidArgumentException
100 */
101 public function setThrowsAnExceptionOnEmptyNamespace()
102 {
103 $this->registry->set('', 'someKey', 'someValue');
104 }
105
106 /**
107 * @test
108 * @expectedException \InvalidArgumentException
109 */
110 public function setThrowsAnExceptionOnWrongNamespace()
111 {
112 $this->registry->set('t', 'someKey', 'someValue');
113 }
114
115 /**
116 * @test
117 */
118 public function setAllowsValidNamespaces()
119 {
120 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('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
127 /**
128 * @test
129 */
130 public function setReallySavesTheGivenValueToTheDatabase()
131 {
132 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with('sys_registry', array(
133 'entry_namespace' => 'tx_phpunit',
134 'entry_key' => 'someKey',
135 'entry_value' => serialize('someValue')
136 ));
137 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
138 $registry->set('tx_phpunit', 'someKey', 'someValue');
139 }
140
141 /**
142 * @test
143 */
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\'', array(
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, array('loadEntriesByNamespace'));
153 $registry->set('tx_phpunit', 'someKey', 'someValue');
154 }
155
156 /**
157 * @test
158 * @expectedException \InvalidArgumentException
159 */
160 public function removeThrowsAnExceptionOnWrongNamespace()
161 {
162 $this->registry->remove('t', 'someKey');
163 }
164
165 /**
166 * @test
167 */
168 public function removeReallyRemovesTheEntryFromTheDatabase()
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
174 /**
175 * @test
176 */
177 public function removeUnsetsValueFromTheInternalEntriesCache()
178 {
179 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('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
189 /**
190 * @test
191 * @expectedException \InvalidArgumentException
192 */
193 public function removeAllByNamespaceThrowsAnExceptionOnWrongNamespace()
194 {
195 $this->registry->removeAllByNamespace('');
196 }
197
198 /**
199 * @test
200 */
201 public function removeAllByNamespaceReallyRemovesAllEntriesOfTheSpecifiedNamespaceFromTheDatabase()
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
207 /**
208 * @test
209 */
210 public function removeAllByNamespaceUnsetsValuesOfTheSpecifiedNamespaceFromTheInternalEntriesCache()
211 {
212 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('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 }