Merge pull request #6641 from nupplaphil/config_followup

Config FollowUp
This commit is contained in:
Hypolite Petovan 2019-02-17 17:56:31 -05:00 committed by GitHub
commit 256e845c5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
75 changed files with 2606 additions and 1368 deletions

View file

@ -5,10 +5,11 @@
namespace Friendica\Test;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Database\DBA;
use Friendica\Factory;
use Friendica\Util\BasePath;
use Friendica\Util\Profiler;
use PHPUnit\DbUnit\DataSet\YamlDataSet;
use PHPUnit\DbUnit\TestCaseTrait;
use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
@ -40,11 +41,14 @@ abstract class DatabaseTest extends MockedTest
}
$basedir = BasePath::create(dirname(__DIR__));
$configLoader = new Config\ConfigCacheLoader($basedir);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$profiler = \Mockery::mock(Profiler::class);
DBA::connect(
$config,
$profiler,
getenv('MYSQL_HOST'),
getenv('MYSQL_USERNAME'),
getenv('MYSQL_PASSWORD'),

View file

@ -5,8 +5,8 @@ namespace Friendica\Test\Util;
use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Config;
use Friendica\Core\Config\ConfigCache;
use Friendica\Render\FriendicaSmartyEngine;
use Friendica\Util\Profiler;
use Mockery\MockInterface;
use org\bovigo\vfs\vfsStreamDirectory;
@ -21,74 +21,83 @@ trait AppMockTrait
protected $app;
/**
* @var MockInterface|ConfigCache The mocked Config Cache
* @var MockInterface|Config\Configuration The mocked Config Cache
*/
protected $configCache;
protected $configMock;
/**
* @var MockInterface|Profiler The mocked profiler
*/
protected $profilerMock;
/**
* Mock the App
*
* @param vfsStreamDirectory $root The root directory
* @param MockInterface|ConfigCache $config The config cache
*/
public function mockApp($root, $config)
public function mockApp($root)
{
$this->configCache = $config;
$this->configMock = \Mockery::mock(Config\Cache\IConfigCache::class);
$configAdapterMock = \Mockery::mock(Config\Adapter\IConfigAdapter::class);
// Disable the adapter
$configAdapterMock->shouldReceive('isConnected')->andReturn(false);
$config = new Config\Configuration($this->configMock, $configAdapterMock);
// Initialize empty Config
Config::init($config);
// Mocking App and most used functions
$this->app = \Mockery::mock(App::class);
$this->app
->shouldReceive('getBasePath')
->andReturn($root->url());
$config
$this->configMock
->shouldReceive('has')
->andReturn(true);
$this->configMock
->shouldReceive('get')
->with('database', 'hostname')
->andReturn(getenv('MYSQL_HOST'));
$config
$this->configMock
->shouldReceive('get')
->with('database', 'username')
->andReturn(getenv('MYSQL_USERNAME'));
$config
$this->configMock
->shouldReceive('get')
->with('database', 'password')
->andReturn(getenv('MYSQL_PASSWORD'));
$config
$this->configMock
->shouldReceive('get')
->with('database', 'database')
->andReturn(getenv('MYSQL_DATABASE'));
$config
$this->configMock
->shouldReceive('get')
->with('config', 'hostname')
->andReturn('localhost');
$config
$this->configMock
->shouldReceive('get')
->with('system', 'theme', NULL)
->with('system', 'theme')
->andReturn('system_theme');
$this->app
->shouldReceive('getConfig')
->andReturn($config);
$this->profilerMock = \Mockery::mock(Profiler::class);
$this->profilerMock->shouldReceive('saveTimestamp');
$this->app
->shouldReceive('getConfigCache')
->andReturn($this->configMock);
$this->app
->shouldReceive('getTemplateEngine')
->andReturn(new FriendicaSmartyEngine());
$this->app
->shouldReceive('getCurrentTheme')
->andReturn('Smarty3');
$this->app
->shouldReceive('saveTimestamp')
->andReturn(true);
$this->app
->shouldReceive('getBaseUrl')
->andReturn('http://friendica.local');
// Initialize empty Config
Config::init($config);
$configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter');
$configAdapter
->shouldReceive('isConnected')
->andReturn(false);
Config::setAdapter($configAdapter);
$this->app
->shouldReceive('getProfiler')
->andReturn($this->profilerMock);
BaseObject::setApp($this->app);
}

View file

@ -2,6 +2,7 @@
namespace Friendica\Test\Util;
use Friendica\Database\DBStructure;
use Mockery\MockInterface;
/**
@ -16,6 +17,7 @@ trait DBStructureMockTrait
/**
* Mocking DBStructure::update()
* @see DBStructure::update();
*
* @param array $args The arguments for the update call
* @param bool $return True, if the connect was successful, otherwise false

View file

@ -7,6 +7,7 @@ namespace Friendica\Test;
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
use Friendica\Core\System;
@ -36,11 +37,14 @@ class ApiTest extends DatabaseTest
public function setUp()
{
$basedir = BasePath::create(dirname(__DIR__) . '/../');
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($configCache, $profiler, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config);
$this->app = new App($config, $logger, false);
$this->logOutput = FActory\LoggerFactory::enableTest($this->app->getLogger());
$this->app = new App($config, $logger, $profiler, false);
parent::setUp();

View file

@ -90,19 +90,14 @@ class ModeTest extends MockedTest
$this->mockConnected(true, 1);
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
$config = \Mockery::mock('Friendica\Core\Config\ConfigCache');
$config = \Mockery::mock(Config\Configuration::class);
$config
->shouldReceive('get')
->with('system', 'maintenance', null)
->with('system', 'maintenance', null, false)
->andReturn(true)
->once();
// Initialize empty Config
Config::init($config);
$configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter');
$configAdapter
->shouldReceive('isConnected')
->andReturn(false);
Config::setAdapter($configAdapter);
$mode = new Mode($this->root->url());
$mode->determine();
@ -123,19 +118,14 @@ class ModeTest extends MockedTest
$this->mockConnected(true, 1);
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
$config = \Mockery::mock('Friendica\Core\Config\ConfigCache');
$config = \Mockery::mock(Config\Configuration::class);
$config
->shouldReceive('get')
->with('system', 'maintenance', null)
->with('system', 'maintenance', null, false)
->andReturn(false)
->once();
// Initialize empty Config
Config::init($config);
$configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter');
$configAdapter
->shouldReceive('isConnected')
->andReturn(false);
Config::setAdapter($configAdapter);
$mode = new Mode($this->root->url());
$mode->determine();

View file

@ -31,8 +31,7 @@ class BaseObjectTest extends TestCase
{
$baseObject = new BaseObject();
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
$this->mockApp($this->root, $configMock);
$this->mockApp($this->root);
$this->assertNull($baseObject->setApp($this->app));
$this->assertEquals($this->app, $baseObject->getApp());

View file

@ -67,8 +67,7 @@ abstract class CacheTest extends MockedTest
protected function setUp()
{
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
$this->mockApp($this->root, $configMock);
$this->mockApp($this->root);
$this->app
->shouldReceive('getHostname')
->andReturn('friendica.local');

View file

@ -12,14 +12,14 @@ class MemcacheCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcache_host', NULL)
->with('system', 'memcache_host')
->andReturn('localhost');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcache_port', NULL)
->with('system', 'memcache_port')
->andReturn(11211);
$this->cache = CacheDriverFactory::create('memcache');

View file

@ -12,9 +12,9 @@ class MemcachedCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcached_hosts', NULL)
->with('system', 'memcached_hosts')
->andReturn([0 => 'localhost, 11211']);
$this->cache = CacheDriverFactory::create('memcached');

View file

@ -12,14 +12,14 @@ class RedisCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'redis_host', NULL)
->with('system', 'redis_host')
->andReturn('localhost');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'redis_port', NULL)
->with('system', 'redis_port')
->andReturn(null);
$this->cache = CacheDriverFactory::create('redis');

View file

@ -1,9 +1,9 @@
<?php
namespace Friendica\Test\Core\Config;
namespace Friendica\Test\Core\Config\Cache;
use Friendica\Core\Config\ConfigCache;
use Friendica\Core\Config\ConfigCacheLoader;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Config\Cache\ConfigCacheLoader;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait;
use org\bovigo\vfs\vfsStream;
@ -59,6 +59,7 @@ class ConfigCacheLoaderTest extends MockedTest
$this->delConfigFile('local.config.php');
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
@ -91,6 +92,7 @@ class ConfigCacheLoaderTest extends MockedTest
$this->delConfigFile('local.config.php');
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
@ -122,6 +124,7 @@ class ConfigCacheLoaderTest extends MockedTest
$this->delConfigFile('local.config.php');
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
@ -160,6 +163,7 @@ class ConfigCacheLoaderTest extends MockedTest
vfsStream::create($structure, $this->root);
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .

View file

@ -1,8 +1,8 @@
<?php
namespace Friendica\Test\Core\Config;
namespace Friendica\Test\Core\Config\Cache;
use Friendica\Core\Config\ConfigCache;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Test\MockedTest;
class ConfigCacheTest extends MockedTest
@ -48,7 +48,7 @@ class ConfigCacheTest extends MockedTest
public function testLoadConfigArray($data)
{
$configCache = new ConfigCache();
$configCache->loadConfigArray($data);
$configCache->load($data);
$this->assertConfigValues($data, $configCache);
}
@ -67,18 +67,38 @@ class ConfigCacheTest extends MockedTest
];
$configCache = new ConfigCache();
$configCache->loadConfigArray($data);
$configCache->loadConfigArray($override);
$configCache->load($data);
$configCache->load($override);
$this->assertConfigValues($data, $configCache);
// override the value
$configCache->loadConfigArray($override, true);
$configCache->load($override, true);
$this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
$this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
}
/**
* Test the loadConfigArray() method with wrong/empty datasets
*/
public function testLoadConfigArrayWrong()
{
$configCache = new ConfigCache();
// empty dataset
$configCache->load([]);
$this->assertEmpty($configCache->getAll());
// wrong dataset
$configCache->load(['system' => 'not_array']);
$this->assertEmpty($configCache->getAll());
// incomplete dataset (key is integer ID of the array)
$configCache->load(['system' => ['value']]);
$this->assertEquals('value', $configCache->get('system', 0));
}
/**
* Test the getAll() method
* @dataProvider dataTests
@ -86,14 +106,12 @@ class ConfigCacheTest extends MockedTest
public function testGetAll($data)
{
$configCache = new ConfigCache();
$configCache->loadConfigArray($data);
$configCache->load($data);
$all = $configCache->getAll();
$this->assertContains($data['system'], $all);
// config values are stored directly in the array base
$this->assertEquals($data['config']['a'], $all['a']);
$this->assertContains($data['config'], $all);
}
/**
@ -113,6 +131,54 @@ class ConfigCacheTest extends MockedTest
$this->assertConfigValues($data, $configCache);
}
/**
* Test the get() method without a value
*/
public function testGetEmpty()
{
$configCache = new ConfigCache();
$this->assertEquals('!<unset>!', $configCache->get('something', 'value'));
}
/**
* Test the has() method
*/
public function testHas()
{
$configCache = new ConfigCache();
$this->assertFalse($configCache->has('system', 'test'));
$this->assertFalse($configCache->has('system'));
$configCache->set('system', 'test', 'it');
$this->assertTrue($configCache->has('system', 'test'));
$this->assertTrue($configCache->has('system'));
}
/**
* Test the get() method with a category
*/
public function testGetCat()
{
$configCache = new ConfigCache([
'system' => [
'key1' => 'value1',
'key2' => 'value2',
],
'config' => [
'key3' => 'value3',
],
]);
$this->assertTrue($configCache->has('system'));
$this->assertEquals([
'key1' => 'value1',
'key2' => 'value2',
], $configCache->get('system'));
}
/**
* Test the delete() method
* @dataProvider dataTests
@ -149,6 +215,32 @@ class ConfigCacheTest extends MockedTest
}
/**
* Test the getP() method with a category
*/
public function testGetPCat()
{
$configCache = new ConfigCache();
$uid = 345;
$configCache->loadP($uid, [
'system' => [
'key1' => 'value1',
'key2' => 'value2',
],
'config' => [
'key3' => 'value3',
],
]);
$this->assertTrue($configCache->hasP($uid,'system'));
$this->assertEquals([
'key1' => 'value1',
'key2' => 'value2',
], $configCache->get($uid, 'system'));
}
/**
* Test the deleteP() method
* @dataProvider dataTests
@ -172,4 +264,20 @@ class ConfigCacheTest extends MockedTest
$this->assertEmpty($configCache->getAll());
}
/**
* Test the hasP() method
*/
public function testHasP()
{
$configCache = new ConfigCache();
$uid = 345;
$this->assertFalse($configCache->hasP($uid, 'system', 'test'));
$this->assertFalse($configCache->hasP($uid, 'system'));
$configCache->setP($uid, 'system', 'test', 'it');
$this->assertTrue($configCache->hasP($uid, 'system', 'test'));
$this->assertTrue($configCache->hasP($uid, 'system'));
}
}

