TYPO3 CMS  TYPO3_8-7
RegistryTest.php
Go to the documentation of this file.
1 <?php
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 
20 
24 class RegistryTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
25 {
26 
31  {
32  $this->assertNull((new Registry())->get('myExtension', 'myKey'));
33  }
34 
39  {
40  $this->assertSame('myDefault', (new Registry())->get('myExtension', 'myKey', 'myDefault'));
41  }
42 
46  public function getReturnsEntryFromDatabase()
47  {
48  (new ConnectionPool())->getConnectionForTable('sys_registry')
49  ->insert(
50  'sys_registry',
51  [
52  'entry_namespace' => 'myExtension',
53  'entry_key' => 'myKey',
54  'entry_value' => serialize('myValue'),
55  ],
56  [
57  'entry_value' => Connection::PARAM_LOB,
58  ]
59  );
60  $this->assertSame('myValue', (new Registry())->get('myExtension', 'myKey'));
61  }
62 
66  public function setInsertsEntryInDatabase()
67  {
68  (new Registry())->set('myExtension', 'myKey', 'myValue');
69  $valueInDatabase = (new ConnectionPool())->getConnectionForTable('sys_registry')
70  ->select(
71  ['entry_value'],
72  'sys_registry',
73  ['entry_namespace' => 'myExtension', 'entry_key' => 'myKey']
74  )
75  ->fetch();
76  $this->assertSame('myValue', unserialize($valueInDatabase['entry_value']));
77  }
78 
83  {
84  (new ConnectionPool())->getConnectionForTable('sys_registry')
85  ->insert(
86  'sys_registry',
87  [
88  'entry_namespace' => 'myExtension',
89  'entry_key' => 'myKey',
90  'entry_value' => serialize('myValue'),
91  ],
92  [
93  'entry_value' => Connection::PARAM_LOB,
94  ]
95  );
96  (new Registry())->set('myExtension', 'myKey', 'myNewValue');
97  $valueInDatabase = (new ConnectionPool())->getConnectionForTable('sys_registry')
98  ->select(
99  ['entry_value'],
100  'sys_registry',
101  ['entry_namespace' => 'myExtension', 'entry_key' => 'myKey']
102  )
103  ->fetch();
104  $this->assertSame('myNewValue', unserialize($valueInDatabase['entry_value']));
105  }
106 
111  {
112  $connection = (new ConnectionPool())->getConnectionForTable('sys_registry');
113  $connection->bulkInsert(
114  'sys_registry',
115  [
116  ['ns1', 'k1', serialize('v1')],
117  ['ns1', 'k2', serialize('v2')],
118  ['ns2', 'k1', serialize('v1')],
119  ],
120  ['entry_namespace', 'entry_key', 'entry_value'],
121  [
122  'entry_value' => Connection::PARAM_LOB,
123  ]
124  );
125 
126  (new Registry())->remove('ns1', 'k1');
127 
128  $this->assertSame(0, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns1', 'entry_key' => 'k1']));
129  $this->assertSame(1, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns1', 'entry_key' => 'k2']));
130  $this->assertSame(1, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns2', 'entry_key' => 'k1']));
131  }
132 
137  {
138  $connection = (new ConnectionPool())->getConnectionForTable('sys_registry');
139  $connection->bulkInsert(
140  'sys_registry',
141  [
142  ['ns1', 'k1', serialize('v1')],
143  ['ns1', 'k2', serialize('v2')],
144  ['ns2', 'k1', serialize('v1')],
145  ],
146  ['entry_namespace', 'entry_key', 'entry_value'],
147  [
148  'entry_value' => Connection::PARAM_LOB,
149  ]
150  );
151 
152  (new Registry())->removeAllByNamespace('ns1');
153 
154  $this->assertSame(0, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns1', 'entry_key' => 'k1']));
155  $this->assertSame(0, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns1', 'entry_key' => 'k2']));
156  $this->assertSame(1, $connection->count('*', 'sys_registry', ['entry_namespace' => 'ns2', 'entry_key' => 'k1']));
157  }
158 
162  public function canGetSetEntry()
163  {
164  $registry = new Registry();
165  $registry->set('ns1', 'key1', 'value1');
166  $this->assertSame('value1', $registry->get('ns1', 'key1'));
167  }
168 
173  {
174  $registry = new Registry();
175  $registry->set('ns1', 'key1', 'value1');
176  $registry->set('ns1', 'key1', 'value2');
177  $this->assertSame('value2', $registry->get('ns1', 'key1'));
178  }
179 
183  public function canNotGetRemovedEntry()
184  {
185  $registry = new Registry();
186  $registry->set('ns1', 'key1', 'value1');
187  $registry->remove('ns1', 'key1');
188  $this->assertNull($registry->get('ns1', 'key1'));
189  }
190 
195  {
196  $registry = new Registry();
197  $registry->set('ns1', 'key1', 'value1');
198  $registry->removeAllByNamespace('ns1');
199  $this->assertNull($registry->get('ns1', 'key1'));
200  }
201 }