3 namespace Friendica\Test\src\Core\Config;
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;
13 abstract class ConfigurationTest extends MockedTest
15 /** @var ConfigModel|MockInterface */
16 protected $configModel;
18 /** @var ConfigCache */
19 protected $configCache;
21 /** @var Configuration */
22 protected $testedConfig;
25 * Assert a config tree
27 * @param string $cat The category to assert
28 * @param array $data The result data array
30 protected function assertConfig(string $cat, array $data)
32 $result = $this->testedConfig->getCache()->getAll();
34 $this->assertNotEmpty($result);
35 $this->assertArrayHasKey($cat, $result);
36 $this->assertArraySubset($data, $result[$cat]);
40 protected function setUp()
44 // Create the config model
45 $this->configModel = Mockery::mock(ConfigModel::class);
46 $this->configCache = new ConfigCache();
50 * @return Configuration
52 public abstract function getInstance();
54 public function dataTests()
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],
68 public function dataConfigLoad()
137 * Test the configuration initialization
139 public function testSetUp(array $data)
141 $this->configModel->shouldReceive('isConnected')
145 $this->testedConfig = $this->getInstance();
146 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
148 // assert config is loaded everytime
149 $this->assertConfig('config', $data['config']);
153 * Test the configuration load() method
155 public function testLoad(array $data, array $possibleCats, array $load)
157 $this->testedConfig = $this->getInstance();
158 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
160 foreach ($load as $loadedCats) {
161 $this->testedConfig->load($loadedCats);
164 // Assert at least loaded cats are loaded
165 foreach ($load as $loadedCats) {
166 $this->assertConfig($loadedCats, $data[$loadedCats]);
170 public function dataDoubleLoad()
182 'key1' => 'overwritten!',
188 // load should overwrite values everytime!
189 'key1' => 'overwritten!',
208 'key1' => 'overwritten!',
218 // load should overwrite values everytime!
219 'key1' => 'overwritten!',
234 * Test the configuration load() method with overwrite
236 public function testCacheLoadDouble(array $data1, array $data2, array $expect)
238 $this->testedConfig = $this->getInstance();
239 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
241 foreach ($data1 as $cat => $data) {
242 $this->testedConfig->load($cat);
245 // Assert at least loaded cats are loaded
246 foreach ($data1 as $cat => $data) {
247 $this->assertConfig($cat, $data);
250 foreach ($data2 as $cat => $data) {
251 $this->testedConfig->load($cat);
256 * Test the configuration load without result
258 public function testLoadWrong()
260 $this->configModel->shouldReceive('isConnected')->andReturn(true)->once();
261 $this->configModel->shouldReceive('load')->withAnyArgs()->andReturn([])->once();
263 $this->testedConfig = $this->getInstance();
264 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
266 $this->assertEmpty($this->testedConfig->getCache()->getAll());
270 * Test the configuration get() and set() methods without adapter
272 * @dataProvider dataTests
274 public function testSetGetWithoutDB($data)
276 $this->configModel->shouldReceive('isConnected')
280 $this->testedConfig = $this->getInstance();
281 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
283 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
285 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
286 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
290 * Test the configuration get() and set() methods with a model/db
292 * @dataProvider dataTests
294 public function testSetGetWithDB($data)
296 $this->configModel->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
298 $this->testedConfig = $this->getInstance();
299 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
301 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
303 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
304 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
308 * Test the configuration get() method with wrong value and no db
310 public function testGetWrongWithoutDB()
312 $this->testedConfig = $this->getInstance();
313 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
316 $this->assertNull($this->testedConfig->get('test', 'it'));
318 /// beware that the cache returns '!<unset>!' and not null for a non existing value
319 $this->assertNull($this->testedConfig->getCache()->get('test', 'it'));
321 // with default value
322 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default'));
324 // with default value and refresh
325 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default', true));
329 * Test the configuration get() method with refresh
331 * @dataProvider dataTests
333 public function testGetWithRefresh($data)
335 $this->configCache->load(['test' => ['it' => 'now']]);
337 $this->testedConfig = $this->getInstance();
338 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
341 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
342 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
345 $this->assertEquals($data, $this->testedConfig->get('test', 'it', null, true));
346 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
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'));
354 * Test the configuration delete() method without a model/db
356 * @dataProvider dataTests
358 public function testDeleteWithoutDB($data)
360 $this->configCache->load(['test' => ['it' => $data]]);
362 $this->testedConfig = $this->getInstance();
363 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
365 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
366 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
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'));
372 $this->assertEmpty($this->testedConfig->getCache()->getAll());
376 * Test the configuration delete() method with a model/db
378 public function testDeleteWithDB()
380 $this->configCache->load(['test' => ['it' => 'now', 'quarter' => 'true']]);
382 $this->configModel->shouldReceive('delete')
386 $this->configModel->shouldReceive('delete')
387 ->with('test', 'second')
390 $this->configModel->shouldReceive('delete')
391 ->with('test', 'third')
394 $this->configModel->shouldReceive('delete')
395 ->with('test', 'quarter')
399 $this->testedConfig = $this->getInstance();
400 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
402 // directly set the value to the cache
403 $this->testedConfig->getCache()->set('test', 'it', 'now');
405 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
406 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
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'));
413 $this->assertFalse($this->testedConfig->delete('test', 'third'));
415 $this->assertTrue($this->testedConfig->delete('test', 'quarter'));
417 $this->assertEmpty($this->testedConfig->getCache()->getAll());