View file

@ -0,0 +1,276 @@
<?php
namespace Friendica\Test\Core\Config;
use Friendica\Core\Config\Adapter\IConfigAdapter;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\Config\Configuration;
use Friendica\Test\MockedTest;
class ConfigurationTest extends MockedTest
{
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],
];
}
/**
* Test the configuration initialization
*/
public function testSetUp()
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
$configuration = new Configuration($configCache, $configAdapter);
$this->assertInstanceOf(IConfigCache::class, $configuration->getCache());
}
/**
* Test the configuration load() method
*/
public function testCacheLoad()
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
// expected loading
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
$configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->once();
$configuration = new Configuration($configCache, $configAdapter);
$configuration->load('testing');
$this->assertEquals('it', $configuration->get('testing', 'test'));
$this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
}
/**
* Test the configuration load() method with overwrite
*/
public function testCacheLoadDouble()
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
// expected loading
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
$configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->twice();
// expected next loading
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
$configuration = new Configuration($configCache, $configAdapter);
$configuration->load('testing');
$this->assertEquals('it', $configuration->get('testing', 'test'));
$this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
$configuration->load('testing');
$this->assertEquals('again', $configuration->get('testing', 'test'));
$this->assertEquals('again', $configuration->getCache()->get('testing', 'test'));
}
/**
* Test the configuration get() and set() methods without adapter
* @dataProvider dataTests
*/
public function testSetGetWithoutDB($data)
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
$configuration = new Configuration($configCache, $configAdapter);
$this->assertTrue($configuration->set('test', 'it', $data));
$this->assertEquals($data, $configuration->get('test', 'it'));
$this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
}
/**
* Test the configuration get() and set() methods with adapter
* @dataProvider dataTests
*/
public function testSetGetWithDB($data)
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
$configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
$configAdapter->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
$configuration = new Configuration($configCache, $configAdapter);
$this->assertTrue($configuration->set('test', 'it', $data));
$this->assertEquals($data, $configuration->get('test', 'it'));
$this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
}
/**
* Test the configuration get() method with wrong value and no db
*/
public function testGetWrongWithoutDB()
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
$configuration = new Configuration($configCache, $configAdapter);
// without refresh
$this->assertNull($configuration->get('test', 'it'));
/// beware that the cache returns '!<unset>!' and not null for a non existing value
$this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
// with default value
$this->assertEquals('default', $configuration->get('test', 'it', 'default'));
// with default value and refresh
$this->assertEquals('default', $configuration->get('test', 'it', 'default', true));
}
/**
* Test the configuration get() method with refresh
* @dataProvider dataTests
*/
public function testGetWithRefresh($data)
{
$configCache = new ConfigCache(['test' => ['it' => 'now']]);
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
$configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->twice();
$configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
$configAdapter->shouldReceive('isLoaded')->with('test', 'not')->andReturn(false)->once();
$configAdapter->shouldReceive('get')->with('test', 'not')->andReturn('!<unset>!')->once();
$configuration = new Configuration($configCache, $configAdapter);
// without refresh
$this->assertEquals('now', $configuration->get('test', 'it'));
$this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
// with refresh
$this->assertEquals($data, $configuration->get('test', 'it', null, true));
$this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
// without refresh and wrong value and default
$this->assertEquals('default', $configuration->get('test', 'not', 'default'));
$this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'not'));
}
/**
* Test the configuration get() method with different isLoaded settings
* @dataProvider dataTests
*/
public function testGetWithoutLoaded($data)
{
$configCache = new ConfigCache(['test' => ['it' => 'now']]);
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
$configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
$configAdapter->shouldReceive('get')->with('test', 'it')->andReturn('!<unset>!')->once();
$configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
$configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
$configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
$configuration = new Configuration($configCache, $configAdapter);
// first run is not loaded and no data is found in the DB
$this->assertEquals('now', $configuration->get('test', 'it'));
$this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
// second run is not loaded, but now data is found in the db (overwrote cache)
$this->assertEquals($data, $configuration->get('test', 'it'));
$this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
// third run is loaded and therefore cache is used
$this->assertEquals($data, $configuration->get('test', 'it'));
$this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
}
/**
* Test the configuration delete() method without adapter
* @dataProvider dataTests
*/
public function testDeleteWithoutDB($data)
{
$configCache = new ConfigCache(['test' => ['it' => $data]]);
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
$configuration = new Configuration($configCache, $configAdapter);
$this->assertEquals($data, $configuration->get('test', 'it'));
$this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
$this->assertTrue($configuration->delete('test', 'it'));
$this->assertNull($configuration->get('test', 'it'));
$this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
$this->assertEmpty($configuration->getCache()->getAll());
}
/**
* Test the configuration delete() method with adapter
*/
public function testDeleteWithDB()
{
$configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
$configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
$configAdapter->shouldReceive('delete')->with('test', 'it')->andReturn(false)->once();
$configAdapter->shouldReceive('delete')->with('test', 'second')->andReturn(true)->once();
$configAdapter->shouldReceive('delete')->with('test', 'third')->andReturn(false)->once();
$configAdapter->shouldReceive('delete')->with('test', 'quarter')->andReturn(true)->once();
$configuration = new Configuration($configCache, $configAdapter);
$this->assertEquals('now', $configuration->get('test', 'it'));
$this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
// delete from cache only
$this->assertTrue($configuration->delete('test', 'it'));
// delete from db only
$this->assertTrue($configuration->delete('test', 'second'));
// no delete
$this->assertFalse($configuration->delete('test', 'third'));
// delete both
$this->assertTrue($configuration->delete('test', 'quarter'));
$this->assertEmpty($configuration->getCache()->getAll());
}
}

