2019-02-10 18:52:21 +00:00
|
|
|
<?php
|
|
|
|
|
2019-02-27 11:32:56 +00:00
|
|
|
namespace Friendica\Test\src\Core\Config;
|
2019-02-10 18:52:21 +00:00
|
|
|
|
2019-02-17 20:41:45 +00:00
|
|
|
use Friendica\Core\Config\Adapter\IPConfigAdapter;
|
2019-07-12 20:38:50 +00:00
|
|
|
use Friendica\Core\Config\Cache\PConfigCache;
|
2019-02-10 18:52:21 +00:00
|
|
|
use Friendica\Core\Config\PConfiguration;
|
|
|
|
use Friendica\Test\MockedTest;
|
|
|
|
|
|
|
|
class PConfigurationTest extends MockedTest
|
|
|
|
{
|
2019-02-11 20:36:26 +00:00
|
|
|
public function dataTests()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'string' => ['data' => 'it'],
|
|
|
|
'boolTrue' => ['data' => true],
|
|
|
|
'boolFalse' => ['data' => false],
|
|
|
|
'integer' => ['data' => 235],
|
|
|
|
'decimal' => ['data' => 2.456],
|
|
|
|
'array' => ['data' => ['1', 2, '3', true, false]],
|
|
|
|
'boolIntTrue' => ['data' => 1],
|
|
|
|
'boolIntFalse' => ['Data' => 0],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-02-10 18:52:21 +00:00
|
|
|
/**
|
2019-02-11 20:36:26 +00:00
|
|
|
* Test the configuration load() method
|
2019-02-10 18:52:21 +00:00
|
|
|
*/
|
|
|
|
public function testCacheLoad()
|
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
|
2019-02-10 18:52:21 +00:00
|
|
|
// expected loading
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('load')
|
|
|
|
->with($uid, 'testing')
|
|
|
|
->andReturn(['testing' => ['test' => 'it']])
|
|
|
|
->once();
|
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->once();
|
2019-02-10 18:52:21 +00:00
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
$configuration->load($uid, 'testing');
|
|
|
|
|
|
|
|
$this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-11 20:36:26 +00:00
|
|
|
* Test the configuration load() method with overwrite
|
2019-02-10 18:52:21 +00:00
|
|
|
*/
|
|
|
|
public function testCacheLoadDouble()
|
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
|
2019-02-10 18:52:21 +00:00
|
|
|
// expected loading
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('load')->with($uid, 'testing')->andReturn(['testing' => ['test' => 'it']])->once();
|
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->twice();
|
2019-02-10 18:52:21 +00:00
|
|
|
// expected next loading
|
|
|
|
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
|
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
$configuration->load($uid, 'testing');
|
|
|
|
|
|
|
|
$this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
|
|
|
|
|
|
|
|
$configuration->load($uid, 'testing');
|
|
|
|
|
|
|
|
$this->assertEquals('again', $configuration->get($uid, 'testing', 'test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-11 20:36:26 +00:00
|
|
|
* Test the configuration get() and set() methods without adapter
|
|
|
|
* @dataProvider dataTests
|
2019-02-10 18:52:21 +00:00
|
|
|
*/
|
2019-02-11 20:36:26 +00:00
|
|
|
public function testSetGetWithoutDB($data)
|
2019-02-10 18:52:21 +00:00
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(2);
|
2019-02-10 18:52:21 +00:00
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
|
2019-02-11 20:36:26 +00:00
|
|
|
$this->assertTrue($configuration->set($uid, 'test', 'it', $data));
|
2019-02-10 18:52:21 +00:00
|
|
|
|
2019-02-11 20:36:26 +00:00
|
|
|
$this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
|
2019-02-10 18:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-11 20:36:26 +00:00
|
|
|
* Test the configuration get() and set() methods with adapter
|
|
|
|
* @dataProvider dataTests
|
2019-02-10 18:52:21 +00:00
|
|
|
*/
|
2019-02-11 20:36:26 +00:00
|
|
|
public function testSetGetWithDB($data)
|
2019-02-10 18:52:21 +00:00
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(2);
|
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
|
|
|
|
$configAdapter->shouldReceive('set')->with($uid, 'test', 'it', $data)->andReturn(true)->once();
|
2019-02-10 18:52:21 +00:00
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
|
2019-02-11 20:36:26 +00:00
|
|
|
$this->assertTrue($configuration->set($uid, 'test', 'it', $data));
|
2019-02-10 18:52:21 +00:00
|
|
|
|
2019-02-11 20:36:26 +00:00
|
|
|
$this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
|
2019-02-10 18:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the configuration get() method with wrong value and no db
|
|
|
|
*/
|
|
|
|
public function testGetWrongWithoutDB()
|
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-10 18:52:21 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
|
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
|
|
|
|
// without refresh
|
|
|
|
$this->assertNull($configuration->get($uid, 'test', 'it'));
|
|
|
|
|
|
|
|
// with default value
|
|
|
|
$this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default'));
|
|
|
|
|
|
|
|
// with default value and refresh
|
|
|
|
$this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default', true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the configuration get() method with refresh
|
2019-02-11 20:36:26 +00:00
|
|
|
* @dataProvider dataTests
|
2019-02-10 18:52:21 +00:00
|
|
|
*/
|
2019-02-11 20:36:26 +00:00
|
|
|
public function testGetWithRefresh($data)
|
2019-02-10 18:52:21 +00:00
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
|
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
|
2019-02-10 18:52:21 +00:00
|
|
|
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('now')->once();
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->twice();
|
|
|
|
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn($data)->once();
|
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'not')->andReturn(false)->once();
|
2019-02-22 23:09:57 +00:00
|
|
|
$configAdapter->shouldReceive('get')->with($uid, 'test', 'not')->andReturn(null)->once();
|
2019-02-10 18:52:21 +00:00
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
|
|
|
|
// without refresh
|
|
|
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
|
|
|
// use the cache again
|
|
|
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
|
|
|
|
|
|
|
// with refresh (and load the second value out of the db)
|
2019-02-11 20:36:26 +00:00
|
|
|
$this->assertEquals($data, $configuration->get($uid, 'test', 'it', null, true));
|
2019-02-10 18:52:21 +00:00
|
|
|
|
|
|
|
// without refresh and wrong value and default
|
|
|
|
$this->assertEquals('default', $configuration->get($uid, 'test', 'not', 'default'));
|
|
|
|
}
|
|
|
|
|
2019-02-11 20:36:26 +00:00
|
|
|
/**
|
|
|
|
* Test the configuration get() method with different isLoaded settings
|
|
|
|
* @dataProvider dataTests
|
|
|
|
*/
|
|
|
|
public function testGetWithoutLoaded($data)
|
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
|
|
|
|
|
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
|
2019-02-22 23:09:57 +00:00
|
|
|
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn(null)->once();
|
2019-02-11 20:36:26 +00:00
|
|
|
|
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
|
|
|
|
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn($data)->once();
|
|
|
|
|
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
|
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
|
|
|
|
// first run is not loaded and no data is found in the DB
|
|
|
|
$this->assertNull($configuration->get($uid, 'test', 'it'));
|
|
|
|
|
|
|
|
// second run is not loaded, but now data is found in the db (overwrote cache)
|
|
|
|
$this->assertEquals($data, $configuration->get($uid,'test', 'it'));
|
|
|
|
|
|
|
|
// third run is loaded and therefore cache is used
|
|
|
|
$this->assertEquals($data, $configuration->get($uid,'test', 'it'));
|
|
|
|
}
|
|
|
|
|
2019-02-10 18:52:21 +00:00
|
|
|
/**
|
|
|
|
* Test the configuration delete() method without adapter
|
2019-02-11 20:36:26 +00:00
|
|
|
* @dataProvider dataTests
|
2019-02-10 18:52:21 +00:00
|
|
|
*/
|
2019-02-11 20:36:26 +00:00
|
|
|
public function testDeleteWithoutDB($data)
|
2019-02-10 18:52:21 +00:00
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
|
2019-02-10 18:52:21 +00:00
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
|
2019-02-11 20:36:26 +00:00
|
|
|
$this->assertTrue($configuration->set($uid, 'test', 'it', $data));
|
|
|
|
$this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
|
2019-02-10 18:52:21 +00:00
|
|
|
|
|
|
|
$this->assertTrue($configuration->delete($uid, 'test', 'it'));
|
|
|
|
$this->assertNull($configuration->get($uid, 'test', 'it'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the configuration delete() method with adapter
|
|
|
|
*/
|
|
|
|
public function testDeleteWithDB()
|
|
|
|
{
|
|
|
|
$uid = 234;
|
2019-07-12 20:38:50 +00:00
|
|
|
$configCache = new PConfigCache();
|
2019-02-17 20:41:45 +00:00
|
|
|
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
|
2019-02-10 18:52:21 +00:00
|
|
|
$configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(false)->once();
|
2019-02-11 20:36:26 +00:00
|
|
|
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
|
|
|
|
|
2019-02-10 18:52:21 +00:00
|
|
|
$configAdapter->shouldReceive('delete')->with($uid, 'test', 'it')->andReturn(false)->once();
|
|
|
|
|
|
|
|
$configAdapter->shouldReceive('delete')->with($uid, 'test', 'second')->andReturn(true)->once();
|
|
|
|
$configAdapter->shouldReceive('delete')->with($uid, 'test', 'third')->andReturn(false)->once();
|
|
|
|
$configAdapter->shouldReceive('delete')->with($uid, 'test', 'quarter')->andReturn(true)->once();
|
|
|
|
|
|
|
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
|
|
|
|
|
|
|
$this->assertFalse($configuration->set($uid, 'test', 'it', 'now'));
|
|
|
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
|
|
|
|
|
|
|
// delete from set
|
|
|
|
$this->assertTrue($configuration->delete($uid, 'test', 'it'));
|
|
|
|
// delete from db only
|
|
|
|
$this->assertTrue($configuration->delete($uid, 'test', 'second'));
|
|
|
|
// no delete
|
|
|
|
$this->assertFalse($configuration->delete($uid, 'test', 'third'));
|
|
|
|
// delete both
|
|
|
|
$this->assertTrue($configuration->delete($uid, 'test', 'quarter'));
|
|
|
|
}
|
|
|
|
}
|