class Config
{
/**
- * @var Config\IConfigAdapter
+ * @var Config\IConfigAdapter|null
*/
private static $adapter;
*/
public static function load($family = "config")
{
- if (!isset(self::$adapter)) {
+ if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
return;
}
*/
public static function get($family, $key, $default_value = null, $refresh = false)
{
- if (!isset(self::$adapter)) {
+ if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
return self::$cache->get($family, $key, $default_value);
}
*/
public static function set($family, $key, $value)
{
- if (!isset(self::$adapter)) {
- self::$cache->set($family, $key, $value);
- return true;
+ if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
+ return self::$cache->set($family, $key, $value);
}
return self::$adapter->set($family, $key, $value);
*/
public static function delete($family, $key)
{
- if (!isset(self::$adapter)) {
+ if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
self::$cache->delete($family, $key);
}
--- /dev/null
+<?php
+
+namespace Friendica\Core\Config;
+
+use Friendica\Database\DBA;
+
+abstract class AbstractDbaConfigAdapter
+{
+ public function isConnected()
+ {
+ return DBA::connected();
+ }
+}
$this->config[$cat][$key] = $value;
}
+
+ return true;
}
/**
* @return mixed
*/
public function delete($cat, $k);
+
+ /**
+ * Checks, if the current adapter is connected to the backend
+ *
+ * @return bool
+ */
+ public function isConnected();
}
* @param string $cat Config category
* @param string $key Config key
* @param mixed $value Value to set
+ *
+ * @return bool True, if the value is set
*/
function set($cat, $key, $value);
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
-class JITConfigAdapter implements IConfigAdapter
+class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
{
private $cache;
private $in_db;
*/
public function load($cat = "config")
{
+ if (!$this->isConnected()) {
+ return;
+ }
+
// We don't preload "system" anymore.
// This reduces the number of database reads a lot.
if ($cat === 'system') {
*/
public function get($cat, $k, $default_value = null, $refresh = false)
{
+ if (!$this->isConnected()) {
+ return $default_value;
+ }
+
if (!$refresh) {
// Do we have the cached value? Then return it
if (isset($this->cache[$cat][$k])) {
*/
public function set($cat, $k, $value)
{
+ if (!$this->isConnected()) {
+ return false;
+ }
+
// We store our setting values in a string variable.
// So we have to do the conversion here so that the compare below works.
// The exception are array values.
*/
public function delete($cat, $k)
{
+ if (!$this->isConnected()) {
+ return false;
+ }
+
if (isset($this->cache[$cat][$k])) {
unset($this->cache[$cat][$k]);
unset($this->in_db[$cat][$k]);
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
-class PreloadConfigAdapter implements IConfigAdapter
+class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
{
private $config_loaded = false;
*/
public function load($family = 'config')
{
+ if (!$this->isConnected()) {
+ return;
+ }
+
if ($this->config_loaded) {
return;
}
*/
public function get($cat, $k, $default_value = null, $refresh = false)
{
+ if (!$this->isConnected()) {
+ return $default_value;
+ }
+
if ($refresh) {
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
if (DBA::isResult($config)) {
*/
public function set($cat, $k, $value)
{
+ if (!$this->isConnected()) {
+ return false;
+ }
+
// We store our setting values as strings.
// So we have to do the conversion here so that the compare below works.
// The exception are array values.
*/
public function delete($cat, $k)
{
+ if (!$this->isConnected()) {
+ return false;
+ }
+
$this->configCache->delete($cat, $k);
$result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
use Friendica\App;
use Friendica\BaseObject;
+use Friendica\Core\Config;
use Friendica\Core\Config\ConfigCache;
use Friendica\Render\FriendicaSmartyEngine;
use Mockery\MockInterface;
*/
trait AppMockTrait
{
- use ConfigMockTrait;
-
/**
* @var MockInterface|App The mocked Friendica\App
*/
protected $app;
+ /**
+ * @var MockInterface|ConfigCache The mocked Config Cache
+ */
+ protected $configCache;
+
/**
* Mock the App
*
*/
public function mockApp($root, $config)
{
- $this->mockConfigGet('system', 'theme', 'testtheme');
-
+ $this->configCache = $config;
// Mocking App and most used functions
$this->app = \Mockery::mock(App::class);
$this->app
->shouldReceive('get')
->with('database', 'database')
->andReturn(getenv('MYSQL_DATABASE'));
+ $config
+ ->shouldReceive('get')
+ ->with('config', 'hostname')
+ ->andReturn('localhost');
+ $config
+ ->shouldReceive('get')
+ ->with('system', 'theme', NULL)
+ ->andReturn('system_theme');
+
$this->app
->shouldReceive('getConfig')
->andReturn($config);
->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);
+
BaseObject::setApp($this->app);
}
}
+++ /dev/null
-<?php
-
-namespace Friendica\Test\Util;
-
-use Mockery\MockInterface;
-
-/**
- * Trait to Mock Config settings
- */
-trait ConfigMockTrait
-{
- /**
- * @var MockInterface The mocking interface of Friendica\Core\Config
- */
- private $configMock;
-
- /**
- * Mocking a config setting
- *
- * @param string $family The family of the config double
- * @param string $key The key of the config double
- * @param mixed $value The value of the config double
- * @param null|int $times How often the Config will get used
- */
- public function mockConfigGet($family, $key, $value, $times = null)
- {
- if (!isset($this->configMock)) {
- $this->configMock = \Mockery::mock('alias:Friendica\Core\Config');
- }
-
- $this->configMock
- ->shouldReceive('get')
- ->times($times)
- ->with($family, $key)
- ->andReturn($value);
- }
-
- /**
- * Mocking setting a new config entry
- *
- * @param string $family The family of the config double
- * @param string $key The key of the config double
- * @param mixed $value The value of the config double
- * @param null|int $times How often the Config will get used
- * @param bool $return Return value of the set (default is true)
- */
- public function mockConfigSet($family, $key, $value, $times = null, $return = true)
- {
- if (!isset($this->configMock)) {
- $this->configMock = \Mockery::mock('alias:Friendica\Core\Config');
- }
-
- $this->mockConfigGet($family, $key, false, 1);
- if ($return) {
- $this->mockConfigGet($family, $key, $value, 1);
- }
-
- $this->configMock
- ->shouldReceive('set')
- ->times($times)
- ->with($family, $key, $value)
- ->andReturn($return);
- }
-}
<?php
-/**
- * Created by PhpStorm.
- * User: philipp
- * Date: 01.11.18
- * Time: 10:08
- */
namespace Friendica\Test\Util;
-
use Mockery\MockInterface;
trait RendererMockTrait
namespace Friendica\Test\src\App;
use Friendica\App\Mode;
+use Friendica\Core\Config;
use Friendica\Test\MockedTest;
-use Friendica\Test\Util\ConfigMockTrait;
use Friendica\Test\Util\DBAMockTrait;
use Friendica\Test\Util\VFSTrait;
-/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
- */
class ModeTest extends MockedTest
{
use VFSTrait;
use DBAMockTrait;
- use ConfigMockTrait;
public function setUp()
{
- parent::setUp(); // TODO: Change the autogenerated stub
+ parent::setUp();
$this->setUpVfsDir();
}
$this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
}
+ /**
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
public function testWithoutDatabase()
{
$this->mockConnected(false, 1);
$this->assertFalse($mode->has(Mode::DBAVAILABLE));
}
+ /**
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
public function testWithoutDatabaseSetup()
{
$this->mockConnected(true, 1);
$this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
}
+ /**
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
public function testWithMaintenanceMode()
{
$this->mockConnected(true, 1);
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
- $this->mockConfigGet('system', 'maintenance', true, 1);
+
+ $config = \Mockery::mock('Friendica\Core\Config\ConfigCache');
+ $config
+ ->shouldReceive('get')
+ ->with('system', 'maintenance', null)
+ ->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();
$this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
}
+ /**
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
public function testNormalMode()
{
$this->mockConnected(true, 1);
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
- $this->mockConfigGet('system', 'maintenance', false, 1);
+
+ $config = \Mockery::mock('Friendica\Core\Config\ConfigCache');
+ $config
+ ->shouldReceive('get')
+ ->with('system', 'maintenance', null)
+ ->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();
namespace Friendica\Test;
-use Friendica\App;
use Friendica\BaseObject;
use Friendica\Test\Util\AppMockTrait;
use Friendica\Test\Util\VFSTrait;
/**
* Tests for the BaseObject class.
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
*/
class BaseObjectTest extends TestCase
{
private $baseObject;
/**
- * Create variables used in tests.
- */
- protected function setUp()
- {
- $this->baseObject = new BaseObject();
- }
-
- /**
- * Test the getApp() function.
- * @return void
- */
- public function testGetApp()
- {
- $this->setUpVfsDir();
- $configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
- $this->mockApp($this->root, $configMock);
-
- $this->assertInstanceOf(App::class, $this->baseObject->getApp());
- }
-
- /**
- * Test the setApp() function.
+ * Test the setApp() and getApp() function.
* @return void
*/
- public function testSetApp()
+ public function testGetSetApp()
{
+ $baseObject = new BaseObject();
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
$this->mockApp($this->root, $configMock);
- $this->assertNull($this->baseObject->setApp($this->app));
- $this->assertEquals($this->app, $this->baseObject->getApp());
+ $this->assertNull($baseObject->setApp($this->app));
+ $this->assertEquals($this->app, $baseObject->getApp());
}
/**
* Test the getApp() function without App
* @expectedException Friendica\Network\HTTPException\InternalServerErrorException
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
*/
public function testGetAppFailed()
{
- BaseObject::getApp();
+ $baseObject = new BaseObject();
+ $baseObject->getApp();
}
}
namespace Friendica\Test\src\Core\Cache;
-
use Friendica\Core\Cache\ArrayCache;
-/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
- */
class ArrayCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
use Friendica\Core\Cache\MemcachedCacheDriver;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\AppMockTrait;
-use Friendica\Test\Util\DateTimeFormatMockTrait;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\PidFile;
{
use VFSTrait;
use AppMockTrait;
- use DateTimeFormatMockTrait;
/**
* @var int Start time of the mock (used for time operations)
->shouldReceive('getHostname')
->andReturn('friendica.local');
- $this->mockUtcNow($this->startTime);
-
parent::setUp();
$this->instance = $this->getInstance();
- // Default config
- $this->mockConfigGet('config', 'hostname', 'localhost');
- $this->mockConfigGet('system', 'throttle_limit_day', 100);
- $this->mockConfigGet('system', 'throttle_limit_week', 100);
- $this->mockConfigGet('system', 'throttle_limit_month', 100);
- $this->mockConfigGet('system', 'theme', 'system_theme');
-
$this->instance->clear(false);
}
public function setUp()
{
+ $this->mockUtcNow($this->startTime);
+
$this->mockConnected();
$this->mockConnect();
use Friendica\Core\Cache\CacheDriverFactory;
/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
* @requires extension memcache
*/
class MemcacheCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
- $this->mockConfigGet('system', 'memcache_host', 'localhost', 1);
- $this->mockConfigGet('system', 'memcache_port', 11211, 1);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'memcache_host', NULL)
+ ->andReturn('localhost');
+
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'memcache_port', NULL)
+ ->andReturn(11211);
$this->cache = CacheDriverFactory::create('memcache');
return $this->cache;
use Friendica\Core\Cache\CacheDriverFactory;
/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
* @requires extension memcached
*/
class MemcachedCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
- $this->mockConfigGet('system', 'memcached_hosts', [0 => 'localhost, 11211']);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'memcached_hosts', NULL)
+ ->andReturn([0 => 'localhost, 11211']);
$this->cache = CacheDriverFactory::create('memcached');
return $this->cache;
use Friendica\Core\Cache\CacheDriverFactory;
/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
* @requires extension redis
*/
class RedisCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
- $this->mockConfigGet('system', 'redis_host', 'localhost', 1);
- $this->mockConfigGet('system', 'redis_port', null, 1);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'redis_host', NULL)
+ ->andReturn('localhost');
+
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'redis_port', NULL)
+ ->andReturn(null);
$this->cache = CacheDriverFactory::create('redis');
return $this->cache;
$this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
$this->db_pass = getenv('MYSQL_PASSWORD');
- $this->mockConfigGet('config', 'php_path', false);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('config', 'php_path', NULL)
+ ->andReturn(false);
$this->mockL10nT();
}
}
function testSetGetKeyValue() {
- $this->mockConfigSet('config', 'test', 'now', 1);
+ $this->configCache
+ ->shouldReceive('set')
+ ->with('config', 'test', 'now')
+ ->andReturn(true)
+ ->once();
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('config', 'test', NULL)
+ ->andReturn('now')
+ ->twice();
+
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test <= now\n", $txt);
- $this->mockConfigGet('config', 'test', 'now', 1);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('config', 'test', null)
+ ->andReturn('now')
+ ->once();
+
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test => now\n", $txt);
- $this->mockConfigGet('config', 'test', null, 1);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('config', 'test', null)
+ ->andReturn(null)
+ ->once();
+
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
$console->setArgument(1, 'test');
function testSetArrayValue() {
$testArray = [1, 2, 3];
- $this->mockConfigGet('config', 'test', $testArray, 1);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('config', 'test', null)
+ ->andReturn($testArray)
+ ->once();
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
}
function testVerbose() {
- $this->mockConfigGet('test', 'it', 'now', 1);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('test', 'it', null)
+ ->andReturn('now')
+ ->once();
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'test');
$console->setArgument(1, 'it');
}
function testUnableToSet() {
- $this->mockConfigSet('test', 'it', 'now', 1, false);
+ $this->configCache
+ ->shouldReceive('set')
+ ->with('test', 'it', 'now')
+ ->andReturn(false)
+ ->once();
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('test', 'it', NULL)
+ ->andReturn(NULL)
+ ->once();
$console = new Config();
$console->setArgument(0, 'test');
$console->setArgument(1, 'it');
use Friendica\Core\Cache\ArrayCache;
use Friendica\Core\Lock\CacheLockDriver;
-/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
- */
class ArrayCacheLockDriverTest extends LockTest
{
protected function getInstance()
protected function setUp()
{
- parent::setUp();
- $this->instance = $this->getInstance();
- $this->instance->releaseAll();
-
// Reusable App object
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
->shouldReceive('getHostname')
->andReturn('friendica.local');
- // Default config
- $this->mockConfigGet('config', 'hostname', 'localhost');
- $this->mockConfigGet('system', 'throttle_limit_day', 100);
- $this->mockConfigGet('system', 'throttle_limit_week', 100);
- $this->mockConfigGet('system', 'throttle_limit_month', 100);
- $this->mockConfigGet('system', 'theme', 'system_theme');
+ parent::setUp();
+ $this->instance = $this->getInstance();
+ $this->instance->releaseAll();
}
protected function tearDown()
/**
* @requires extension Memcache
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
*/
class MemcacheCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
- $this->mockConfigGet('system', 'memcache_host', 'localhost', 1);
- $this->mockConfigGet('system', 'memcache_port', 11211, 1);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'memcache_host', NULL)
+ ->andReturn('localhost');
+
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'memcache_port', NULL)
+ ->andReturn(11211);
return new CacheLockDriver(CacheDriverFactory::create('memcache'));
}
/**
* @requires extension memcached
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
*/
class MemcachedCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
- $this->mockConfigGet('system', 'memcached_hosts', [0 => 'localhost, 11211']);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'memcached_hosts', NULL)
+ ->andReturn([0 => 'localhost, 11211']);
return new CacheLockDriver(CacheDriverFactory::create('memcached'));
}
/**
* @requires extension redis
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
*/
class RedisCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
- $this->mockConfigGet('system', 'redis_host', 'localhost', 1);
- $this->mockConfigGet('system', 'redis_port', null, 1);
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'redis_host', NULL)
+ ->andReturn('localhost');
+
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'redis_port', NULL)
+ ->andReturn(null);
return new CacheLockDriver(CacheDriverFactory::create('redis'));
}
use Friendica\Core\Lock\SemaphoreLockDriver;
-/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
- */
class SemaphoreLockDriverTest extends LockTest
{
public function setUp()
parent::setUp();
$this->app->shouldReceive('getHostname')->andReturn('friendica.local');
- $this->mockConfigGet('system', 'temppath', '/tmp/');
+
+ $this->configCache
+ ->shouldReceive('get')
+ ->with('system', 'temppath', NULL)
+ ->andReturn('/tmp/');
}
protected function getInstance()
$this->logOutput = FActory\LoggerFactory::enableTest($this->app->getLogger());
parent::setUp();
-
- // Default config
- Config::set('config', 'hostname', 'localhost');
- Config::set('system', 'throttle_limit_day', 100);
- Config::set('system', 'throttle_limit_week', 100);
- Config::set('system', 'throttle_limit_month', 100);
- Config::set('system', 'theme', 'system_theme');
}
/**