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