Fixing some AutomaticInstallationConsoleTests

This commit is contained in:
Philipp Holzer 2019-03-13 23:05:33 +01:00 committed by Hypolite Petovan
parent bef952bd16
commit c5973beca4
3 changed files with 122 additions and 57 deletions

View file

@ -39,8 +39,9 @@ trait AppMockTrait
* Mock the App * Mock the App
* *
* @param vfsStreamDirectory $root The root directory * @param vfsStreamDirectory $root The root directory
* @param Config\Cache\ConfigCache $configCache
*/ */
public function mockApp($root) public function mockApp(vfsStreamDirectory $root, $configCache = null, $raw = false)
{ {
$this->configMock = \Mockery::mock(Config\Cache\IConfigCache::class); $this->configMock = \Mockery::mock(Config\Cache\IConfigCache::class);
$this->mode = \Mockery::mock(App\Mode::class); $this->mode = \Mockery::mock(App\Mode::class);
@ -48,7 +49,7 @@ trait AppMockTrait
// Disable the adapter // Disable the adapter
$configAdapterMock->shouldReceive('isConnected')->andReturn(false); $configAdapterMock->shouldReceive('isConnected')->andReturn(false);
$config = new Config\Configuration($this->configMock, $configAdapterMock); $config = new Config\Configuration((isset($configCache) ? $configCache : $this->configMock), $configAdapterMock);
// Initialize empty Config // Initialize empty Config
Config::init($config); Config::init($config);
@ -62,9 +63,33 @@ trait AppMockTrait
->shouldReceive('getMode') ->shouldReceive('getMode')
->andReturn($this->mode); ->andReturn($this->mode);
$this->configMock $this->profilerMock = \Mockery::mock(Profiler::class);
->shouldReceive('has') $this->profilerMock->shouldReceive('saveTimestamp');
->andReturn(true);
$this->app
->shouldReceive('getConfigCache')
->andReturn((isset($configCache) ? $configCache : $this->configMock));
$this->app
->shouldReceive('getTemplateEngine')
->andReturn(new FriendicaSmartyEngine());
$this->app
->shouldReceive('getCurrentTheme')
->andReturn('Smarty3');
$this->app
->shouldReceive('getProfiler')
->andReturn($this->profilerMock);
$this->app
->shouldReceive('getBaseUrl')
->andReturnUsing(function () {
return $this->configMock->get('system', 'url');
});
BaseObject::setApp($this->app);
if ($raw) {
return;
}
$this->configMock $this->configMock
->shouldReceive('get') ->shouldReceive('get')
->with('database', 'hostname') ->with('database', 'hostname')
@ -89,26 +114,5 @@ trait AppMockTrait
->shouldReceive('get') ->shouldReceive('get')
->with('system', 'theme') ->with('system', 'theme')
->andReturn('system_theme'); ->andReturn('system_theme');
$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('getBaseUrl')
->andReturn('http://friendica.local');
$this->app
->shouldReceive('getProfiler')
->andReturn($this->profilerMock);
BaseObject::setApp($this->app);
} }
} }

View file

@ -25,7 +25,7 @@ trait VFSTrait
]; ];
// create a virtual directory and copy all needed files and folders to it // create a virtual directory and copy all needed files and folders to it
$this->root = vfsStream::setup('friendica', null, $structure); $this->root = vfsStream::setup('friendica', 0777, $structure);
$this->setConfigFile('defaults.config.php'); $this->setConfigFile('defaults.config.php');
$this->setConfigFile('settings.config.php'); $this->setConfigFile('settings.config.php');

View file

