]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/ConfigurationTest.php
Move Preload/JIT Configuration logic from Adapter to Core-Configuration
[friendica.git] / tests / src / Core / Config / ConfigurationTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Config;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\Configuration;
7 use Friendica\Core\Config\JitConfiguration;
8 use Friendica\Model\Config\Config as ConfigModel;
9 use Friendica\Test\MockedTest;
10 use Mockery\MockInterface;
11 use Mockery;
12
13 abstract class ConfigurationTest extends MockedTest
14 {
15         /** @var ConfigModel|MockInterface */
16         protected $configModel;
17
18         /** @var ConfigCache */
19         protected $configCache;
20
21         /** @var Configuration */
22         protected $testedConfig;
23
24         /**
25          * Assert a config tree
26          *
27          * @param string $cat  The category to assert
28          * @param array  $data The result data array
29          */
30         protected function assertConfig(string $cat, array $data)
31         {
32                 $result = $this->testedConfig->getCache()->getAll();
33
34                 $this->assertNotEmpty($result);
35                 $this->assertArrayHasKey($cat, $result);
36                 $this->assertArraySubset($data, $result[$cat]);
37         }
38
39
40         protected function setUp()
41         {
42                 parent::setUp();
43
44                 // Create the config model
45                 $this->configModel = Mockery::mock(ConfigModel::class);
46                 $this->configCache = new ConfigCache();
47         }
48
49         /**
50          * @return Configuration
51          */
52         public abstract function getInstance();
53
54         public function dataTests()
55         {
56                 return [
57                         'string'       => ['data' => 'it'],
58                         'boolTrue'     => ['data' => true],
59                         'boolFalse'    => ['data' => false],
60                         'integer'      => ['data' => 235],
61                         'decimal'      => ['data' => 2.456],
62                         'array'        => ['data' => ['1', 2, '3', true, false]],
63                         'boolIntTrue'  => ['data' => 1],
64                         'boolIntFalse' => ['Data' => 0],
65                 ];
66         }
67
68         public function dataConfigLoad()
69         {
70                 $data = [
71                         'system' => [
72                                 'key1' => 'value1',
73                                 'key2' => 'value2',
74                                 'key3' => 'value3',
75                         ],
76                         'config' => [
77                                 'key1' => 'value1a',
78                                 'key4' => 'value4',
79                         ],
80                         'other'  => [
81                                 'key5' => 'value5',
82                                 'key6' => 'value6',
83                         ],
84                 ];
85
86                 return [
87                         'system' => [
88                                 'data'         => $data,
89                                 'possibleCats' => [
90                                         'system',
91                                         'config',
92                                         'other'
93                                 ],
94                                 'load'         => [
95                                         'system',
96                                 ],
97                         ],
98                         'other'  => [
99                                 'data'         => $data,
100                                 'possibleCats' => [
101                                         'system',
102                                         'config',
103                                         'other'
104                                 ],
105                                 'load'         => [
106                                         'other',
107                                 ],
108                         ],
109                         'config' => [
110                                 'data'         => $data,
111                                 'possibleCats' => [
112                                         'system',
113                                         'config',
114                                         'other'
115                                 ],
116                                 'load'         => [
117                                         'config',
118                                 ],
119                         ],
120                         'all'    => [
121                                 'data'         => $data,
122                                 'possibleCats' => [
123                                         'system',
124                                         'config',
125                                         'other'
126                                 ],
127                                 'load'         => [
128                                         'system',
129                                         'config',
130                                         'other'
131                                 ],
132                         ],
133                 ];
134         }
135
136         /**
137          * Test the configuration initialization
138          */
139         public function testSetUp(array $data)
140         {
141                 $this->configModel->shouldReceive('isConnected')
142                                   ->andReturn(true)
143                                   ->once();
144
145                 $this->testedConfig = $this->getInstance();
146                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
147
148                 // assert config is loaded everytime
149                 $this->assertConfig('config', $data['config']);
150         }
151
152         /**
153          * Test the configuration load() method
154          */
155         public function testLoad(array $data, array $possibleCats, array $load)
156         {
157                 $this->testedConfig = $this->getInstance();
158                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
159
160                 foreach ($load as $loadedCats) {
161                         $this->testedConfig->load($loadedCats);
162                 }
163
164                 // Assert at least loaded cats are loaded
165                 foreach ($load as $loadedCats) {
166                         $this->assertConfig($loadedCats, $data[$loadedCats]);
167                 }
168         }
169
170         public function dataDoubleLoad()
171         {
172                 return [
173                         'config' => [
174                                 'data1'  => [
175                                         'config' => [
176                                                 'key1' => 'value1',
177                                                 'key2' => 'value2',
178                                         ],
179                                 ],
180                                 'data2'  => [
181                                         'config' => [
182                                                 'key1' => 'overwritten!',
183                                                 'key3' => 'value3',
184                                         ],
185                                 ],
186                                 'expect' => [
187                                         'config' => [
188                                                 // load should overwrite values everytime!
189                                                 'key1' => 'overwritten!',
190                                                 'key2' => 'value2',
191                                                 'key3' => 'value3',
192                                         ],
193                                 ],
194                         ],
195                         'other'  => [
196                                 'data1'  => [
197                                         'config' => [
198                                                 'key12' => 'data4',
199                                                 'key45' => 7,
200                                         ],
201                                         'other'  => [
202                                                 'key1' => 'value1',
203                                                 'key2' => 'value2',
204                                         ],
205                                 ],
206                                 'data2'  => [
207                                         'other'  => [
208                                                 'key1' => 'overwritten!',
209                                                 'key3' => 'value3',
210                                         ],
211                                         'config' => [
212                                                 'key45' => 45,
213                                                 'key52' => true,
214                                         ]
215                                 ],
216                                 'expect' => [
217                                         'other'  => [
218                                                 // load should overwrite values everytime!
219                                                 'key1' => 'overwritten!',
220                                                 'key2' => 'value2',
221                                                 'key3' => 'value3',
222                                         ],
223                                         'config' => [
224                                                 'key12' => 'data4',
225                                                 'key45' => 45,
226                                                 'key52' => true,
227                                         ],
228                                 ],
229                         ],
230                 ];
231         }
232
233         /**
234          * Test the configuration load() method with overwrite
235          */
236         public function testCacheLoadDouble(array $data1, array $data2, array $expect)
237         {
238                 $this->testedConfig = $this->getInstance();
239                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
240
241                 foreach ($data1 as $cat => $data) {
242                         $this->testedConfig->load($cat);
243                 }
244
245                 // Assert at least loaded cats are loaded
246                 foreach ($data1 as $cat => $data) {
247                         $this->assertConfig($cat, $data);
248                 }
249
250                 foreach ($data2 as $cat => $data) {
251                         $this->testedConfig->load($cat);
252                 }
253         }
254
255         /**
256          * Test the configuration get() and set() methods without adapter
257          *
258          * @dataProvider dataTests
259          */
260         public function testSetGetWithoutDB($data)
261         {
262                 $this->configModel->shouldReceive('isConnected')
263                                   ->andReturn(false)
264                                   ->times(3);
265
266                 $this->testedConfig = $this->getInstance();
267                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
268
269                 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
270
271                 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
272                 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
273         }
274
275         /**
276          * Test the configuration get() and set() methods with a model/db
277          *
278          * @dataProvider dataTests
279          */
280         public function testSetGetWithDB($data)
281         {
282                 $this->configModel->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
283
284                 $this->testedConfig = $this->getInstance();
285                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
286
287                 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
288
289                 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
290                 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
291         }
292
293         /**
294          * Test the configuration get() method with wrong value and no db
295          */
296         public function testGetWrongWithoutDB()
297         {
298                 $this->testedConfig = $this->getInstance();
299                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
300
301                 // without refresh
302                 $this->assertNull($this->testedConfig->get('test', 'it'));
303
304                 /// beware that the cache returns '!<unset>!' and not null for a non existing value
305                 $this->assertNull($this->testedConfig->getCache()->get('test', 'it'));
306
307                 // with default value
308                 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default'));
309
310                 // with default value and refresh
311                 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default', true));
312         }
313
314         /**
315          * Test the configuration get() method with refresh
316          *
317          * @dataProvider dataTests
318          */
319         public function testGetWithRefresh($data)
320         {
321                 $this->configCache->load(['test' => ['it' => 'now']]);
322
323                 $this->testedConfig = $this->getInstance();
324                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
325
326                 // without refresh
327                 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
328                 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
329
330                 // with refresh
331                 $this->assertEquals($data, $this->testedConfig->get('test', 'it', null, true));
332                 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
333
334                 // without refresh and wrong value and default
335                 $this->assertEquals('default', $this->testedConfig->get('test', 'not', 'default'));
336                 $this->assertNull($this->testedConfig->getCache()->get('test', 'not'));
337         }
338
339         /**
340          * Test the configuration delete() method without a model/db
341          *
342          * @dataProvider dataTests
343          */
344         public function testDeleteWithoutDB($data)
345         {
346                 $this->configCache->load(['test' => ['it' => $data]]);
347
348                 $this->testedConfig = $this->getInstance();
349                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
350
351                 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
352                 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
353
354                 $this->assertTrue($this->testedConfig->delete('test', 'it'));
355                 $this->assertNull($this->testedConfig->get('test', 'it'));
356                 $this->assertNull($this->testedConfig->getCache()->get('test', 'it'));
357
358                 $this->assertEmpty($this->testedConfig->getCache()->getAll());
359         }
360
361         /**
362          * Test the configuration delete() method with a model/db
363          */
364         public function testDeleteWithDB()
365         {
366                 $this->configCache->load(['test' => ['it' => 'now', 'quarter' => 'true']]);
367
368                 $this->configModel->shouldReceive('delete')
369                                   ->with('test', 'it')
370                                   ->andReturn(false)
371                                   ->once();
372                 $this->configModel->shouldReceive('delete')
373                                   ->with('test', 'second')
374                                   ->andReturn(true)
375                                   ->once();
376                 $this->configModel->shouldReceive('delete')
377                                   ->with('test', 'third')
378                                   ->andReturn(false)
379                                   ->once();
380                 $this->configModel->shouldReceive('delete')
381                                   ->with('test', 'quarter')
382                                   ->andReturn(true)
383                                   ->once();
384
385                 $this->testedConfig = $this->getInstance();
386                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
387
388                 // directly set the value to the cache
389                 $this->testedConfig->getCache()->set('test', 'it', 'now');
390
391                 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
392                 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
393
394                 // delete from cache only
395                 $this->assertTrue($this->testedConfig->delete('test', 'it'));
396                 // delete from db only
397                 $this->assertTrue($this->testedConfig->delete('test', 'second'));
398                 // no delete
399                 $this->assertFalse($this->testedConfig->delete('test', 'third'));
400                 // delete both
401                 $this->assertTrue($this->testedConfig->delete('test', 'quarter'));
402
403                 $this->assertEmpty($this->testedConfig->getCache()->getAll());
404         }
405 }