[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / RegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional;
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 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Registry;
19
20 /**
21 * Test case
22 */
23 class RegistryTest extends \TYPO3\Components\TestingFramework\Core\Functional\FunctionalTestCase
24 {
25
26 /**
27 * @test
28 */
29 public function getReturnsNullIfEntryIsNotInDatabase()
30 {
31 $this->assertNull((new Registry())->get('myExtension', 'myKey'));
32 }
33
34 /**
35 * @test
36 */
37 public function getReturnsDefaultValueIfEntryIsNotInDatabase()
38 {
39 $this->assertSame('myDefault', (new Registry())->get('myExtension', 'myKey', 'myDefault'));
40 }
41
42 /**
43 * @test
44 */
45 public function getReturnsEntryFromDatabase()
46 {
47 (new ConnectionPool())->getConnectionForTable('sys_registry')
48 ->insert(
49 'sys_registry',
50 [
51 'entry_namespace' => 'myExtension',
52 'entry_key' => 'myKey',
53 'entry_value' => serialize('myValue'),
54 ]
55 );
56 $this->assertSame('myValue', (new Registry())->get('myExtension', 'myKey'));
57 }
58
59 /**
60 * @test
61 */
62 public function setInsertsEntryInDatabase()
63 {
64 (new Registry())->set('myExtension', 'myKey', 'myValue');
65 $valueInDatabase = (new ConnectionPool())->getConnectionForTable('sys_registry')
66 ->select(
67 ['entry_value'],
68 'sys_registry',
69 ['entry_namespace' => 'myExtension', 'entry_key' => 'myKey']
70 )
71 ->fetch();
72 $this->assertSame('myValue', unserialize($valueInDatabase['entry_value']));
73 }
74
75 /**
76 * @test
77 */
78 public function setOverridesExistingEntryInDatabase()
79 {
80 (new ConnectionPool())->getConnectionForTable('sys_registry')
81 ->insert(
82 'sys_registry',
83 [
84 'entry_namespace' => 'myExtension',
85 'entry_key' => 'myKey',
86 'entry_value' => serialize('myValue'),
87 ]
88 );
89 (new Registry())->set('myExtension', 'myKey', 'myNewValue');
90 $valueInDatabase = (new ConnectionPool())->getConnectionForTable('sys_registry')
91 ->select(
92 ['entry_value'],
93 'sys_registry',
94 ['entry_namespace' => 'myExtension', 'entry_key' => 'myKey']
95 )
96 ->fetch();
97 $this->assertSame('myNewValue', unserialize($valueInDatabase['entry_value']));
98 }
99
100 /**
101 * @test
102 */
103 public function removeDeletesEntryInDatabaseButLeavesOthers()
104 {
105 $connection = (new ConnectionPool())->getConnectionForTable('sys_registry');
106 $connection->bulkInsert(
107 'sys_registry',
108 [
109 ['ns1', 'k1', serialize('v1')],
110 ['ns1', 'k2', serialize('v2')],
111 ['ns2', 'k1', serialize('v1')],
112 ],
113 ['entry_namespace', 'entry_key', 'entry_value']
114 );
115
116 (new Registry())->remove('ns1', 'k1');
117
118 $this->assertSame(0, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns1', 'entry_key' => 'k1']));
119 $this->assertSame(1, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns1', 'entry_key' => 'k2']));
120 $this->assertSame(1, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns2', 'entry_key' => 'k1']));
121 }
122
123 /**
124 * @test
125 */
126 public function removeAllByNamespaceDeletesEntryInDatabaseAndLeavesOthers()
127 {
128 $connection = (new ConnectionPool())->getConnectionForTable('sys_registry');
129 $connection->bulkInsert(
130 'sys_registry',
131 [
132 ['ns1', 'k1', serialize('v1')],
133 ['ns1', 'k2', serialize('v2')],
134 ['ns2', 'k1', serialize('v1')],
135 ],
136 ['entry_namespace', 'entry_key', 'entry_value']
137 );
138
139 (new Registry())->removeAllByNamespace('ns1');
140
141 $this->assertSame(0, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns1', 'entry_key' => 'k1']));
142 $this->assertSame(0, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns1', 'entry_key' => 'k2']));
143 $this->assertSame(1, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns2', 'entry_key' => 'k1']));
144 }
145
146 /**
147 * @test
148 */
149 public function canGetSetEntry()
150 {
151 $registry = new Registry();
152 $registry->set('ns1', 'key1', 'value1');
153 $this->assertSame('value1', $registry->get('ns1', 'key1'));
154 }
155
156 /**
157 * @test
158 */
159 public function getReturnsNewValueIfValueHasBeenSetMultipleTimes()
160 {
161 $registry = new Registry();
162 $registry->set('ns1', 'key1', 'value1');
163 $registry->set('ns1', 'key1', 'value2');
164 $this->assertSame('value2', $registry->get('ns1', 'key1'));
165 }
166
167 /**
168 * @test
169 */
170 public function canNotGetRemovedEntry()
171 {
172 $registry = new Registry();
173 $registry->set('ns1', 'key1', 'value1');
174 $registry->remove('ns1', 'key1');
175 $this->assertNull($registry->get('ns1', 'key1'));
176 }
177
178 /**
179 * @test
180 */
181 public function canNotGetRemovedAllByNamespaceEntry()
182 {
183 $registry = new Registry();
184 $registry->set('ns1', 'key1', 'value1');
185 $registry->removeAllByNamespace('ns1');
186 $this->assertNull($registry->get('ns1', 'key1'));
187 }
188 }