]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/ConfigurationTest.php
Added string type-hint for get() and a test case for it
[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 load without result
257          */
258         public function testLoadWrong()
259         {
260                 $this->configModel->shouldReceive('isConnected')->andReturn(true)->once();
261                 $this->configModel->shouldReceive('load')->withAnyArgs()->andReturn([])->once();
262
263                 $this->testedConfig = $this->getInstance();
264                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
265
266                 $this->assertEmpty($this->testedConfig->getCache()->getAll());
267         }
268
269         /**
270          * Test the configuration get() and set() methods without adapter
271          *
272          * @dataProvider dataTests
273          */
274         public function testSetGetWithoutDB($data)
275         {
276                 $this->configModel->shouldReceive('isConnected')
277                                   ->andReturn(false)
278                                   ->times(3);
279
280                 $this->testedConfig = $this->getInstance();
281                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
282
283                 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
284
285                 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
286                 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
287         }
288
289         /**
290          * Test the configuration get() and set() methods with a model/db
291          *
292          * @dataProvider dataTests
293          */
294         public function testSetGetWithDB($data)
295         {
296                 $this->configModel->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
297
298                 $this->testedConfig = $this->getInstance();
299                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
300
301                 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
302
303                 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
304                 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
305         }
306
307         /**
308          * Test the configuration get() method with wrong value and no db
309          */
310         public function testGetWrongWithoutDB()
311         {
312                 $this->testedConfig = $this->getInstance();
313                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
314
315                 // without refresh
316                 $this->assertNull($this->testedConfig->get('test', 'it'));
317
318                 /// beware that the cache returns '!<unset>!' and not null for a non existing value
319                 $this->assertNull($this->testedConfig->getCache()->get('test', 'it'));
320
321                 // with default value
322                 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default'));
323
324                 // with default value and refresh
325                 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default', true));
326         }
327
328         /**
329          * Test the configuration get() method with refresh
330          *
331          * @dataProvider dataTests
332          */
333         public function testGetWithRefresh($data)
334         {
335                 $this->configCache->load(['test' => ['it' => 'now']]);
336
337                 $this->testedConfig = $this->getInstance();
338                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
339
340                 // without refresh
341                 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
342                 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
343
344                 // with refresh
345                 $this->assertEquals($data, $this->testedConfig->get('test', 'it', null, true));
346                 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
347
348                 // without refresh and wrong value and default
349                 $this->assertEquals('default', $this->testedConfig->get('test', 'not', 'default'));
350                 $this->assertNull($this->testedConfig->getCache()->get('test', 'not'));
351         }
352
353         /**
354          * Test the configuration delete() method without a model/db
355          *
356          * @dataProvider dataTests
357          */
358         public function testDeleteWithoutDB($data)
359         {
360                 $this->configCache->load(['test' => ['it' => $data]]);
361
362                 $this->testedConfig = $this->getInstance();
363                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
364
365                 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
366                 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
367
368                 $this->assertTrue($this->testedConfig->delete('test', 'it'));
369                 $this->assertNull($this->testedConfig->get('test', 'it'));
370                 $this->assertNull($this->testedConfig->getCache()->get('test', 'it'));
371
372                 $this->assertEmpty($this->testedConfig->getCache()->getAll());
373         }
374
375         /**
376          * Test the configuration delete() method with a model/db
377          */
378         public function testDeleteWithDB()
379         {
380                 $this->configCache->load(['test' => ['it' => 'now', 'quarter' => 'true']]);
381
382                 $this->configModel->shouldReceive('delete')
383                                   ->with('test', 'it')
384                                   ->andReturn(false)
385                                   ->once();
386                 $this->configModel->shouldReceive('delete')
387                                   ->with('test', 'second')
388                                   ->andReturn(true)
389                                   ->once();
390                 $this->configModel->shouldReceive('delete')
391                                   ->with('test', 'third')
392                                   ->andReturn(false)
393                                   ->once();
394                 $this->configModel->shouldReceive('delete')
395                                   ->with('test', 'quarter')
396                                   ->andReturn(true)
397                                   ->once();
398
399                 $this->testedConfig = $this->getInstance();
400                 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
401
402                 // directly set the value to the cache
403                 $this->testedConfig->getCache()->set('test', 'it', 'now');
404
405                 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
406                 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
407
408                 // delete from cache only
409                 $this->assertTrue($this->testedConfig->delete('test', 'it'));
410                 // delete from db only
411                 $this->assertTrue($this->testedConfig->delete('test', 'second'));
412                 // no delete
413                 $this->assertFalse($this->testedConfig->delete('test', 'third'));
414                 // delete both
415                 $this->assertTrue($this->testedConfig->delete('test', 'quarter'));
416
417                 $this->assertEmpty($this->testedConfig->getCache()->getAll());
418         }
419 }