[FOLLOWUP][TASK] Replace @expectedException in ext:core
[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 */
40 public function getThrowsExceptionForInvalidNamespacesUsingNoNamespace()
41 {
42 $this->expectException(\InvalidArgumentException::class);
43 $this->expectExceptionCode(1249755131);
44
45 $this->registry->get('', 'someKey');
46 }
47
48 /**
49 * @test
50 */
51 public function getThrowsExceptionForInvalidNamespacesUsingTooShortNamespace()
52 {
53 $this->expectException(\InvalidArgumentException::class);
54 $this->expectExceptionCode(1249755131);
55
56 $this->registry->get('t', 'someKey');
57 }
58
59 /**
60 * @test
61 */
62 public function getRetrievesTheCorrectEntry()
63 {
64 $testKey = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getRetrievesTheCorrectEntry';
65 $testValue = 'getRetrievesTheCorrectEntry';
66 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
67 array('entry_key' => $testKey, 'entry_value' => serialize($testValue))
68 )));
69 $this->assertEquals($this->registry->get('tx_phpunit', $testKey), $testValue, 'The actual data did not match the expected data.');
70 }
71
72 /**
73 * @test
74 */
75 public function getLazyLoadsEntriesOfOneNamespace()
76 {
77 $testKey1 = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace1';
78 $testValue1 = 'getLazyLoadsEntriesOfOneNamespace1';
79 $testKey2 = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace2';
80 $testValue2 = 'getLazyLoadsEntriesOfOneNamespace2';
81 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
82 array('entry_key' => $testKey1, 'entry_value' => serialize($testValue1)),
83 array('entry_key' => $testKey2, 'entry_value' => serialize($testValue2))
84 )));
85 $this->assertEquals($this->registry->get('tx_phpunit', $testKey1), $testValue1, 'The actual data did not match the expected data.');
86 $this->assertEquals($this->registry->get('tx_phpunit', $testKey2), $testValue2, 'The actual data did not match the expected data.');
87 }
88
89 /**
90 * @test
91 */
92 public function getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound()
93 {
94 $defaultValue = 'getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound';
95 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
96 array('entry_key' => 'foo', 'entry_value' => 'bar')
97 )));
98 $this->assertEquals($defaultValue, $this->registry->get('tx_phpunit', 'someNonExistingKey', $defaultValue), 'A value other than the default value was returned.');
99 }
100
101 /**
102 * @test
103 */
104 public function setThrowsAnExceptionOnEmptyNamespace()
105 {
106 $this->expectException(\InvalidArgumentException::class);
107 $this->expectExceptionCode(1249755131);
108
109 $this->registry->set('', 'someKey', 'someValue');
110 }
111
112 /**
113 * @test
114 */
115 public function setThrowsAnExceptionOnWrongNamespace()
116 {
117 $this->expectException(\InvalidArgumentException::class);
118 $this->expectExceptionCode(1249755131);
119
120 $this->registry->set('t', 'someKey', 'someValue');
121 }
122
123 /**
124 * @test
125 */
126 public function setAllowsValidNamespaces()
127 {
128 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
129 $registry->set('tx_thisIsValid', 'someKey', 'someValue');
130 $registry->set('thisIsValid', 'someKey', 'someValue');
131 $registry->set('user_soIsThis', 'someKey', 'someValue');
132 $registry->set('core', 'someKey', 'someValue');
133 }
134
135 /**
136 * @test
137 */
138 public function setReallySavesTheGivenValueToTheDatabase()
139 {
140 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with('sys_registry', array(
141 'entry_namespace' => 'tx_phpunit',
142 'entry_key' => 'someKey',
143 'entry_value' => serialize('someValue')
144 ));
145 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
146 $registry->set('tx_phpunit', 'someKey', 'someValue');
147 }
148
149 /**
150 * @test
151 */
152 public function setUpdatesExistingKeys()
153 {
154 $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'));
155 $GLOBALS['TYPO3_DB']->expects($this->once())->method('sql_num_rows')->with('DBResource')->will($this->returnValue(1));
156 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_UPDATEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'', array(
157 'entry_value' => serialize('someValue')
158 ));
159 $GLOBALS['TYPO3_DB']->expects($this->never())->method('exec_INSERTquery');
160 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
161 $registry->set('tx_phpunit', 'someKey', 'someValue');
162 }
163
164 /**
165 * @test
166 */
167 public function removeThrowsAnExceptionOnWrongNamespace()
168 {
169 $this->expectException(\InvalidArgumentException::class);
170 $this->expectExceptionCode(1249755131);
171
172 $this->registry->remove('t', 'someKey');
173 }
174
175 /**
176 * @test
177 */
178 public function removeReallyRemovesTheEntryFromTheDatabase()
179 {
180 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'');
181 $this->registry->remove('tx_phpunit', 'someKey');
182 }
183
184 /**
185 * @test
186 */
187 public function removeUnsetsValueFromTheInternalEntriesCache()
188 {
189 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
190 $registry->set('tx_phpunit', 'someKey', 'someValue');
191 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
192 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
193 $registry->remove('tx_phpunit', 'someKey');
194 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
195 $this->assertEquals('someOtherValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
196 $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
197 }
198
199 /**
200 * @test
201 */
202 public function removeAllByNamespaceThrowsAnExceptionOnWrongNamespace()
203 {
204 $this->expectException(\InvalidArgumentException::class);
205 $this->expectExceptionCode(1249755131);
206
207 $this->registry->removeAllByNamespace('');
208 }
209
210 /**
211 * @test
212 */
213 public function removeAllByNamespaceReallyRemovesAllEntriesOfTheSpecifiedNamespaceFromTheDatabase()
214 {
215 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\'');
216 $this->registry->removeAllByNamespace('tx_phpunit');
217 }
218
219 /**
220 * @test
221 */
222 public function removeAllByNamespaceUnsetsValuesOfTheSpecifiedNamespaceFromTheInternalEntriesCache()
223 {
224 $registry = $this->getMock(\TYPO3\CMS\Core\Registry::class, array('loadEntriesByNamespace'));
225 $registry->set('tx_phpunit', 'someKey', 'someValue');
226 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
227 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
228 $registry->removeAllByNamespace('tx_phpunit');
229 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
230 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
231 $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
232 }
233 }