3 namespace Friendica\Test\src\Core\Config;
5 use Friendica\Core\Config\Cache\PConfigCache;
6 use Friendica\Core\Config\PConfiguration;
7 use Friendica\Model\Config\PConfig as PConfigModel;
8 use Friendica\Test\MockedTest;
10 use Mockery\MockInterface;
12 abstract class PConfigurationTest extends MockedTest
14 /** @var PConfigModel|MockInterface */
15 protected $configModel;
17 /** @var PConfigCache */
18 protected $configCache;
20 /** @var PConfiguration */
21 protected $testedConfig;
24 * Assert a config tree
26 * @param int $uid The uid to assert
27 * @param string $cat The category to assert
28 * @param array $data The result data array
30 protected function assertConfig(int $uid, string $cat, array $data)
32 $result = $this->testedConfig->getCache()->getAll();
34 $this->assertNotEmpty($result);
35 $this->assertArrayHasKey($uid, $result);
36 $this->assertArrayHasKey($cat, $result[$uid]);
37 $this->assertArraySubset($data, $result[$uid][$cat]);
41 protected function setUp()
45 // Create the config model
46 $this->configModel = Mockery::mock(PConfigModel::class);
47 $this->configCache = new PConfigCache();
51 * @return PConfiguration
53 public abstract function getInstance();
55 public function dataTests()
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],
69 public function dataConfigLoad()
142 * Test the configuration initialization
143 * @dataProvider dataConfigLoad
145 public function testSetUp(int $uid, array $data)
147 $this->testedConfig = $this->getInstance();
148 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
150 $this->assertEmpty($this->testedConfig->getCache()->getAll());
154 * Test the configuration load() method
156 public function testLoad(int $uid, array $data, array $possibleCats, array $load)
158 $this->testedConfig = $this->getInstance();
159 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
161 foreach ($load as $loadedCats) {
162 $this->testedConfig->load($uid, $loadedCats);
165 // Assert at least loaded cats are loaded
166 foreach ($load as $loadedCats) {
167 $this->assertConfig($uid, $loadedCats, $data[$loadedCats]);
171 public function dataDoubleLoad()
184 'key1' => 'overwritten!',
190 // load should overwrite values everytime!
191 'key1' => 'overwritten!',
211 'key1' => 'overwritten!',
221 // load should overwrite values everytime!
222 'key1' => 'overwritten!',
237 * Test the configuration load() method with overwrite
239 public function testCacheLoadDouble(int $uid, array $data1, array $data2, array $expect)
241 $this->testedConfig = $this->getInstance();
242 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
244 foreach ($data1 as $cat => $data) {
245 $this->testedConfig->load($uid, $cat);
248 // Assert at least loaded cats are loaded
249 foreach ($data1 as $cat => $data) {
250 $this->assertConfig($uid, $cat, $data);
253 foreach ($data2 as $cat => $data) {
254 $this->testedConfig->load($uid, $cat);
259 * Test the configuration get() and set() methods without adapter
261 * @dataProvider dataTests
263 public function testSetGetWithoutDB(int $uid, $data)
265 $this->testedConfig = $this->getInstance();
266 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
268 $this->assertTrue($this->testedConfig->set($uid, 'test', 'it', $data));
270 $this->assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
271 $this->assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
275 * Test the configuration get() and set() methods with a model/db
277 * @dataProvider dataTests
279 public function testSetGetWithDB(int $uid, $data)
281 $this->configModel->shouldReceive('set')
282 ->with($uid, 'test', 'it', $data)
286 $this->testedConfig = $this->getInstance();
287 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
289 $this->assertTrue($this->testedConfig->set($uid, 'test', 'it', $data));
291 $this->assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
292 $this->assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
296 * Test the configuration get() method with wrong value and no db
298 public function testGetWrongWithoutDB()
300 $this->testedConfig = $this->getInstance();
301 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
304 $this->assertNull($this->testedConfig->get(0, 'test', 'it'));
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'));
309 // with default value
310 $this->assertEquals('default', $this->testedConfig->get(0, 'test', 'it', 'default'));
312 // with default value and refresh
313 $this->assertEquals('default', $this->testedConfig->get(0, 'test', 'it', 'default', true));
317 * Test the configuration get() method with refresh
319 * @dataProvider dataTests
321 public function testGetWithRefresh(int $uid, $data)
323 $this->configCache->load($uid, ['test' => ['it' => 'now']]);
325 $this->testedConfig = $this->getInstance();
326 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
329 $this->assertEquals('now', $this->testedConfig->get($uid, 'test', 'it'));
330 $this->assertEquals('now', $this->testedConfig->getCache()->get($uid, 'test', 'it'));
333 $this->assertEquals($data, $this->testedConfig->get($uid, 'test', 'it', null, true));
334 $this->assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
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'));
342 * Test the configuration delete() method without a model/db
344 * @dataProvider dataTests
346 public function testDeleteWithoutDB(int $uid, $data)
348 $this->configCache->load($uid, ['test' => ['it' => $data]]);
350 $this->testedConfig = $this->getInstance();
351 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
353 $this->assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
354 $this->assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
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'));
360 $this->assertEmpty($this->testedConfig->getCache()->getAll());
364 * Test the configuration delete() method with a model/db
366 public function testDeleteWithDB()
368 $this->configCache->load(0, ['test' => ['it' => 'now', 'quarter' => 'true']]);
370 $this->configModel->shouldReceive('delete')
371 ->with(0, 'test', 'it')
374 $this->configModel->shouldReceive('delete')
375 ->with(0, 'test', 'second')
378 $this->configModel->shouldReceive('delete')
379 ->with(0, 'test', 'third')
382 $this->configModel->shouldReceive('delete')
383 ->with(0, 'test', 'quarter')
387 $this->testedConfig = $this->getInstance();
388 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
390 // directly set the value to the cache
391 $this->testedConfig->getCache()->set(0, 'test', 'it', 'now');
393 $this->assertEquals('now', $this->testedConfig->get(0, 'test', 'it'));
394 $this->assertEquals('now', $this->testedConfig->getCache()->get(0, 'test', 'it'));
396 // delete from cache only
397 $this->assertTrue($this->testedConfig->delete(0, 'test', 'it'));
398 // delete from db only
399 $this->assertTrue($this->testedConfig->delete(0, 'test', 'second'));
401 $this->assertFalse($this->testedConfig->delete(0, 'test', 'third'));
403 $this->assertTrue($this->testedConfig->delete(0, 'test', 'quarter'));
405 $this->assertEmpty($this->testedConfig->getCache()->getAll());
408 public function dataMultiUid()
439 * Test if multiple uids for caching are usable without errors
440 * @dataProvider dataMultiUid
442 public function testMultipleUidsWithCache(array $data1, array $data2)
444 $this->configCache->load($data1['uid'], $data1['data']);
445 $this->configCache->load($data2['uid'], $data2['data']);
447 $this->testedConfig = $this->getInstance();
448 $this->assertInstanceOf(PConfigCache::class, $this->testedConfig->getCache());
450 $this->assertConfig($data1['uid'], 'cat1', $data1['data']['cat1']);
451 $this->assertConfig($data1['uid'], 'cat2', $data1['data']['cat2']);
452 $this->assertConfig($data2['uid'], 'cat1', $data2['data']['cat1']);
453 $this->assertConfig($data2['uid'], 'cat2', $data2['data']['cat2']);
457 * Test when using an invalid UID
458 * @todo check it the clean way before using the config class
460 public function testInvalidUid()
465 $this->testedConfig = $this->getInstance();
467 $this->assertNull($this->testedConfig->get($uid, 'cat1', 'cat2'));
468 $this->assertEquals('fallback!', $this->testedConfig->get($uid, 'cat1', 'cat2', 'fallback!'));
470 $this->assertFalse($this->testedConfig->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
471 $this->assertFalse($this->testedConfig->delete($uid, 'cat1', 'key1'));