[TASK] Re-work/simplify copyright header in PHP files - Part 4
[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 public function setUp() {
33 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', 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 $this->registry->set('tx_thisIsValid', 'someKey', 'someValue');
114 $this->registry->set('thisIsValid', 'someKey', 'someValue');
115 $this->registry->set('user_soIsThis', 'someKey', 'someValue');
116 $this->registry->set('core', 'someKey', 'someValue');
117 }
118
119 /**
120 * @test
121 */
122 public function setReallySavesTheGivenValueToTheDatabase() {
123 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with('sys_registry', array(
124 'entry_namespace' => 'tx_phpunit',
125 'entry_key' => 'someKey',
126 'entry_value' => serialize('someValue')
127 ));
128 $this->registry->set('tx_phpunit', 'someKey', 'someValue');
129 }
130
131 /**
132 * @test
133 */
134 public function setUpdatesExistingKeys() {
135 $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'));
136 $GLOBALS['TYPO3_DB']->expects($this->once())->method('sql_num_rows')->with('DBResource')->will($this->returnValue(1));
137 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_UPDATEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'', array(
138 'entry_value' => serialize('someValue')
139 ));
140 $GLOBALS['TYPO3_DB']->expects($this->never())->method('exec_INSERTquery');
141 $this->registry->set('tx_phpunit', 'someKey', 'someValue');
142 }
143
144 /**
145 * @test
146 */
147 public function setStoresValueInTheInternalEntriesCache() {
148 $registry = $this->getMock('TYPO3\\CMS\\Core\\Registry', array('loadEntriesByNamespace'));
149 $registry->expects($this->never())->method('loadEntriesByNamespace');
150 $registry->set('tx_phpunit', 'someKey', 'someValue');
151 $this->assertEquals('someValue', $registry->get('tx_phpunit', 'someKey'), 'The actual data did not match the expected data.');
152 }
153
154 /**
155 * @test
156 * @expectedException \InvalidArgumentException
157 */
158 public function removeThrowsAnExceptionOnWrongNamespace() {
159 $this->registry->remove('t', 'someKey');
160 }
161
162 /**
163 * @test
164 */
165 public function removeReallyRemovesTheEntryFromTheDatabase() {
166 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'');
167 $this->registry->remove('tx_phpunit', 'someKey');
168 }
169
170 /**
171 * @test
172 */
173 public function removeUnsetsValueFromTheInternalEntriesCache() {
174 $registry = $this->getMock('TYPO3\\CMS\\Core\\Registry', array('loadEntriesByNamespace'));
175 $registry->set('tx_phpunit', 'someKey', 'someValue');
176 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
177 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
178 $registry->remove('tx_phpunit', 'someKey');
179 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
180 $this->assertEquals('someOtherValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
181 $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
182 }
183
184 /**
185 * @test
186 * @expectedException \InvalidArgumentException
187 */
188 public function removeAllByNamespaceThrowsAnExceptionOnWrongNamespace() {
189 $this->registry->removeAllByNamespace('');
190 }
191
192 /**
193 * @test
194 */
195 public function removeAllByNamespaceReallyRemovesAllEntriesOfTheSpecifiedNamespaceFromTheDatabase() {
196 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\'');
197 $this->registry->removeAllByNamespace('tx_phpunit');
198 }
199
200 /**
201 * @test
202 */
203 public function removeAllByNamespaceUnsetsValuesOfTheSpecifiedNamespaceFromTheInternalEntriesCache() {
204 $registry = $this->getMock('TYPO3\\CMS\\Core\\Registry', array('loadEntriesByNamespace'));
205 $registry->set('tx_phpunit', 'someKey', 'someValue');
206 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
207 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
208 $registry->removeAllByNamespace('tx_phpunit');
209 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
210 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
211 $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
212 }
213
214 }