]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/Cache/ConfigCacheTest.php
wrong indent
[friendica.git] / tests / src / Core / Config / Cache / ConfigCacheTest.php
1 <?php
2
3 namespace Friendica\Test\src\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->assertNull($configCache->get('something', 'value'));
142         }
143
144         /**
145          * Test the get() method with a category
146          */
147         public function testGetCat()
148         {
149                 $configCache = new ConfigCache([
150                         'system' => [
151                                 'key1' => 'value1',
152                                 'key2' => 'value2',
153                         ],
154                         'config' => [
155                                 'key3' => 'value3',
156                         ],
157                 ]);
158
159                 $this->assertEquals([
160                         'key1' => 'value1',
161                         'key2' => 'value2',
162                 ], $configCache->get('system'));
163         }
164
165         /**
166          * Test the delete() method
167          * @dataProvider dataTests
168          */
169         public function testDelete($data)
170         {
171                 $configCache = new ConfigCache($data);
172
173                 foreach ($data as $cat => $values) {
174                         foreach ($values as $key => $value) {
175                                 $configCache->delete($cat, $key);
176                         }
177                 }
178
179                 $this->assertEmpty($configCache->getAll());
180         }
181
182         /**
183          * Test the setP() and getP() methods
184          * @dataProvider dataTests
185          */
186         public function testSetGetP($data)
187         {
188                 $configCache = new ConfigCache();
189                 $uid = 345;
190
191                 foreach ($data as $cat => $values) {
192                         foreach ($values as $key => $value) {
193                                 $configCache->setP($uid, $cat, $key, $value);
194                         }
195                 }
196
197                 $this->assertConfigValues($data, $configCache, $uid);
198         }
199
200
201         /**
202          * Test the getP() method with a category
203          */
204         public function testGetPCat()
205         {
206                 $configCache = new ConfigCache();
207                 $uid = 345;
208
209                 $configCache->loadP($uid, [
210                         'system' => [
211                                 'key1' => 'value1',
212                                 'key2' => 'value2',
213                         ],
214                         'config' => [
215                                 'key3' => 'value3',
216                         ],
217                 ]);
218
219                 $this->assertEquals([
220                         'key1' => 'value1',
221                         'key2' => 'value2',
222                 ], $configCache->get($uid, 'system'));
223         }
224
225         /**
226          * Test the deleteP() method
227          * @dataProvider dataTests
228          */
229         public function testDeleteP($data)
230         {
231                 $configCache = new ConfigCache();
232                 $uid = 345;
233
234                 foreach ($data as $cat => $values) {
235                         foreach ($values as $key => $value) {
236                                 $configCache->setP($uid, $cat, $key, $value);
237                         }
238                 }
239
240                 foreach ($data as $cat => $values) {
241                         foreach ($values as $key => $value) {
242                                 $configCache->deleteP($uid, $cat, $key);
243                         }
244                 }
245
246                 $this->assertEmpty($configCache->getAll());
247         }
248
249         /**
250          * Test the keyDiff() method with result
251          * @dataProvider dataTests
252          */
253         public function testKeyDiffWithResult($data)
254         {
255                 $configCache = new ConfigCache($data);
256
257                 $diffConfig = [
258                         'fakeCat' => [
259                                 'fakeKey' => 'value',
260                         ]
261                 ];
262
263                 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
264         }
265
266         /**
267          * Test the keyDiff() method without result
268          * @dataProvider dataTests
269          */
270         public function testKeyDiffWithoutResult($data)
271         {
272                 $configCache = new ConfigCache($data);
273
274                 $diffConfig = $configCache->getAll();
275
276                 $this->assertEmpty($configCache->keyDiff($diffConfig));
277         }
278 }