1
0
Fork 0

Introduce DICE

- Adding dice library
- Adding dependency config

- Removing Factories
- Refactoring App\Mode constructor
- Refactoring App\Router constructor
- Refactoring BasePath for DI usage
- Refactoring ConfigFileLoader constructor
- Refactoring Profiler constructor

- Adjust entrypoints (index, console, worker, ..)

- Adding functional test for DI
- Fix tests because of refactorings
This commit is contained in:
Philipp Holzer 2019-07-21 01:22:10 +02:00
commit 55999730e0
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
28 changed files with 563 additions and 308 deletions

View file

@ -4,32 +4,56 @@ namespace Friendica\Test\src\App;
use Friendica\App\Mode;
use Friendica\Core\Config;
use Friendica\Database\Database;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\DBAMockTrait;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\BasePath;
use Mockery\MockInterface;
class ModeTest extends MockedTest
{
use VFSTrait;
use DBAMockTrait;
/**
* @var BasePath|MockInterface
*/
private $basePathMock;
/**
* @var Database|MockInterface
*/
private $databaseMock;
/**
* @var Config\Cache\ConfigCache|MockInterface
*/
private $configCacheMock;
public function setUp()
{
parent::setUp();
$this->setUpVfsDir();
$this->basePathMock = \Mockery::mock(BasePath::class);
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
$this->databaseMock = \Mockery::mock(Database::class);
$this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class);
}
public function testItEmpty()
{
$mode = new Mode($this->root->url());
$mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
$this->assertTrue($mode->isInstall());
$this->assertFalse($mode->isNormal());
}
public function testWithoutConfig()
{
$mode = new Mode($this->root->url());
$mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
$this->assertTrue($this->root->hasChild('config/local.config.php'));
@ -45,15 +69,11 @@ class ModeTest extends MockedTest
$this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testWithoutDatabase()
{
$this->mockConnected(false, 1);
$this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
$mode = new Mode($this->root->url());
$mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
$mode->determine();
$this->assertFalse($mode->isNormal());
@ -63,16 +83,13 @@ class ModeTest extends MockedTest
$this->assertFalse($mode->has(Mode::DBAVAILABLE));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testWithoutDatabaseSetup()
{
$this->mockConnected(true, 1);
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', false, 1);
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
$this->databaseMock->shouldReceive('fetchFirst')
->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
$mode = new Mode($this->root->url());
$mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
$mode->determine();
$this->assertFalse($mode->isNormal());
@ -81,25 +98,15 @@ class ModeTest extends MockedTest
$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->databaseMock->shouldReceive('connected')->andReturn(true)->once();
$this->databaseMock->shouldReceive('fetchFirst')
->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
->andReturn(true)->once();
$config = \Mockery::mock(Config\Configuration::class);
$config
->shouldReceive('get')
->with('system', 'maintenance', null, false)
->andReturn(true)
->once();
// Initialize empty Config
Config::init($config);
$mode = new Mode($this->root->url());
$mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
$mode->determine();
$this->assertFalse($mode->isNormal());
@ -109,25 +116,18 @@ class ModeTest extends MockedTest
$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->databaseMock->shouldReceive('connected')->andReturn(true)->once();
$this->databaseMock->shouldReceive('fetchFirst')
->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
->andReturn(false)->once();
$this->databaseMock->shouldReceive('selectFirst')
->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
->andReturn(false)->once();
$config = \Mockery::mock(Config\Configuration::class);
$config
->shouldReceive('get')
->with('system', 'maintenance', null, false)
->andReturn(false)
->once();
// Initialize empty Config
Config::init($config);
$mode = new Mode($this->root->url());
$mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
$mode->determine();
$this->assertTrue($mode->isNormal());

View file

@ -21,7 +21,7 @@ class DBATest extends DatabaseTest
$logger = Factory\LoggerFactory::create('test', self::$dba, $config, self::$profiler);
$baseUrl = new BaseURL($config, $_SERVER);
$router = new App\Router();
$l10n = new L10n(L10n::detectLanguage($config->get('system', 'language')),
$l10n = new L10n($config,
self::$dba,
$logger);
$this->app = new App(self::$dba, $config, self::$mode, $router, $baseUrl, $logger, self::$profiler, $l10n, false);

View file

@ -25,7 +25,7 @@ class DBStructureTest extends DatabaseTest
$logger = Factory\LoggerFactory::create('test', self::$dba, $config, self::$profiler);
$baseUrl = new BaseURL($config, $_SERVER);
$router = new App\Router();
$l10n = new L10n(L10n::detectLanguage($config->get('system', 'language')),
$l10n = new L10n($config,
self::$dba,
$logger);
$this->app = new App(self::$dba, $config, self::$mode, $router, $baseUrl, $logger, self::$profiler, $l10n, false);

View file

@ -58,7 +58,8 @@ class BasePathTest extends MockedTest
*/
public function testDetermineBasePath(array $server, $input, $output)
{
$this->assertEquals($output, BasePath::create($input, $server));
$basepath = new BasePath($input, $server);
$this->assertEquals($output, $basepath->getPath());
}
/**
@ -68,6 +69,7 @@ class BasePathTest extends MockedTest
*/
public function testFailedBasePath()
{
BasePath::create('/now23452sgfgas', []);
$basepath = new BasePath('/now23452sgfgas', []);
$basepath->getPath();
}
}

View file

@ -2,31 +2,21 @@
namespace Friendica\Test\src\Util\Config;
use Friendica\App;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\ConfigFileLoader;
use Mockery\MockInterface;
use org\bovigo\vfs\vfsStream;
class ConfigFileLoaderTest extends MockedTest
{
use VFSTrait;
/**
* @var App\Mode|MockInterface
*/
private $mode;
protected function setUp()
{
parent::setUp();
$this->setUpVfsDir();
$this->mode = \Mockery::mock(App\Mode::class);
$this->mode->shouldReceive('isInstall')->andReturn(true);
}
/**
@ -34,7 +24,9 @@ class ConfigFileLoaderTest extends MockedTest
*/
public function testLoadConfigFiles()
{
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$this->delConfigFile('local.config.php');
$configFileLoader = new ConfigFileLoader($this->root->url());
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);
@ -55,7 +47,7 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent('<?php return true;');
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url());
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);
@ -79,7 +71,7 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url());
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);
@ -111,7 +103,7 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url());
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);
@ -142,7 +134,7 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root)
->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url());
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);
@ -191,7 +183,7 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url());
$conf = $configFileLoader->loadAddonConfig('test');
@ -223,7 +215,7 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.config.php'));
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url());
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);
@ -252,7 +244,7 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.ini.php'));
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url());
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);
@ -281,7 +273,7 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.ini.php'));
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url());
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);