View file

@ -0,0 +1,247 @@
<?php
namespace Friendica\Test\Core\Config;
use Friendica\Core\Config\Adapter\IPConfigAdapter;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Config\PConfiguration;
use Friendica\Test\MockedTest;
class PConfigurationTest extends MockedTest
{
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],
];
}
/**
* Test the configuration load() method
*/
public function testCacheLoad()
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
// expected loading
$configAdapter->shouldReceive('load')
->with($uid, 'testing')
->andReturn(['testing' => ['test' => 'it']])
->once();
$configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->once();
$configuration = new PConfiguration($configCache, $configAdapter);
$configuration->load($uid, 'testing');
$this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
}
/**
* Test the configuration load() method with overwrite
*/
public function testCacheLoadDouble()
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
// expected loading
$configAdapter->shouldReceive('load')->with($uid, 'testing')->andReturn(['testing' => ['test' => 'it']])->once();
$configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->twice();
// 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'));
}
/**
* Test the configuration get() and set() methods without adapter
* @dataProvider dataTests
*/
public function testSetGetWithoutDB($data)
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(2);
$configuration = new PConfiguration($configCache, $configAdapter);
$this->assertTrue($configuration->set($uid, 'test', 'it', $data));
$this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
}
/**
* Test the configuration get() and set() methods with adapter
* @dataProvider dataTests
*/
public function testSetGetWithDB($data)
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$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();
$configuration = new PConfiguration($configCache, $configAdapter);
$this->assertTrue($configuration->set($uid, 'test', 'it', $data));
$this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
}
/**
* Test the configuration get() method with wrong value and no db
*/
public function testGetWrongWithoutDB()
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$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
* @dataProvider dataTests
*/
public function testGetWithRefresh($data)
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('now')->once();
$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();
$configAdapter->shouldReceive('get')->with($uid, 'test', 'not')->andReturn('!<unset>!')->once();
$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)
$this->assertEquals($data, $configuration->get($uid, 'test', 'it', null, true));
// without refresh and wrong value and default
$this->assertEquals('default', $configuration->get($uid, 'test', 'not', 'default'));
}
/**
* Test the configuration get() method with different isLoaded settings
* @dataProvider dataTests
*/
public function testGetWithoutLoaded($data)
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('!<unset>!')->once();
$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'));
}
/**
* Test the configuration delete() method without adapter
* @dataProvider dataTests
*/
public function testDeleteWithoutDB($data)
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
$configuration = new PConfiguration($configCache, $configAdapter);
$this->assertTrue($configuration->set($uid, 'test', 'it', $data));
$this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
$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;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
$configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(false)->once();
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
$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'));
}
}

