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