$this->profiler->reset();
if ($this->mode->has(App\Mode::DBAVAILABLE)) {
- $this->profiler->update($this->config);
-
Core\Hook::loadHooks();
$loader = (new Config())->createConfigFileManager($this->getBasePath(), $_SERVER);
Core\Hook::callAll('load_config', $loader);
namespace Friendica\App;
use Detection\MobileDetect;
-use Friendica\Core\Config\ValueObject\Cache;
+use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
/**
*
* @throws \Exception
*/
- public function determine(string $basePath, Database $database, Cache $configCache): Mode
+ public function determine(string $basePath, Database $database, IManageConfigValues $config): Mode
{
$mode = 0;
$mode |= Mode::DBAVAILABLE;
- if (!empty($configCache->get('system', 'maintenance'))) {
+ if (!empty($config->get('system', 'maintenance'))) {
return new Mode($mode);
}
namespace Friendica\Console;
-use Friendica\Core\Config\ValueObject\Cache;
+use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Update;
use Friendica\Database\Database;
use Friendica\Database\DBStructure;
/** @var Database */
private $dba;
- /** @var Cache */
- private $configCache;
+ /** @var IManageConfigValues */
+ private $config;
/** @var DbaDefinition */
private $dbaDefinition;
return $help;
}
- public function __construct(Database $dba, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, BasePath $basePath, Cache $configCache, $argv = null)
+ public function __construct(Database $dba, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, BasePath $basePath, IManageConfigValues $config, $argv = null)
{
parent::__construct($argv);
$this->dba = $dba;
$this->dbaDefinition = $dbaDefinition;
$this->viewDefinition = $viewDefinition;
- $this->configCache = $configCache;
+ $this->config = $config;
$this->basePath = $basePath->getPath();
}
throw new RuntimeException('Unable to connect to database');
}
- $basePath = $this->configCache->get('system', 'basepath');
+ $basePath = $this->config->get('system', 'basepath');
switch ($this->getArgument(0)) {
case "dryrun":
namespace Friendica\Core\PConfig\Factory;
-use Friendica\Core\Config\ValueObject\Cache;
+use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\PConfig\Repository;
use Friendica\Core\PConfig\Type;
class PConfig
{
/**
- * @param Cache $configCache The config cache
- * @param ValueObject\Cache $pConfigCache The personal config cache
- * @param Repository\PConfig $configRepo The configuration model
+ * @param IManageConfigValues $config The config
+ * @param ValueObject\Cache $pConfigCache The personal config cache
+ * @param Repository\PConfig $configRepo The configuration model
*
* @return IManagePersonalConfigValues
*/
- public function create(Cache $configCache, ValueObject\Cache $pConfigCache, Repository\PConfig $configRepo): IManagePersonalConfigValues
+ public function create(IManageConfigValues $config, ValueObject\Cache $pConfigCache, Repository\PConfig $configRepo): IManagePersonalConfigValues
{
- if ($configCache->get('system', 'config_adapter') === 'preload') {
+ if ($config->get('system', 'config_adapter') === 'preload') {
$configuration = new Type\PreloadPConfig($pConfigCache, $configRepo);
} else {
$configuration = new Type\JitPConfig($pConfigCache, $configRepo);
namespace Friendica\Database;
-use Friendica\Core\Config\ValueObject\Cache;
+use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\System;
use Friendica\Database\Definition\DbaDefinition;
use Friendica\Database\Definition\ViewDefinition;
protected $connected = false;
/**
- * @var \Friendica\Core\Config\ValueObject\Cache
+ * @var IManageConfigValues
*/
- protected $configCache;
+ protected $config;
/**
* @var Profiler
*/
/** @var ViewDefinition */
protected $viewDefinition;
- public function __construct(Cache $configCache, Profiler $profiler, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition)
+ public function __construct(IManageConfigValues $config, Profiler $profiler, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition)
{
// We are storing these values for being able to perform a reconnect
- $this->configCache = $configCache;
- $this->profiler = $profiler;
+ $this->config = $config;
+ $this->profiler = $profiler;
$this->dbaDefinition = $dbaDefinition;
$this->viewDefinition = $viewDefinition;
$this->connected = false;
$port = 0;
- $serveraddr = trim($this->configCache->get('database', 'hostname') ?? '');
+ $serveraddr = trim($this->config->get('database', 'hostname') ?? '');
$serverdata = explode(':', $serveraddr);
$host = trim($serverdata[0]);
if (count($serverdata) > 1) {
$port = trim($serverdata[1]);
}
- if (trim($this->configCache->get('database', 'port') ?? 0)) {
- $port = trim($this->configCache->get('database', 'port') ?? 0);
+ if (trim($this->config->get('database', 'port') ?? 0)) {
+ $port = trim($this->config->get('database', 'port') ?? 0);
}
- $user = trim($this->configCache->get('database', 'username'));
- $pass = trim($this->configCache->get('database', 'password'));
- $database = trim($this->configCache->get('database', 'database'));
- $charset = trim($this->configCache->get('database', 'charset'));
- $socket = trim($this->configCache->get('database', 'socket'));
+ $user = trim($this->config->get('database', 'username'));
+ $pass = trim($this->config->get('database', 'password'));
+ $database = trim($this->config->get('database', 'database'));
+ $charset = trim($this->config->get('database', 'charset'));
+ $socket = trim($this->config->get('database', 'socket'));
if (!$host && !$socket || !$user) {
return false;
}
- $persistent = (bool)$this->configCache->get('database', 'persistent');
+ $persistent = (bool)$this->config->get('database', 'persistent');
- $this->pdo_emulate_prepares = (bool)$this->configCache->get('database', 'pdo_emulate_prepares');
+ $this->pdo_emulate_prepares = (bool)$this->config->get('database', 'pdo_emulate_prepares');
- if (!$this->configCache->get('database', 'disable_pdo') && class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
+ if (!$this->config->get('database', 'disable_pdo') && class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
$this->driver = self::PDO;
if ($socket) {
$connect = 'mysql:unix_socket=' . $socket;
private function logIndex(string $query)
{
- if (!$this->configCache->get('system', 'db_log_index')) {
+ if (!$this->config->get('system', 'db_log_index')) {
return;
}
return;
}
- $watchlist = explode(',', $this->configCache->get('system', 'db_log_index_watch'));
- $denylist = explode(',', $this->configCache->get('system', 'db_log_index_denylist'));
+ $watchlist = explode(',', $this->config->get('system', 'db_log_index_watch'));
+ $denylist = explode(',', $this->config->get('system', 'db_log_index_denylist'));
while ($row = $this->fetch($r)) {
- if ((intval($this->configCache->get('system', 'db_loglimit_index')) > 0)) {
+ if ((intval($this->config->get('system', 'db_loglimit_index')) > 0)) {
$log = (in_array($row['key'], $watchlist) &&
- ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index'))));
+ ($row['rows'] >= intval($this->config->get('system', 'db_loglimit_index'))));
} else {
$log = false;
}
- if ((intval($this->configCache->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index_high')))) {
+ if ((intval($this->config->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($this->config->get('system', 'db_loglimit_index_high')))) {
$log = true;
}
if ($log) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
@file_put_contents(
- $this->configCache->get('system', 'db_log_index'),
+ $this->config->get('system', 'db_log_index'),
DateTimeFormat::utcNow() . "\t" .
$row['key'] . "\t" . $row['rows'] . "\t" . $row['Extra'] . "\t" .
basename($backtrace[1]["file"]) . "\t" .
$orig_sql = $sql;
- if ($this->configCache->get('system', 'db_callstack') !== null) {
+ if ($this->config->get('system', 'db_callstack') !== null) {
$sql = "/*" . System::callstack() . " */ " . $sql;
}
$this->profiler->stopRecording();
- if ($this->configCache->get('system', 'db_log')) {
+ if ($this->config->get('system', 'db_log')) {
$stamp2 = microtime(true);
$duration = (float)($stamp2 - $stamp1);
- if (($duration > $this->configCache->get('system', 'db_loglimit'))) {
+ if (($duration > $this->config->get('system', 'db_loglimit'))) {
$duration = round($duration, 3);
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
@file_put_contents(
- $this->configCache->get('system', 'db_log'),
+ $this->config->get('system', 'db_log'),
DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
basename($backtrace[1]["file"]) . "\t" .
$backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
use Friendica\Core\Addon;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\ValueObject\Cache;
-use Friendica\Core\Logger;
use Friendica\Core\Renderer;
use Friendica\Core\Update;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Core\Config\Factory\Config;
-use Friendica\Model\Register;
use Friendica\Module\BaseAdmin;
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPException\ServiceUnavailableException;
namespace Friendica\Util;
-use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\System;
use Psr\Container\ContainerExceptionInterface;
return $this->rendertime;
}
- /**
- * Updates the enabling of the current profiler
- *
- * Note: The reason there are two different ways of updating the configuration of this class is because it can
- * be used even with no available database connection which IManageConfigValues doesn't ensure.
- *
- * @param IManageConfigValues $config
- */
- public function update(IManageConfigValues $config)
+ public function __construct(IManageConfigValues $config)
{
- $this->enabled = (bool) $config->get('system', 'profiler') ?? false;
- $this->rendertime = (bool) $config->get('rendertime', 'callstack') ?? false;
- }
-
- /**
- * Note: The reason we are using a Config Cache object to initialize this class is to ensure it'll work even with no
- * available database connection.
- *
- * @param \Friendica\Core\Config\ValueObject\Cache $configCache The configuration cache
- */
- public function __construct(Cache $configCache)
- {
- $this->enabled = (bool) $configCache->get('system', 'profiler') ?? false;
- $this->rendertime = (bool) $configCache->get('rendertime', 'callstack') ?? false;
+ $this->enabled = (bool)$config->get('system', 'profiler') ?? false;
+ $this->rendertime = (bool)$config->get('rendertime', 'callstack') ?? false;
$this->reset();
}
$timestamp = array_pop($this->timestamps);
$duration = floatval(microtime(true) - $timestamp['stamp'] - $timestamp['credit']);
- $value = $timestamp['value'];
+ $value = $timestamp['value'];
foreach ($this->timestamps as $key => $stamp) {
$this->timestamps[$key]['credit'] += $duration;
$this->performance[$value] = 0;
}
- $this->performance[$value] += (float) $duration;
- $this->performance['marktime'] += (float) $duration;
+ $this->performance[$value] += (float)$duration;
+ $this->performance['marktime'] += (float)$duration;
if (!isset($this->callstack[$value][$callstack])) {
// Prevent ugly E_NOTICE
$this->callstack[$value][$callstack] = 0;
}
- $this->callstack[$value][$callstack] += (float) $duration;
+ $this->callstack[$value][$callstack] += (float)$duration;
}
/**
$this->performance[$value] = 0;
}
- $this->performance[$value] += (float) $duration;
- $this->performance['marktime'] += (float) $duration;
+ $this->performance[$value] += (float)$duration;
+ $this->performance['marktime'] += (float)$duration;
if (!isset($this->callstack[$value][$callstack])) {
// Prevent ugly E_NOTICE
$this->callstack[$value][$callstack] = 0;
}
- $this->callstack[$value][$callstack] += (float) $duration;
+ $this->callstack[$value][$callstack] += (float)$duration;
}
/**
*/
public function resetPerformance()
{
- $this->performance = [];
- $this->performance['start'] = microtime(true);
- $this->performance['ready'] = 0;
- $this->performance['database'] = 0;
+ $this->performance = [];
+ $this->performance['start'] = microtime(true);
+ $this->performance['ready'] = 0;
+ $this->performance['database'] = 0;
$this->performance['database_write'] = 0;
- $this->performance['cache'] = 0;
- $this->performance['cache_write'] = 0;
- $this->performance['network'] = 0;
- $this->performance['file'] = 0;
- $this->performance['rendering'] = 0;
- $this->performance['session'] = 0;
- $this->performance['marktime'] = 0;
- $this->performance['marktime'] = microtime(true);
- $this->performance['classcreate'] = 0;
- $this->performance['classinit'] = 0;
- $this->performance['init'] = 0;
- $this->performance['content'] = 0;
+ $this->performance['cache'] = 0;
+ $this->performance['cache_write'] = 0;
+ $this->performance['network'] = 0;
+ $this->performance['file'] = 0;
+ $this->performance['rendering'] = 0;
+ $this->performance['session'] = 0;
+ $this->performance['marktime'] = microtime(true);
+ $this->performance['classcreate'] = 0;
+ $this->performance['classinit'] = 0;
+ $this->performance['init'] = 0;
+ $this->performance['content'] = 0;
}
/**
*/
public function resetCallstack()
{
- $this->callstack = [];
- $this->callstack['database'] = [];
+ $this->callstack = [];
+ $this->callstack['database'] = [];
$this->callstack['database_write'] = [];
- $this->callstack['cache'] = [];
- $this->callstack['cache_write'] = [];
- $this->callstack['network'] = [];
- $this->callstack['file'] = [];
- $this->callstack['rendering'] = [];
- $this->callstack['session'] = [];
+ $this->callstack['cache'] = [];
+ $this->callstack['cache_write'] = [];
+ $this->callstack['network'] = [];
+ $this->callstack['file'] = [];
+ $this->callstack['rendering'] = [];
+ $this->callstack['session'] = [];
}
/**
* Returns the rendertime string
+ *
* @param float $limit Minimal limit for displaying the execution duration
*
* @return string the rendertime
$logger->info(
$message,
[
- 'action' => 'profiling',
- 'database_read' => round($this->get('database') - $this->get('database_write'), 3),
+ 'action' => 'profiling',
+ 'database_read' => round($this->get('database') - $this->get('database_write'), 3),
'database_write' => round($this->get('database_write'), 3),
- 'cache_read' => round($this->get('cache'), 3),
- 'cache_write' => round($this->get('cache_write'), 3),
- 'network_io' => round($this->get('network'), 2),
- 'file_io' => round($this->get('file'), 2),
- 'other_io' => round($duration - ($this->get('database')
- + $this->get('cache') + $this->get('cache_write')
- + $this->get('network') + $this->get('file')), 2),
- 'total' => round($duration, 2)
+ 'cache_read' => round($this->get('cache'), 3),
+ 'cache_write' => round($this->get('cache_write'), 3),
+ 'network_io' => round($this->get('network'), 2),
+ 'file_io' => round($this->get('file'), 2),
+ 'other_io' => round($duration - ($this->get('database')
+ + $this->get('cache') + $this->get('cache_write')
+ + $this->get('network') + $this->get('file')), 2),
+ 'total' => round($duration, 2)
]
);
*
* @param string $id Identifier of the entry to look for.
*
- * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
+ * @return float Entry.
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
- * @return float Entry.
+ * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
*/
public function get(string $id): float
{
use Dice\Dice;
use Friendica\App\Arguments;
use Friendica\App\Router;
+use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Config\Util\ConfigFileManager;
-use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Type\Memory;
use Friendica\Database\Database;
]);
DI::init($this->dice);
- $configCache = $this->dice->create(Cache::class);
- $configCache->set('database', 'disable_pdo', true);
+ $config = $this->dice->create(IManageConfigValues::class);
+ $config->set('database', 'disable_pdo', true);
/** @var Database $dba */
$dba = $this->dice->create(Database::class);
--- /dev/null
+<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Test\Util;
+
+use Friendica\Core\Config\Model\Config;
+use Friendica\Core\Config\Util\ConfigFileManager;
+use Friendica\Core\Config\ValueObject\Cache;
+use Friendica\Database\Database;
+use Friendica\Database\Definition\DbaDefinition;
+use Friendica\Database\Definition\ViewDefinition;
+use Friendica\Test\DatabaseTestTrait;
+use Friendica\Test\Util\Database\StaticDatabase;
+use Friendica\Util\Profiler;
+
+trait CreateDatabaseTrait
+{
+ use DatabaseTestTrait;
+ use VFSTrait;
+
+ public function getDbInstance(): Database
+ {
+ $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
+ $config = new Config($configFileManager, new Cache([
+ 'database' => [
+ 'disable_pdo' => true
+ ],
+ ]));
+
+ $database = new StaticDatabase($config, new Profiler($config), (new DbaDefinition($this->root->url()))->load(), (new ViewDefinition($this->root->url()))->load());
+ $database->setTestmode(true);
+
+ return $database;
+ }
+}
use Friendica\App;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
+use Friendica\Core\Config\Model\Config;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Lock\Capability\ICanLock;
self::assertArrayHasKey('system', $configCache->getAll());
}
- /**
- * Test the construction of a profiler class with DI
- */
- public function testProfiler()
- {
- /** @var Profiler $profiler */
- $profiler = $this->dice->create(Profiler::class);
-
- self::assertInstanceOf(Profiler::class, $profiler);
-
- $configCache = new Cache([
- 'system' => [
- 'profiler' => true,
- ],
- 'rendertime' => [
- 'callstack' => true,
- ]
- ]);
-
- // create new DI-library because of shared instance rule (so the Profiler wouldn't get created twice)
- $this->dice = new Dice();
- $profiler = $this->dice->create(Profiler::class, [$configCache]);
-
- self::assertInstanceOf(Profiler::class, $profiler);
- self::assertTrue($profiler->isRendertime());
- }
-
public function testDatabase()
{
// PDO needs to be disabled for PHP 7.2, see https://jira.mariadb.org/browse/MDEV-24121
use Detection\MobileDetect;
use Friendica\App\Arguments;
use Friendica\App\Mode;
-use Friendica\Core\Config\ValueObject\Cache;
+use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait;
private $databaseMock;
/**
- * @var Cache|MockInterface
+ * @var IManageConfigValues|MockInterface
*/
- private $configCacheMock;
+ private $configMock;
protected function setUp(): void
{
$this->setUpVfsDir();
- $this->databaseMock = Mockery::mock(Database::class);
- $this->configCacheMock = Mockery::mock(Cache::class);
+ $this->databaseMock = Mockery::mock(Database::class);
+ $this->configMock = Mockery::mock(IManageConfigValues::class);
}
public function testItEmpty()
self::assertFalse($this->root->hasChild('config/local.config.php'));
- $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configCacheMock);
+ $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configMock);
self::assertTrue($mode->isInstall());
self::assertFalse($mode->isNormal());
{
$this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
- $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configCacheMock);
+ $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configMock);
self::assertFalse($mode->isNormal());
self::assertTrue($mode->isInstall());
public function testWithMaintenanceMode()
{
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
- $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
+ $this->configMock->shouldReceive('get')->with('system', 'maintenance')
->andReturn(true)->once();
- $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configCacheMock);
+ $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configMock);
self::assertFalse($mode->isNormal());
self::assertFalse($mode->isInstall());
public function testNormalMode()
{
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
- $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
+ $this->configMock->shouldReceive('get')->with('system', 'maintenance')
->andReturn(false)->once();
- $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configCacheMock);
+ $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configMock);
self::assertTrue($mode->isNormal());
self::assertFalse($mode->isInstall());
public function testDisabledMaintenance()
{
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
- $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
+ $this->configMock->shouldReceive('get')->with('system', 'maintenance')
->andReturn(false)->once();
- $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configCacheMock);
+ $mode = (new Mode())->determine($this->root->url(), $this->databaseMock, $this->configMock);
self::assertTrue($mode->isNormal());
self::assertFalse($mode->isInstall());
{
$mode = new Mode();
- $modeNew = $mode->determine('', $this->databaseMock, $this->configCacheMock);
+ $modeNew = $mode->determine('', $this->databaseMock, $this->configMock);
self::assertNotSame($modeNew, $mode);
}
$configFactory = new Config();
$configFileManager = (new Config())->createConfigFileManager($this->root->url(), []);
$configCache = $configFactory->createCache($configFileManager);
+ $config = new \Friendica\Core\Config\Model\Config($configFileManager, $configCache);
$dbaDefinition = (new DbaDefinition($configCache->get('system', 'basepath')))->load();
$viewDefinition = (new ViewDefinition($configCache->get('system', 'basepath')))->load();
- $dba = new StaticDatabase($configCache, $profiler, $dbaDefinition, $viewDefinition);
+ $dba = new StaticDatabase($config, $profiler, $dbaDefinition, $viewDefinition);
$this->cache = new Cache\Type\DatabaseCache('database', $dba);
return $this->cache;
namespace Friendica\Test\src\Core\KeyValueStorage;
-use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\KeyValueStorage\Capabilities\IManageKeyValuePairs;
use Friendica\Core\KeyValueStorage\Type\DBKeyValueStorage;
use Friendica\Database\Database;
-use Friendica\Database\Definition\DbaDefinition;
-use Friendica\Database\Definition\ViewDefinition;
-use Friendica\Test\DatabaseTestTrait;
-use Friendica\Test\Util\Database\StaticDatabase;
-use Friendica\Util\BasePath;
-use Friendica\Util\Profiler;
+use Friendica\Test\Util\CreateDatabaseTrait;
class DBKeyValueStorageTest extends KeyValueStorageTest
{
- use DatabaseTestTrait;
+ use CreateDatabaseTrait;
/** @var Database */
protected $database;
{
parent::setUp();
+ $this->setUpVfsDir();
$this->setUpDb();
}
public function getInstance(): IManageKeyValuePairs
{
- $cache = new Cache();
- $cache->set('database', 'disable_pdo', true);
-
- $basePath = new BasePath(dirname(__FILE__, 5), $_SERVER);
-
- $this->database = new StaticDatabase($cache, new Profiler($cache), (new DbaDefinition($basePath->getPath()))->load(), (new ViewDefinition($basePath->getPath()))->load());
- $this->database->setTestmode(true);
+ $this->database = $this->getDbInstance();
return new DBKeyValueStorage($this->database);
}
namespace Friendica\Test\src\Core\Lock;
-use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Lock\Type\DatabaseLock;
-use Friendica\Database\Database;
-use Friendica\Database\Definition\DbaDefinition;
-use Friendica\Database\Definition\ViewDefinition;
-use Friendica\Test\DatabaseTestTrait;
-use Friendica\Test\Util\Database\StaticDatabase;
-use Friendica\Test\Util\VFSTrait;
-use Friendica\Util\BasePath;
-use Friendica\Util\Profiler;
+use Friendica\Test\Util\CreateDatabaseTrait;
class DatabaseLockDriverTest extends LockTest
{
- use VFSTrait;
- use DatabaseTestTrait;
+ use CreateDatabaseTrait;
protected $pid = 123;
- /** @var Database */
- protected $database;
-
protected function setUp(): void
{
$this->setUpVfsDir();
protected function getInstance()
{
- $cache = new Cache();
- $cache->set('database', 'disable_pdo', true);
-
- $basePath = new BasePath(dirname(__FILE__, 5), $_SERVER);
-
- $this->database = new StaticDatabase($cache, new Profiler($cache), (new DbaDefinition($basePath->getPath()))->load(), (new ViewDefinition($basePath->getPath()))->load());
- $this->database->setTestmode(true);
-
- return new DatabaseLock($this->database, $this->pid);
+ return new DatabaseLock($this->getDbInstance(), $this->pid);
}
protected function tearDown(): void
{
- $this->tearDownDb();
-
parent::tearDown();
+
+ $this->tearDownDb();
}
}
namespace Friendica\Test\src\Core\Storage;
-use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Storage\Type\Database;
-use Friendica\Database\Definition\DbaDefinition;
-use Friendica\Database\Definition\ViewDefinition;
-use Friendica\Test\DatabaseTestTrait;
-use Friendica\Test\Util\Database\StaticDatabase;
-use Friendica\Test\Util\VFSTrait;
-use Friendica\Util\Profiler;
-use Psr\Log\NullLogger;
+use Friendica\Test\Util\CreateDatabaseTrait;
class DatabaseStorageTest extends StorageTest
{
- use DatabaseTestTrait;
- use VFSTrait;
+ use CreateDatabaseTrait;
protected function setUp(): void
{
protected function getInstance()
{
- $profiler = \Mockery::mock(Profiler::class);
- $profiler->shouldReceive('startRecording');
- $profiler->shouldReceive('stopRecording');
- $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
-
- // load real config to avoid mocking every config-entry which is related to the Database class
- $configFactory = new Config();
- $configFileManager = (new Config())->createConfigFileManager($this->root->url());
- $configCache = $configFactory->createCache($configFileManager);
-
- $dbaDefinition = (new DbaDefinition($configCache->get('system', 'basepath')))->load();
- $viewDefinition = (new ViewDefinition($configCache->get('system', 'basepath')))->load();
-
- $dba = new StaticDatabase($configCache, $profiler, $dbaDefinition, $viewDefinition);
-
- return new Database($dba);
+ return new Database($this->getDbInstance());
}
protected function tearDown(): void
{
- $this->tearDownDb();
-
parent::tearDown();
+
+ $this->tearDownDb();
}
}
use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Storage\Type;
use Friendica\Test\DatabaseTest;
+use Friendica\Test\Util\CreateDatabaseTrait;
use Friendica\Test\Util\Database\StaticDatabase;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\Profiler;
class StorageManagerTest extends DatabaseTest
{
- use VFSTrait;
+ use CreateDatabaseTrait;
- /** @var Database */
- private $dba;
/** @var IManageConfigValues */
private $config;
/** @var LoggerInterface */
/** @var L10n */
private $l10n;
+ /** @var Database */
+ protected $database;
+
protected function setUp(): void
{
parent::setUp();
$this->setUpVfsDir();
+ $this->setUpDb();
+
vfsStream::newDirectory(Type\FilesystemConfig::DEFAULT_BASE_FOLDER, 0777)->at($this->root);
$this->logger = new NullLogger();
- $profiler = \Mockery::mock(Profiler::class);
- $profiler->shouldReceive('startRecording');
- $profiler->shouldReceive('stopRecording');
- $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
-
- // load real config to avoid mocking every config-entry which is related to the Database class
$configFactory = new Config();
$configFileManager = $configFactory->createConfigFileManager($this->root->url());
$configCache = $configFactory->createCache($configFileManager);
- $dbaDefinition = (new DbaDefinition($configCache->get('system', 'basepath')))->load();
- $viewDefinition = (new ViewDefinition($configCache->get('system', 'basepath')))->load();
-
- $this->dba = new StaticDatabase($configCache, $profiler, $dbaDefinition, $viewDefinition);
-
$this->config = new \Friendica\Core\Config\Model\Config($configFileManager, $configCache);
$this->config->set('storage', 'name', 'Database');
$this->config->set('storage', 'filesystem_path', $this->root->getChild(Type\FilesystemConfig::DEFAULT_BASE_FOLDER)
->url());
$this->l10n = \Mockery::mock(L10n::class);
+
+ $this->database = $this->getDbInstance();
}
protected function tearDown(): void
*/
public function testInstance()
{
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
self::assertInstanceOf(StorageManager::class, $storageManager);
}
$this->config->set('storage', 'name', $name);
}
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
if ($interface === ICanWriteToStorage::class) {
$storage = $storageManager->getWritableStorageByName($name);
*/
public function testIsValidBackend($name, $valid, $interface, $assert, $assertName)
{
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
// true in every of the backends
self::assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
*/
public function testListBackends()
{
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
}
static::markTestSkipped('only works for ICanWriteToStorage');
}
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
$selBackend = $storageManager->getWritableStorageByName($name);
$storageManager->setBackend($selBackend);
$this->expectException(InvalidClassStorageException::class);
}
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
self::assertInstanceOf($assert, $storageManager->getBackend());
}
->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null]);
DI::init($dice);
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
self::assertTrue($storageManager->register(SampleStorageBackend::class));
->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null]);
DI::init($dice);
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
self::assertTrue($storageManager->register(SampleStorageBackend::class));
self::markTestSkipped("No user backend");
}
- $this->loadFixture(__DIR__ . '/../../../../datasets/storage/database.fixture.php', $this->dba);
+ $this->loadFixture(__DIR__ . '/../../../../datasets/storage/database.fixture.php', $this->database);
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
$storage = $storageManager->getWritableStorageByName($name);
$storageManager->move($storage);
- $photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
+ $photos = $this->database->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
- while ($photo = $this->dba->fetch($photos)) {
+ while ($photo = $this->database->fetch($photos)) {
self::assertEmpty($photo['data']);
$storage = $storageManager->getByName($photo['backend-class']);
$this->expectException(InvalidClassStorageException::class);
$this->expectExceptionMessage('Backend SystemResource is not valid');
- $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, false);
+ $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false);
$storage = $storageManager->getWritableStorageByName(SystemResource::getName());
$storageManager->move($storage);
}
use Friendica\App\BaseURL;
use Friendica\Core\Config\Capability\IManageConfigValues;
-use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
use Friendica\Core\Config\Model\Config;
-use Friendica\Core\Config\Model\ConfigTransaction;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Test\MockedTest;
namespace Friendica\Test\src\Util;
-use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Test\MockedTest;
use Friendica\Util\Profiler;
*/
public function testSetUp()
{
- $configCache = \Mockery::mock(Cache::class);
- $configCache->shouldReceive('get')
+ $config = \Mockery::mock(IManageConfigValues::class);
+ $config->shouldReceive('get')
->withAnyArgs()
->andReturn(true)
->twice();
- $profiler = new Profiler($configCache);
+ $profiler = new Profiler($config);
self::assertInstanceOf(Profiler::class, $profiler);
}
*/
public function testSaveTimestamp($timestamp, $name, array $functions)
{
- $configCache = \Mockery::mock(Cache::class);
- $configCache->shouldReceive('get')
+ $config = \Mockery::mock(IManageConfigValues::class);
+ $config->shouldReceive('get')
->withAnyArgs()
->andReturn(true)
->twice();
- $profiler = new Profiler($configCache);
+ $profiler = new Profiler($config);
foreach ($functions as $function) {
$profiler->saveTimestamp($timestamp, $name, $function);
*/
public function testReset($timestamp, $name)
{
- $configCache = \Mockery::mock(Cache::class);
- $configCache->shouldReceive('get')
+ $config = \Mockery::mock(IManageConfigValues::class);
+ $config->shouldReceive('get')
->withAnyArgs()
->andReturn(true)
->twice();
- $profiler = new Profiler($configCache);
+ $profiler = new Profiler($config);
$profiler->saveTimestamp($timestamp, $name);
$profiler->reset();
->shouldReceive('info')
->once();
- $configCache = \Mockery::mock(Cache::class);
- $configCache->shouldReceive('get')
+ $config = \Mockery::mock(IManageConfigValues::class);
+ $config->shouldReceive('get')
->withAnyArgs()
->andReturn(true)
->twice();
- $profiler = new Profiler($configCache);
+ $profiler = new Profiler($config);
foreach ($data as $perf => $items) {
foreach ($items['functions'] as $function) {
}
}
}
-
- /**
- * Test different enable and disable states of the profiler
- */
- public function testEnableDisable()
- {
- $configCache = \Mockery::mock(Cache::class);
- $configCache->shouldReceive('get')
- ->with('system', 'profiler')
- ->andReturn(true)
- ->once();
- $configCache->shouldReceive('get')
- ->with('rendertime', 'callstack')
- ->andReturn(false)
- ->once();
-
- $profiler = new Profiler($configCache);
-
- self::assertFalse($profiler->isRendertime());
- self::assertEmpty($profiler->getRendertimeString());
-
- $profiler->saveTimestamp(time(), 'network', 'test1');
-
- $config = \Mockery::mock(IManageConfigValues::class);
- $config->shouldReceive('get')
- ->with('system', 'profiler')
- ->andReturn(false)
- ->once();
- $config->shouldReceive('get')
- ->with('rendertime', 'callstack')
- ->andReturn(false)
- ->once();
-
- $profiler->update($config);
-
- self::assertFalse($profiler->isRendertime());
- self::assertEmpty($profiler->getRendertimeString());
-
- $config->shouldReceive('get')
- ->with('system', 'profiler')
- ->andReturn(true)
- ->once();
- $config->shouldReceive('get')
- ->with('rendertime', 'callstack')
- ->andReturn(true)
- ->once();
-
- $profiler->update($config);
-
- $profiler->saveTimestamp(time(), 'database', 'test2');
-
- self::assertTrue($profiler->isRendertime());
- $output = $profiler->getRendertimeString();
- self::assertMatchesRegularExpression('/test1: \d+/', $output);
- self::assertMatchesRegularExpression('/test2: \d+/', $output);
- }
}