ac9fae540b8c45ab89eb0c977d28408b21955f6b
[friendica.git/.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                 $this->assertFalse($configCache->has('system'));
153
154                 $configCache->set('system', 'test', 'it');
155                 $this->assertTrue($configCache->has('system', 'test'));
156                 $this->assertTrue($configCache->has('system'));
157         }
158
159         /**
160          * Test the get() method with a category
161          */
162         public function testGetCat()
163         {
164                 $configCache = new ConfigCache([
165                         'system' => [
166                                 'key1' => 'value1',
167                                 'key2' => 'value2',
168                         ],
169                         'config' => [
170                                 'key3' => 'value3',
171                         ],
172                 ]);
173
174                 $this->assertTrue($configCache->has('system'));
175
176                 $this->assertEquals([
177                         'key1' => 'value1',
178                         'key2' => 'value2',
179                 ], $configCache->get('system'));
180         }
181
182         /**
183          * Test the delete() method
184          * @dataProvider dataTests
185          */
186         public function testDelete($data)
187         {
188                 $configCache = new ConfigCache($data);
189
190                 foreach ($data as $cat => $values) {
191                         foreach ($values as $key => $value) {
192                                 $configCache->delete($cat, $key);
193                         }
194                 }
195
196                 $this->assertEmpty($configCache->getAll());
197         }
198
199         /**
200          * Test the setP() and getP() methods
201          * @dataProvider dataTests
202          */
203         public function testSetGetP($data)
204         {
205                 $configCache = new ConfigCache();
206                 $uid = 345;
207
208                 foreach ($data as $cat => $values) {
209                         foreach ($values as $key => $value) {
210                                 $configCache->setP($uid, $cat, $key, $value);
211                         }
212                 }
213
214                 $this->assertConfigValues($data, $configCache, $uid);
215         }
216
217
218         /**
219          * Test the getP() method with a category
220          */
221         public function testGetPCat()
222         {
223                 $configCache = new ConfigCache();
224                 $uid = 345;
225
226                 $configCache->loadP($uid, [
227                         'system' => [
228                                 'key1' => 'value1',
229                                 'key2' => 'value2',
230                         ],
231                         'config' => [
232                                 'key3' => 'value3',
233                         ],
234                 ]);
235
236                 $this->assertTrue($configCache->hasP($uid,'system'));
237
238                 $this->assertEquals([
239                         'key1' => 'value1',
240                         'key2' => 'value2',
241                 ], $configCache->get($uid, 'system'));
242         }
243
244         /**
245          * Test the deleteP() method
246          * @dataProvider dataTests
247          */
248         public function testDeleteP($data)
249         {
250                 $configCache = new ConfigCache();
251                 $uid = 345;
252
253                 foreach ($data as $cat => $values) {
254                         foreach ($values as $key => $value) {
255                                 $configCache->setP($uid, $cat, $key, $value);
256                         }
257                 }
258
259                 foreach ($data as $cat => $values) {
260                         foreach ($values as $key => $value) {
261                                 $configCache->deleteP($uid, $cat, $key);
262                         }
263                 }
264
265                 $this->assertEmpty($configCache->getAll());
266         }
267
268         /**
269          * Test the hasP() method
270          */
271         public function testHasP()
272         {
273                 $configCache = new ConfigCache();
274                 $uid = 345;
275
276                 $this->assertFalse($configCache->hasP($uid, 'system', 'test'));
277                 $this->assertFalse($configCache->hasP($uid, 'system'));
278
279                 $configCache->setP($uid, 'system', 'test', 'it');
280                 $this->assertTrue($configCache->hasP($uid, 'system', 'test'));
281                 $this->assertTrue($configCache->hasP($uid, 'system'));
282         }
283 }