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 get() and set() methods without adapter
258 * @dataProvider dataTests
260 public function testSetGetWithoutDB($data)
262 $this->configModel->shouldReceive('isConnected')
266 $this->testedConfig = $this->getInstance();
267 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
269 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
271 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
272 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
276 * Test the configuration get() and set() methods with a model/db
278 * @dataProvider dataTests
280 public function testSetGetWithDB($data)
282 $this->configModel->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
284 $this->testedConfig = $this->getInstance();
285 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
287 $this->assertTrue($this->testedConfig->set('test', 'it', $data));
289 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
290 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
294 * Test the configuration get() method with wrong value and no db
296 public function testGetWrongWithoutDB()
298 $this->testedConfig = $this->getInstance();
299 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
302 $this->assertNull($this->testedConfig->get('test', 'it'));
304 /// beware that the cache returns '!<unset>!' and not null for a non existing value
305 $this->assertNull($this->testedConfig->getCache()->get('test', 'it'));
307 // with default value
308 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default'));
310 // with default value and refresh
311 $this->assertEquals('default', $this->testedConfig->get('test', 'it', 'default', true));
315 * Test the configuration get() method with refresh
317 * @dataProvider dataTests
319 public function testGetWithRefresh($data)
321 $this->configCache->load(['test' => ['it' => 'now']]);
323 $this->testedConfig = $this->getInstance();
324 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
327 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
328 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
331 $this->assertEquals($data, $this->testedConfig->get('test', 'it', null, true));
332 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
334 // without refresh and wrong value and default
335 $this->assertEquals('default', $this->testedConfig->get('test', 'not', 'default'));
336 $this->assertNull($this->testedConfig->getCache()->get('test', 'not'));
340 * Test the configuration delete() method without a model/db
342 * @dataProvider dataTests
344 public function testDeleteWithoutDB($data)
346 $this->configCache->load(['test' => ['it' => $data]]);
348 $this->testedConfig = $this->getInstance();
349 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
351 $this->assertEquals($data, $this->testedConfig->get('test', 'it'));
352 $this->assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
354 $this->assertTrue($this->testedConfig->delete('test', 'it'));
355 $this->assertNull($this->testedConfig->get('test', 'it'));
356 $this->assertNull($this->testedConfig->getCache()->get('test', 'it'));
358 $this->assertEmpty($this->testedConfig->getCache()->getAll());
362 * Test the configuration delete() method with a model/db
364 public function testDeleteWithDB()
366 $this->configCache->load(['test' => ['it' => 'now', 'quarter' => 'true']]);
368 $this->configModel->shouldReceive('delete')
372 $this->configModel->shouldReceive('delete')
373 ->with('test', 'second')
376 $this->configModel->shouldReceive('delete')
377 ->with('test', 'third')
380 $this->configModel->shouldReceive('delete')
381 ->with('test', 'quarter')
385 $this->testedConfig = $this->getInstance();
386 $this->assertInstanceOf(ConfigCache::class, $this->testedConfig->getCache());
388 // directly set the value to the cache
389 $this->testedConfig->getCache()->set('test', 'it', 'now');
391 $this->assertEquals('now', $this->testedConfig->get('test', 'it'));
392 $this->assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
394 // delete from cache only
395 $this->assertTrue($this->testedConfig->delete('test', 'it'));
396 // delete from db only
397 $this->assertTrue($this->testedConfig->delete('test', 'second'));
399 $this->assertFalse($this->testedConfig->delete('test', 'third'));
401 $this->assertTrue($this->testedConfig->delete('test', 'quarter'));
403 $this->assertEmpty($this->testedConfig->getCache()->getAll());