View file

@ -52,9 +52,9 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
$this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
$this->db_pass = getenv('MYSQL_PASSWORD');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'php_path', NULL)
->with('config', 'php_path')
->andReturn(false);
$this->mockL10nT();

View file

@ -32,14 +32,14 @@ class ConfigConsoleTest extends ConsoleTest
}
function testSetGetKeyValue() {
$this->configCache
$this->configMock
->shouldReceive('set')
->with('config', 'test', 'now')
->andReturn(true)
->once();
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'test', NULL)
->with('config', 'test')
->andReturn('now')
->twice();
@ -50,9 +50,9 @@ class ConfigConsoleTest extends ConsoleTest
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test <= now\n", $txt);
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'test', null)
->with('config', 'test')
->andReturn('now')
->once();
@ -62,9 +62,9 @@ class ConfigConsoleTest extends ConsoleTest
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test => now\n", $txt);
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'test', null)
->with('config', 'test')
->andReturn(null)
->once();
@ -77,9 +77,9 @@ class ConfigConsoleTest extends ConsoleTest
function testSetArrayValue() {
$testArray = [1, 2, 3];
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'test', null)
->with('config', 'test')
->andReturn($testArray)
->once();
@ -105,9 +105,9 @@ class ConfigConsoleTest extends ConsoleTest
}
function testVerbose() {
$this->configCache
$this->configMock
->shouldReceive('get')
->with('test', 'it', null)
->with('test', 'it')
->andReturn('now')
->once();
$console = new Config($this->consoleArgv);
@ -133,14 +133,14 @@ CONF;
}
function testUnableToSet() {
$this->configCache
$this->configMock
->shouldReceive('set')
->with('test', 'it', 'now')
->andReturn(false)
->once();
$this->configCache
$this->configMock
->shouldReceive('get')
->with('test', 'it', NULL)
->with('test', 'it')
->andReturn(NULL)
->once();
$console = new Config();

View file

@ -29,8 +29,7 @@ abstract class ConsoleTest extends MockedTest
Intercept::setUp();
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
$this->mockApp($this->root, $configMock);
$this->mockApp($this->root);
}
/**

View file

@ -27,8 +27,7 @@ abstract class LockTest extends MockedTest
{
// Reusable App object
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
$this->mockApp($this->root, $configMock);
$this->mockApp($this->root);
$this->app
->shouldReceive('getHostname')
->andReturn('friendica.local');

View file

@ -13,14 +13,14 @@ class MemcacheCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcache_host', NULL)
->with('system', 'memcache_host')
->andReturn('localhost');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcache_port', NULL)
->with('system', 'memcache_port')
->andReturn(11211);
return new CacheLockDriver(CacheDriverFactory::create('memcache'));

View file

@ -13,9 +13,9 @@ class MemcachedCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcached_hosts', NULL)
->with('system', 'memcached_hosts')
->andReturn([0 => 'localhost, 11211']);
return new CacheLockDriver(CacheDriverFactory::create('memcached'));

View file

@ -13,14 +13,14 @@ class RedisCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'redis_host', NULL)
->with('system', 'redis_host')
->andReturn('localhost');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'redis_port', NULL)
->with('system', 'redis_port')
->andReturn(null);
return new CacheLockDriver(CacheDriverFactory::create('redis'));

View file

@ -12,9 +12,9 @@ class SemaphoreLockDriverTest extends LockTest
$this->app->shouldReceive('getHostname')->andReturn('friendica.local');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'temppath', NULL)
->with('system', 'temppath')
->andReturn('/tmp/');
}

View file

@ -3,6 +3,7 @@ namespace Friendica\Test\Database;
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Database\DBA;
use Friendica\Factory;
use Friendica\Test\DatabaseTest;
@ -13,11 +14,14 @@ class DBATest extends DatabaseTest
public function setUp()
{
$basedir = BasePath::create(dirname(__DIR__) . '/../../');
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($configCache, $profiler, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config);
$this->app = new App($config, $logger, false);
$this->logOutput = FActory\LoggerFactory::enableTest($this->app->getLogger());
$this->app = new App($config, $logger, $profiler, false);
parent::setUp();

View file

@ -3,7 +3,7 @@
namespace Friendica\Test\Database;
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Database\DBStructure;
use Friendica\Factory;
use Friendica\Test\DatabaseTest;
@ -14,11 +14,14 @@ class DBStructureTest extends DatabaseTest
public function setUp()
{
$basedir = BasePath::create(dirname(__DIR__) . '/../../');
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($configCache, $profiler, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config);
$this->app = new App($config, $logger, false);
$this->logOutput = FActory\LoggerFactory::enableTest($this->app->getLogger());
$this->app = new App($config, $logger, $profiler, false);
parent::setUp();
}

View file

@ -0,0 +1,181 @@
<?php
namespace src\Util;
use Friendica\Test\MockedTest;
use Friendica\Util\Profiler;
use Mockery\MockInterface;
use Psr\Log\LoggerInterface;
class ProfilerTest extends MockedTest
{
/**
* @var LoggerInterface|MockInterface
*/
private $logger;
protected function setUp()
{
parent::setUp();
$this->logger = \Mockery::mock('Psr\Log\LoggerInterface');
}
/**
* Test the Profiler setup
*/
public function testSetUp()
{
$profiler = new Profiler(true, true);
}
/**
* A dataset for different profiling settings
* @return array
*/
public function dataPerformance()
{
return [
'database' => [
'timestamp' => time(),
'name' => 'database',
'functions' => ['test', 'it'],
],
'database_write' => [
'timestamp' => time(),
'name' => 'database_write',
'functions' => ['test', 'it2'],
],
'cache' => [
'timestamp' => time(),
'name' => 'cache',
'functions' => ['test', 'it3'],
],
'cache_write' => [
'timestamp' => time(),
'name' => 'cache_write',
'functions' => ['test', 'it4'],
],
'network' => [
'timestamp' => time(),
'name' => 'network',
'functions' => ['test', 'it5'],
],
'file' => [
'timestamp' => time(),
'name' => 'file',
'functions' => [],
],
'rendering' => [
'timestamp' => time(),
'name' => 'rendering',
'functions' => ['test', 'it7'],
],
'parser' => [
'timestamp' => time(),
'name' => 'parser',
'functions' => ['test', 'it8'],
],
'marktime' => [
'timestamp' => time(),
'name' => 'parser',
'functions' => ['test'],
],
// This one isn't set during reset
'unknown' => [
'timestamp' => time(),
'name' => 'unknown',
'functions' => ['test'],
],
];
}
/**
* Test the Profiler savetimestamp
* @dataProvider dataPerformance
*/
public function testSaveTimestamp($timestamp, $name, array $functions)
{
$profiler = new Profiler(true, true);
foreach ($functions as $function) {
$profiler->saveTimestamp($timestamp, $name, $function);
}
$this->assertGreaterThanOrEqual(0, $profiler->get($name));
}
/**
* Test the Profiler reset
* @dataProvider dataPerformance
*/
public function testReset($timestamp, $name, array $functions)
{
$profiler = new Profiler(true, true);
$profiler->saveTimestamp($timestamp, $name);
$profiler->reset();
$this->assertEquals(0, $profiler->get($name));
}
public function dataBig()
{
return [
'big' => [
'data' => [
'database' => [
'timestamp' => time(),
'name' => 'database',
'functions' => ['test', 'it'],
],
'database_write' => [
'timestamp' => time(),
'name' => 'database_write',
'functions' => ['test', 'it2'],
],
'cache' => [
'timestamp' => time(),
'name' => 'cache',
'functions' => ['test', 'it3'],
],
'cache_write' => [
'timestamp' => time(),
'name' => 'cache_write',
'functions' => ['test', 'it4'],
],
'network' => [
'timestamp' => time(),
'name' => 'network',
'functions' => ['test', 'it5'],
],
]
]
];
}
/**
* Test the output of the Profiler
* @dataProvider dataBig
*/
public function testSaveLog($data)
{
$this->logger
->shouldReceive('info')
->with('test', \Mockery::any())
->once();
$this->logger
->shouldReceive('info')
->once();
$profiler = new Profiler(true, true);
foreach ($data as $perf => $items) {
foreach ($items['functions'] as $function) {
$profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
}
}
$profiler->saveLog($this->logger, 'test');
}
}