]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/Cache/ConfigCacheTest.php
Config FollowUp
[friendica.git] / tests / src / Core / Config / Cache / ConfigCacheTest.php
1 <?php
2
3 namespace Friendica\Test\Core\Config\Cache;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Test\MockedTest;
7
8 class ConfigCacheTest extends MockedTest
9 {
10         public function dataTests()
11         {
12                 return [
13                         'normal' => [
14                                 'data' => [
15                                         'system' => [
16                                                 'test' => 'it',
17                                                 'boolTrue' => true,
18                                                 'boolFalse' => false,
19                                                 'int' => 235,
20                                                 'dec' => 2.456,
21                                                 'array' => ['1', 2, '3', true, false],
22                                         ],
23                                         'config' => [
24                                                 'a' => 'value',
25                                         ],
26                                 ]
27                         ]
28                 ];
29         }
30
31         private function assertConfigValues($data, ConfigCache $configCache, $uid = null)
32         {
33                 foreach ($data as $cat => $values) {
34                         foreach ($values as $key => $value) {
35                                 if (isset($uid)) {
36                                         $this->assertEquals($data[$cat][$key], $configCache->getP($uid, $cat, $key));
37                                 } else {
38                                         $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
39                                 }
40                         }
41                 }
42         }
43
44         /**
45          * Test the loadConfigArray() method without override
46          * @dataProvider dataTests
47          */
48         public function testLoadConfigArray($data)
49         {
50                 $configCache = new ConfigCache();
51                 $configCache->load($data);
52
53                 $this->assertConfigValues($data, $configCache);
54         }
55
56         /**
57          * Test the loadConfigArray() method with overrides
58          * @dataProvider dataTests
59          */
60         public function testLoadConfigArrayOverride($data)
61         {
62                 $override = [
63                         'system' => [
64                                 'test' => 'not',
65                                 'boolTrue' => false,
66                         ]
67                 ];
68
69                 $configCache = new ConfigCache();
70                 $configCache->load($data);
71                 $configCache->load($override);
72
73                 $this->assertConfigValues($data, $configCache);
74
75                 // override the value
76                 $configCache->load($override, true);
77
78                 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
79                 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
80         }
81
82         /**
83          * Test the loadConfigArray() method with wrong/empty datasets
84          */
85         public function testLoadConfigArrayWrong()
86         {
87                 $configCache = new ConfigCache();
88
89                 // empty dataset
90                 $configCache->load([]);
91                 $this->assertEmpty($configCache->getAll());
92
93                 // wrong dataset
94                 $configCache->load(['system' => 'not_array']);
95                 $this->assertEmpty($configCache->getAll());
96
97                 // incomplete dataset (key is integer ID of the array)
98                 $configCache->load(['system' => ['value']]);
99                 $this->assertEquals('value', $configCache->get('system', 0));
100         }
101
102         /**
103          * Test the getAll() method
104          * @dataProvider dataTests
105          */
106         public function testGetAll($data)
107         {
108                 $configCache = new ConfigCache();
109                 $configCache->load($data);
110
111                 $all = $configCache->getAll();
112
113                 $this->assertContains($data['system'], $all);
114                 $this->assertContains($data['config'], $all);
115         }
116
117         /**
118          * Test the set() and get() method
119          * @dataProvider dataTests
120          */
121         public function testSetGet($data)
122         {
123                 $configCache = new ConfigCache();
124
125                 foreach ($data as $cat => $values) {
126                         foreach ($values as $key => $value) {
127                                 $configCache->set($cat, $key, $value);
128                         }
129                 }
130
131                 $this->assertConfigValues($data, $configCache);
132         }
133
134         /**
135          * Test the get() method without a value
136          */
137         public function testGetEmpty()
138         {
139                 $configCache = new ConfigCache();
140
141                 $this->assertEquals('!<unset>!', $configCache->get('something', 'value'));
142         }
143
144         /**
145          * Test the has() method
146          */
147         public function testHas()
148         {
149                 $configCache = new ConfigCache();
150
151                 $this->assertFalse($configCache->has('system', 'test'));
152
153                 $configCache->set('system', 'test', 'it');
154                 $this->assertTrue($configCache->has('system', 'test'));
155
156                 $this->assertFalse($configCache->has('system', null));
157                 $configCache->set('system', null, 'it');
158                 $this->assertTrue($configCache->has('system', null));
159         }
160
161         /**
162          * Test the delete() method
163          * @dataProvider dataTests
164          */
165         public function testDelete($data)
166         {
167                 $configCache = new ConfigCache($data);
168
169                 foreach ($data as $cat => $values) {
170                         foreach ($values as $key => $value) {
171                                 $configCache->delete($cat, $key);
172                         }
173                 }
174
175                 $this->assertEmpty($configCache->getAll());
176         }
177
178         /**
179          * Test the setP() and getP() methods
180          * @dataProvider dataTests
181          */
182         public function testSetGetP($data)
183         {
184                 $configCache = new ConfigCache();
185                 $uid = 345;
186
187                 foreach ($data as $cat => $values) {
188                         foreach ($values as $key => $value) {
189                                 $configCache->setP($uid, $cat, $key, $value);
190                         }
191                 }
192
193                 $this->assertConfigValues($data, $configCache, $uid);
194         }
195
196
197         /**
198          * Test the deleteP() method
199          * @dataProvider dataTests
200          */
201         public function testDeleteP($data)
202         {
203                 $configCache = new ConfigCache();
204                 $uid = 345;
205
206                 foreach ($data as $cat => $values) {
207                         foreach ($values as $key => $value) {
208                                 $configCache->setP($uid, $cat, $key, $value);
209                         }
210                 }
211
212                 foreach ($data as $cat => $values) {
213                         foreach ($values as $key => $value) {
214                                 $configCache->deleteP($uid, $cat, $key);
215                         }
216                 }
217
218                 $this->assertEmpty($configCache->getAll());
219         }
220
221         /**
222          * Test the hasP() method
223          */
224         public function testHasP()
225         {
226                 $configCache = new ConfigCache();
227                 $uid = 345;
228
229                 $this->assertFalse($configCache->hasP($uid, 'system', 'test'));
230
231                 $configCache->setP($uid, 'system', 'test', 'it');
232                 $this->assertTrue($configCache->hasP($uid, 'system', 'test'));
233
234                 $this->assertFalse($configCache->hasP($uid, 'system', null));
235                 $configCache->setP($uid, 'system', null, 'it');
236                 $this->assertTrue($configCache->hasP($uid, 'system', null));
237         }
238 }