@ -4,11 +4,12 @@ namespace Friendica\Test\src\Core\Console;
use Friendica\Core\Config\Cache\ConfigCache; use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Console\AutomaticInstallation; use Friendica\Core\Console\AutomaticInstallation;
use Friendica\Core\Installer; use Friendica\Core\Logger;
use Friendica\Test\Util\DBAMockTrait; use Friendica\Test\Util\DBAMockTrait;
use Friendica\Test\Util\DBStructureMockTrait; use Friendica\Test\Util\DBStructureMockTrait;
use Friendica\Test\Util\L10nMockTrait; use Friendica\Test\Util\L10nMockTrait;
use Friendica\Test\Util\RendererMockTrait; use Friendica\Test\Util\RendererMockTrait;
use Friendica\Util\Logger\VoidLogger;
use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamFile; use org\bovigo\vfs\vfsStreamFile;
@ -60,24 +61,28 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
/** /**
* Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.config.php * Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.config.php
* *
* @param bool $withDb if true, DB will get saved too * @param ConfigCache $config The config cache of this test
* *
* @return array The arguments to pass to the mock for 'replaceMacros()' * @return array The arguments to pass to the mock for 'replaceMacros()'
*/ */
private function createArgumentsForMacro($withDb) private function createArgumentsForMacro(ConfigCache $config)
{ {
$args = [ $args = [
'$phpath' => trim(shell_exec('which php')), '$dbhost' => $config->get('database','hostname'),
'$dbhost' => (($withDb) ? $this->db_host . (isset($this->db_port) ? ':' . $this->db_port : '') : ''), '$dbuser' => $config->get('database','username'),
'$dbuser' => (($withDb) ? $this->db_user : ''), '$dbpass' => $config->get('database','password'),
'$dbpass' => (($withDb) ? $this->db_pass : ''), '$dbdata' => $config->get('database','database'),
'$dbdata' => (($withDb) ? $this->db_data : ''),
'$timezone' => Installer::DEFAULT_TZ, '$phpath' => $config->get('config','php_path'),
'$language' => Installer::DEFAULT_LANG, '$adminmail' => $config->get('config','admin_email'),
'$urlpath' => '/friendica', '$hostname' => $config->get('config','hostname'),
'$basepath' => '/test',
'$hostname' => 'friendica.local', '$urlpath' => $config->get('system','urlpath'),
'$adminmail' => 'admin@friendica.local' '$baseurl' => $config->get('system','url'),
'$sslpolicy' => $config->get('system','ssl_policy'),
'$timezone' => $config->get('system','default_timezone'),
'$language' => $config->get('system','language'),
'$basepath' => $config->get('system','basepath'),
]; ];
return $args; return $args;
@ -193,13 +198,38 @@ FIN;
], ],
'system' => [ 'system' => [
'urlpath' => '', 'urlpath' => '',
'url' => '',
'basepath' => '', 'basepath' => '',
'ssl_policy' => '', 'ssl_policy' => '',
'default_timezone' => '', 'default_timezone' => '',
'language' => '', 'language' => '',
], ],
], ],
] ],
'normal' => [
'data' => [
'database' => [
'hostname' => getenv('MYSQL_HOST'),
'port' =>!empty(getenv('MYSQL_PORT')) ? getenv('MYSQL_PORT') : null,
'username' => getenv('MYSQL_USERNAME'),
'password' => getenv('MYSQL_PASSWORD'),
'database' => getenv('MYSQL_DATABASE'),
],
'config' => [
'php_path' => '',
'hostname' => 'friendica.local',
'admin_email' => 'admin@philipp.info',
],
'system' => [
'urlpath' => 'test/it',
'url' => 'friendica.local/test/it',
'basepath' => '',
'ssl_policy' => '2',
'default_timezone' => 'en',
'language' => 'Europe/Berlin',
],
],
],
]; ];
} }
@ -212,8 +242,15 @@ FIN;
$configCache = new ConfigCache(); $configCache = new ConfigCache();
$configCache->load($data); $configCache->load($data);
$configCache->set('system', 'basepath', $this->root->url()); $configCache->set('system', 'basepath', $this->root->url());
$configCache->set('config', 'php_path', trim(shell_exec('which php')));
$this->mockApp($this->root, $configCache); $this->mockApp($this->root, null, true);
$this->configMock->shouldReceive('set');
$this->configMock->shouldReceive('has')->andReturn(true);
$this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) use ($configCache) {
return $configCache->get($cat, $key);
});
$this->mockConnect(true, 1); $this->mockConnect(true, 1);
$this->mockConnected(true, 1); $this->mockConnected(true, 1);
@ -221,17 +258,14 @@ FIN;
$this->mockUpdate([$this->root->url(), false, true, true], null, 1); $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1); $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1); $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro($configCache), '', 1);
$console = new AutomaticInstallation($this->consoleArgv); $console = new AutomaticInstallation($this->consoleArgv);
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
$this->assertEquals(Installer::DEFAULT_LANG, $configCache->get('system', 'language'));
$this->assertEquals(Installer::DEFAULT_TZ, $configCache->get('system', 'default_timezone'));
$this->assertEquals(Installer::DEFAULT_HOST, $configCache->get('database', 'hostname'));
$this->assertFinished($txt, true, false); $this->assertFinished($txt, true, false);
$this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
} }
/** /**
@ -240,11 +274,24 @@ FIN;
*/ */
public function testWithConfig(array $data) public function testWithConfig(array $data)
{ {
$configCache = new ConfigCache();
$configCache->load($data);
$configCache->set('system', 'basepath', $this->root->url());
$configCache->set('config', 'php_path', trim(shell_exec('which php')));
$this->mockApp($this->root, $configCache, true);
$this->mode->shouldReceive('isInstall')->andReturn(false);
Logger::init(new VoidLogger());
$this->mockConnect(true, 1); $this->mockConnect(true, 1);
$this->mockConnected(true, 1); $this->mockConnected(true, 1);
$this->mockExistsTable('user', false, 1); $this->mockExistsTable('user', false, 1);
$this->mockUpdate([$this->root->url(), false, true, true], null, 1); $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
$conf = function ($cat, $key) use ($configCache) {
return $configCache->get($cat, $key);
};
$config = <<<CONF $config = <<<CONF
<?php <?php
@ -255,10 +302,10 @@ FIN;
return [ return [
'database' => [ 'database' => [
'hostname' => '', 'hostname' => '{$conf('database','hostname')}',
'username' => '', 'username' => '{$conf('database', 'username')}',
'password' => '', 'password' => '{$conf('database', 'password')}',
'database' => '', 'database' => '{$conf('database', 'database')}',
'charset' => 'utf8mb4', 'charset' => 'utf8mb4',
], ],
@ -269,14 +316,18 @@ return [
// **************************************************************** // ****************************************************************
'config' => [ 'config' => [
'admin_email' => '', 'admin_email' => '{$conf('config', 'admin_email')}',
'hostname' => '{$conf('config', 'hostname')}',
'sitename' => 'Friendica Social Network', 'sitename' => 'Friendica Social Network',
'register_policy' => \Friendica\Module\Register::OPEN, 'register_policy' => \Friendica\Module\Register::OPEN,
'register_text' => '', 'register_text' => '',
], ],
'system' => [ 'system' => [
'default_timezone' => 'UTC', 'basepath => '{$conf('system', 'basepath')}',
'language' => 'en', 'urlpath => '{$conf('system', 'urlpath')}',
'url' => '{$conf('system', 'url')}',
'default_timezone' => '{$conf('system', 'default_timezone')}',
'language' => '{$conf('system', 'language')}',
], ],
]; ];
CONF; CONF;
@ -293,20 +344,28 @@ CONF;
$this->assertFinished($txt, false, true); $this->assertFinished($txt, false, true);
$this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')); $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
$this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
} }
/** /**
* @medium * @medium
* @dataProvider dataInstaller
*/ */
public function testWithEnvironmentAndSave() public function testWithEnvironmentAndSave(array $data)
{ {
$configCache = new ConfigCache();
$configCache->set('system', 'basepath', $this->root->url());
$configCache->set('config', 'php_path', trim(shell_exec('which php')));
$this->mockApp($this->root, $configCache);
$this->mockConnect(true, 1); $this->mockConnect(true, 1);
$this->mockConnected(true, 1); $this->mockConnected(true, 1);
$this->mockExistsTable('user', false, 1); $this->mockExistsTable('user', false, 1);
$this->mockUpdate([$this->root->url(), false, true, true], null, 1); $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1); $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1); $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro($configCache), '', 1);
$this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local')); $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
$this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin')); $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
@ -318,6 +377,8 @@ CONF;
$txt = $this->dumpExecute($console); $txt = $this->dumpExecute($console);
print_r($configCache);
$this->assertFinished($txt, true); $this->assertFinished($txt, true);
} }