[TASK] Split DataHandler::copyRecord_procBasedOnFieldType
[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 * @author Bastian Waidelich <bastian@typo3.org>
21 */
22 class RegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * @var \TYPO3\CMS\Core\Registry
26 */
27 protected $registry;
28
29 /**
30 * Sets up this testcase
31 */
32 protected function setUp() {
33 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array());
34 $GLOBALS['TYPO3_DB']->expects($this->any())->method('fullQuoteStr')->will($this->onConsecutiveCalls('\'tx_phpunit\'', '\'someKey\'', '\'tx_phpunit\'', '\'someKey\''));
35 $this->registry = new \TYPO3\CMS\Core\Registry();
36 }
37
38 /**
39 * @test
40 * @expectedException \InvalidArgumentException
41 */
42 public function getThrowsExceptionForInvalidNamespacesUsingNoNamespace() {
43 $this->registry->get('', 'someKey');
44 }
45
46 /**
47 * @test
48 * @expectedException \InvalidArgumentException
49 */
50 public function getThrowsExceptionForInvalidNamespacesUsingTooShortNamespace() {
51 $this->registry->get('t', 'someKey');
52 }
53
54 /**
55 * @test
56 */
57 public function getRetrievesTheCorrectEntry() {
58 $testKey = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getRetrievesTheCorrectEntry';
59 $testValue = 'getRetrievesTheCorrectEntry';
60 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
61 array('entry_key' => $testKey, 'entry_value' => serialize($testValue))
62 )));
63 $this->assertEquals($this->registry->get('tx_phpunit', $testKey), $testValue, 'The actual data did not match the expected data.');
64 }
65
66 /**
67 * @test
68 */
69 public function getLazyLoadsEntriesOfOneNamespace() {
70 $testKey1 = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace1';
71 $testValue1 = 'getLazyLoadsEntriesOfOneNamespace1';
72 $testKey2 = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace2';
73 $testValue2 = 'getLazyLoadsEntriesOfOneNamespace2';
74 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
75 array('entry_key' => $testKey1, 'entry_value' => serialize($testValue1)),
76 array('entry_key' => $testKey2, 'entry_value' => serialize($testValue2))
77 )));
78 $this->assertEquals($this->registry->get('tx_phpunit', $testKey1), $testValue1, 'The actual data did not match the expected data.');
79 $this->assertEquals($this->registry->get('tx_phpunit', $testKey2), $testValue2, 'The actual data did not match the expected data.');
80 }
81
82 /**
83 * @test
84 */
85 public function getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound() {
86 $defaultValue = 'getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound';
87 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
88 array('entry_key' => 'foo', 'entry_value' => 'bar')
89 )));
90 $this->assertEquals($defaultValue, $this->registry->get('tx_phpunit', 'someNonExistingKey', $defaultValue), 'A value other than the default value was returned.');
91 }
92
93 /**
94 * @test
95 * @expectedException \InvalidArgumentException
96 */
97 public function setThrowsAnExceptionOnEmptyNamespace() {
98 $this->registry->set('', 'someKey', 'someValue');
99 }
100
101 /**
102 * @test
103 * @expectedException \InvalidArgumentException
104 */
105 public function setThrowsAnExceptionOnWrongNamespace() {
106 $this->registry->set('t', 'someKey', 'someValue');
107 }
108
109 /**
110 * @test
111 */
112 public function setAllowsValidNamespaces() {
113 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
114 $registry->set('tx_thisIsValid', 'someKey', 'someValue');
115 $registry->set('thisIsValid', 'someKey', 'someValue');
116 $registry->set('user_soIsThis', 'someKey', 'someValue');
117 $registry->set('core', 'someKey', 'someValue');
118 }
119
120 /**
121 * @test
122 */
123 public function setReallySavesTheGivenValueToTheDatabase() {
124 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with('sys_registry', array(
125 'entry_namespace' => 'tx_phpunit',
126 'entry_key' => 'someKey',
127 'entry_value' => serialize('someValue')
128 ));
129 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
130 $registry->set('tx_phpunit', 'someKey', 'someValue');
131 }
132
133 /**
134 * @test
135 */
136 public function setUpdatesExistingKeys() {
137 $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'));
138 $GLOBALS['TYPO3_DB']->expects($this->once())->method('sql_num_rows')->with('DBResource')->will($this->returnValue(1));
139 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_UPDATEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'', array(
140 'entry_value' => serialize('someValue')
141 ));
142 $GLOBALS['TYPO3_DB']->expects($this->never())->method('exec_INSERTquery');
143 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
144 $registry->set('tx_phpunit', 'someKey', 'someValue');
145 }
146
147 /**
148 * @test
149 * @expectedException \InvalidArgumentException
150 */
151 public function removeThrowsAnExceptionOnWrongNamespace() {
152 $this->registry->remove('t', 'someKey');
153 }
154
155 /**
156 * @test
157 */
158 public function removeReallyRemovesTheEntryFromTheDatabase() {
159 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'');
160 $this->registry->remove('tx_phpunit', 'someKey');
161 }
162
163 /**
164 * @test
165 */
166 public function removeUnsetsValueFromTheInternalEntriesCache() {
167 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
168 $registry->set('tx_phpunit', 'someKey', 'someValue');
169 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
170 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
171 $registry->remove('tx_phpunit', 'someKey');
172 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
173 $this->assertEquals('someOtherValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
174 $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
175 }
176
177 /**
178 * @test
179 * @expectedException \InvalidArgumentException
180 */
181 public function removeAllByNamespaceThrowsAnExceptionOnWrongNamespace() {
182 $this->registry->removeAllByNamespace('');
183 }
184
185 /**
186 * @test
187 */
188 public function removeAllByNamespaceReallyRemovesAllEntriesOfTheSpecifiedNamespaceFromTheDatabase() {
189 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\'');
190 $this->registry->removeAllByNamespace('tx_phpunit');
191 }
192
193 /**
194 * @test
195 */
196 public function removeAllByNamespaceUnsetsValuesOfTheSpecifiedNamespaceFromTheInternalEntriesCache() {
197 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
198 $registry->set('tx_phpunit', 'someKey', 'someValue');
199 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
200 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
201 $registry->removeAllByNamespace('tx_phpunit');
202 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
203 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
204 $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
205 }
206
207 }