View file

@ -2,6 +2,8 @@
namespace Friendica\Test\src\Util;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Config\Configuration;
use Friendica\Test\MockedTest;
use Friendica\Util\Profiler;
use Mockery\MockInterface;
@ -26,7 +28,12 @@ class ProfilerTest extends MockedTest
*/
public function testSetUp()
{
$profiler = new Profiler(true, true);
$configCache = \Mockery::mock(ConfigCache::class);
$configCache->shouldReceive('get')
->withAnyArgs()
->andReturn(true)
->twice();
$profiler = new Profiler($configCache);
}
/**
@ -96,7 +103,13 @@ class ProfilerTest extends MockedTest
*/
public function testSaveTimestamp($timestamp, $name, array $functions)
{
$profiler = new Profiler(true, true);
$configCache = \Mockery::mock(ConfigCache::class);
$configCache->shouldReceive('get')
->withAnyArgs()
->andReturn(true)
->twice();
$profiler = new Profiler($configCache);
foreach ($functions as $function) {
$profiler->saveTimestamp($timestamp, $name, $function);
@ -111,7 +124,13 @@ class ProfilerTest extends MockedTest
*/
public function testReset($timestamp, $name, array $functions)
{
$profiler = new Profiler(true, true);
$configCache = \Mockery::mock(ConfigCache::class);
$configCache->shouldReceive('get')
->withAnyArgs()
->andReturn(true)
->twice();
$profiler = new Profiler($configCache);
$profiler->saveTimestamp($timestamp, $name);
$profiler->reset();
@ -168,7 +187,13 @@ class ProfilerTest extends MockedTest
->shouldReceive('info')
->once();
$profiler = new Profiler(true, true);
$configCache = \Mockery::mock(ConfigCache::class);
$configCache->shouldReceive('get')
->withAnyArgs()
->andReturn(true)
->twice();
$profiler = new Profiler($configCache);
foreach ($data as $perf => $items) {
foreach ($items['functions'] as $function) {
@ -193,19 +218,48 @@ class ProfilerTest extends MockedTest
*/
public function testEnableDisable()
{
$profiler = new Profiler(true, false);
$configCache = \Mockery::mock(ConfigCache::class);
$configCache->shouldReceive('get')
->with('system', 'profiler')
->andReturn(true)
->once();
$configCache->shouldReceive('get')
->with('rendertime', 'callstack')
->andReturn(false)
->once();
$profiler = new Profiler($configCache);
$this->assertFalse($profiler->isRendertime());
$this->assertEmpty($profiler->getRendertimeString());
$profiler->saveTimestamp(time(), 'network', 'test1');
$profiler->update(false, false);
$config = \Mockery::mock(Configuration::class);
$config->shouldReceive('get')
->with('system', 'profiler')
->andReturn(false)
->once();
$config->shouldReceive('get')
->with('rendertime', 'callstack')
->andReturn(false)
->once();
$profiler->update($config);
$this->assertFalse($profiler->isRendertime());
$this->assertEmpty($profiler->getRendertimeString());
$profiler->update(true, true);
$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');