3 namespace Friendica\Test\src\Core\Config;
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;
12 abstract class ConfigurationTest extends MockedTest
14 /** @var ConfigModel|MockInterface */
15 protected $configModel;
17 /** @var ConfigCache */
18 protected $configCache;
20 /** @var IConfiguration */
21 protected $testedConfig;
24 * Assert a config tree
26 * @param string $cat The category to assert
27 * @param array $data The result data array
29 protected function assertConfig(string $cat, array $data)
31 $result = $this->testedConfig->getCache()->getAll();
33 $this->assertNotEmpty($result);
34 $this->assertArrayHasKey($cat, $result);
35 $this->assertArraySubset($data, $result[$cat]);
39 protected function setUp()
43 // Create the config model
44 $this->configModel = Mockery::mock(ConfigModel::class);
45 $this->configCache = new ConfigCache();
49 * @return IConfiguration
51 public abstract function getInstance();
53 public function dataTests()
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],
67 public function dataConfigLoad()
136 * Test the configuration initialization
138 public function testSetUp(array $data)
140 $this->configModel->shouldReceive('isConnected')
144 $this->testedConfig = $this->getInstance();
145 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
147 // assert config is loaded everytime
148 $this->assertConfig('config', $data['config']);
152 * Test the configuration load() method
154 public function testLoad(array $data, array $possibleCats, array $load)
156 $this->testedConfig = $this->getInstance();
157 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
159 foreach ($load as $loadedCats) {
160 $this->testedConfig->load($loadedCats);
163 // Assert at least loaded cats are loaded
164 foreach ($load as $loadedCats) {
165 $this->assertConfig($loadedCats, $data[$loadedCats]);
169 public function dataDoubleLoad()
181 'key1' => 'overwritten!',
187 // load should overwrite values everytime!
188 'key1' => 'overwritten!',
207 'key1' => 'overwritten!',
217 // load should overwrite values everytime!
218 'key1' => 'overwritten!',
233 * Test the configuration load() method with overwrite
235 public function testCacheLoadDouble(array $data1, array $data2, array $expect)
237 $this->testedConfig = $this->getInstance();
238 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
240 foreach ($data1 as $cat => $data) {
241 $this->testedConfig->load($cat);
244 // Assert at least loaded cats are loaded
245 foreach ($data1 as $cat => $data) {
246 $this->assertConfig($cat, $data);
249 foreach ($data2 as $cat => $data) {
250 $this->testedConfig->load($cat);
255 * Test the configuration load without result
257 public function testLoadWrong()
259 $this->configModel->shouldReceive('isConnected')->andReturn(true)->once();
260 $this->configModel->shouldReceive('load')->withAnyArgs()->andReturn([])->once();
262 $this->testedConfig = $this->getInstance();
263 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
265 $this->assertEmpty($this->testedConfig->getCache()->getAll());
269 * Test the configuration get() and set() methods without adapter
271 * @dataProvider dataTests
273 public function testSetGetWithoutDB($data)
275 $this->configModel->shouldReceive('isConnected')
279 $this->testedConfig = $this->getInstance();
280 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
282 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
284 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
285 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
289 * Test the configuration get() and set() methods with a model/db
291 * @dataProvider dataTests
293 public function testSetGetWithDB($data)
295 $this->configModel->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
297 $this->testedConfig = $this->getInstance();
298 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
300 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
302 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
303 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
307 * Test the configuration get() method with wrong value and no db
309 public function testGetWrongWithoutDB()
311 $this->testedConfig = $this->getInstance();
312 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
315 $this->assertNull($this->testedConfig->get('test', 'it'));
317 /// beware that the cache returns '!<unset>!' and not null for a non existing value
318 $this->assertNull($this->testedConfig->getCache()->get('test', 'it'));
320 // with default value
321 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default'));
323 // with default value and refresh
324 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default', true));
328 * Test the configuration get() method with refresh
330 * @dataProvider dataTests
332 public function testGetWithRefresh($data)
334 $this->configCache->load(['test' => ['it' => 'now']]);
336 $this->testedConfig = $this->getInstance();
337 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
340 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
341 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
344 $this->assertEquals($data, $this->testedConfig->get('test', 'it', null, true));
345 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
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'));
353 * Test the configuration delete() method without a model/db
355 * @dataProvider dataTests
357 public function testDeleteWithoutDB($data)
359 $this->configCache->load(['test' => ['it' => $data]]);
361 $this->testedConfig = $this->getInstance();
362 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
364 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
365 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
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'));
371 $this->assertEmpty($this->testedConfig->getCache()->getAll());
375 * Test the configuration delete() method with a model/db
377 public function testDeleteWithDB()
379 $this->configCache->load(['test' => ['it' => 'now', 'quarter' => 'true']]);
381 $this->configModel->shouldReceive('delete')
385 $this->configModel->shouldReceive('delete')
386 ->with('test', 'second')
389 $this->configModel->shouldReceive('delete')
390 ->with('test', 'third')
393 $this->configModel->shouldReceive('delete')
394 ->with('test', 'quarter')
398 $this->testedConfig = $this->getInstance();
399 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
401 // directly set the value to the cache
402 $this->testedConfig->getCache()->set('test', 'it', 'now');
404 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
405 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
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'));
412 $this->assertFalse($this->testedConfig->delete('test', 'third'));
414 $this->assertTrue($this->testedConfig->delete('test', 'quarter'));
416 $this->assertEmpty($this->testedConfig->getCache()->getAll());