ConfigManager no longer uses singleton pattern

This commit is contained in:
ArthurHoaro 2016-06-09 20:04:02 +02:00
parent 7f179985b4
commit 278d9ee283
9 changed files with 342 additions and 275 deletions

View file

@ -276,7 +276,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
*/
public function testCheckCurrentResourcePermissions()
{
$conf = ConfigManager::getInstance();
$conf = new ConfigManager('');
$conf->set('path.thumbnails_cache', 'cache');
$conf->set('path.config', 'data/config.php');
$conf->set('path.data_dir', 'data');
@ -290,7 +290,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
array(),
ApplicationUtils::checkResourcePermissions()
ApplicationUtils::checkResourcePermissions($conf)
);
}
@ -299,7 +299,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
*/
public function testCheckCurrentResourcePermissionsErrors()
{
$conf = ConfigManager::getInstance();
$conf = new ConfigManager('');
$conf->set('path.thumbnails_cache', 'null/cache');
$conf->set('path.config', 'null/data/config.php');
$conf->set('path.data_dir', 'null/data');
@ -322,7 +322,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
'"null/tmp" directory is not readable',
'"null/tmp" directory is not writable'
),
ApplicationUtils::checkResourcePermissions()
ApplicationUtils::checkResourcePermissions($conf)
);
}
}

View file

@ -11,13 +11,14 @@ class DummyUpdater extends Updater
/**
* Object constructor.
*
* @param array $doneUpdates Updates which are already done.
* @param LinkDB $linkDB LinkDB instance.
* @param boolean $isLoggedIn True if the user is logged in.
* @param array $doneUpdates Updates which are already done.
* @param LinkDB $linkDB LinkDB instance.
* @param ConfigManager $conf Configuration Manager instance.
* @param boolean $isLoggedIn True if the user is logged in.
*/
public function __construct($doneUpdates, $linkDB, $isLoggedIn)
public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
{
parent::__construct($doneUpdates, $linkDB, $isLoggedIn);
parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn);
// Retrieve all update methods.
// For unit test, only retrieve final methods,

View file

@ -29,8 +29,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
*/
public function setUp()
{
ConfigManager::$CONFIG_FILE = self::$configFile;
$this->conf = ConfigManager::reset();
$this->conf = new ConfigManager(self::$configFile);
}
/**
@ -108,10 +107,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
'updateMethodDummy3',
'updateMethodException',
);
$updater = new DummyUpdater($updates, array(), true);
$updater = new DummyUpdater($updates, array(), $this->conf, true);
$this->assertEquals(array(), $updater->update());
$updater = new DummyUpdater(array(), array(), false);
$updater = new DummyUpdater(array(), array(), $this->conf, false);
$this->assertEquals(array(), $updater->update());
}
@ -126,7 +125,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
'updateMethodDummy2',
'updateMethodDummy3',
);
$updater = new DummyUpdater($updates, array(), true);
$updater = new DummyUpdater($updates, array(), $this->conf, true);
$this->assertEquals($expectedUpdates, $updater->update());
}
@ -142,7 +141,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
);
$expectedUpdate = array('updateMethodDummy2');
$updater = new DummyUpdater($updates, array(), true);
$updater = new DummyUpdater($updates, array(), $this->conf, true);
$this->assertEquals($expectedUpdate, $updater->update());
}
@ -159,7 +158,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
'updateMethodDummy3',
);
$updater = new DummyUpdater($updates, array(), true);
$updater = new DummyUpdater($updates, array(), $this->conf, true);
$updater->update();
}
@ -172,8 +171,8 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
*/
public function testUpdateMergeDeprecatedConfig()
{
ConfigManager::$CONFIG_FILE = 'tests/utils/config/configPhp';
$this->conf = $this->conf->reset();
$this->conf->setConfigFile('tests/utils/config/configPhp');
$this->conf->reset();
$optionsFile = 'tests/Updater/options.php';
$options = '<?php
@ -181,10 +180,10 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
file_put_contents($optionsFile, $options);
// tmp config file.
ConfigManager::$CONFIG_FILE = 'tests/Updater/config';
$this->conf->setConfigFile('tests/Updater/config');
// merge configs
$updater = new Updater(array(), array(), true);
$updater = new Updater(array(), array(), $this->conf, true);
// This writes a new config file in tests/Updater/config.php
$updater->updateMethodMergeDeprecatedConfigFile();
@ -193,7 +192,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
$this->assertTrue($this->conf->get('general.default_private_links'));
$this->assertFalse(is_file($optionsFile));
// Delete the generated file.
unlink($this->conf->getConfigFile());
unlink($this->conf->getConfigFileExt());
}
/**
@ -201,7 +200,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
*/
public function testMergeDeprecatedConfigNoFile()
{
$updater = new Updater(array(), array(), true);
$updater = new Updater(array(), array(), $this->conf, true);
$updater->updateMethodMergeDeprecatedConfigFile();
$this->assertEquals('root', $this->conf->get('credentials.login'));
@ -216,7 +215,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
$refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false);
$this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
$updater = new Updater(array(), $linkDB, true);
$updater = new Updater(array(), $linkDB, $this->conf, true);
$updater->updateMethodRenameDashTags();
$this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
}
@ -227,29 +226,29 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
public function testConfigToJson()
{
$configFile = 'tests/utils/config/configPhp';
ConfigManager::$CONFIG_FILE = $configFile;
$conf = ConfigManager::reset();
$this->conf->setConfigFile($configFile);
$this->conf->reset();
// The ConfigIO is initialized with ConfigPhp.
$this->assertTrue($conf->getConfigIO() instanceof ConfigPhp);
$this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
$updater = new Updater(array(), array(), false);
$updater = new Updater(array(), array(), $this->conf, false);
$done = $updater->updateMethodConfigToJson();
$this->assertTrue($done);
// The ConfigIO has been updated to ConfigJson.
$this->assertTrue($conf->getConfigIO() instanceof ConfigJson);
$this->assertTrue(file_exists($conf->getConfigFile()));
$this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
$this->assertTrue(file_exists($this->conf->getConfigFileExt()));
// Check JSON config data.
$conf->reload();
$this->assertEquals('root', $conf->get('credentials.login'));
$this->assertEquals('lala', $conf->get('extras.redirector'));
$this->assertEquals('data/datastore.php', $conf->get('path.datastore'));
$this->assertEquals('1', $conf->get('plugins.WALLABAG_VERSION'));
$this->conf->reload();
$this->assertEquals('root', $this->conf->get('credentials.login'));
$this->assertEquals('lala', $this->conf->get('extras.redirector'));
$this->assertEquals('data/datastore.php', $this->conf->get('path.datastore'));
$this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
rename($configFile . '.save.php', $configFile . '.php');
unlink($conf->getConfigFile());
unlink($this->conf->getConfigFileExt());
}
/**
@ -257,11 +256,11 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
*/
public function testConfigToJsonNothingToDo()
{
$filetime = filemtime($this->conf->getConfigFile());
$updater = new Updater(array(), array(), false);
$filetime = filemtime($this->conf->getConfigFileExt());
$updater = new Updater(array(), array(), $this->conf, false);
$done = $updater->updateMethodConfigToJson();
$this->assertTrue($done);
$expected = filemtime($this->conf->getConfigFile());
$expected = filemtime($this->conf->getConfigFileExt());
$this->assertEquals($expected, $filetime);
}
}

View file

@ -15,8 +15,7 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
public function setUp()
{
ConfigManager::$CONFIG_FILE = 'tests/utils/config/configJson';
$this->conf = ConfigManager::reset();
$this->conf = new ConfigManager('tests/utils/config/configJson');
}
/**
@ -54,10 +53,10 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
$this->conf->set('paramArray', array('foo' => 'bar'));
$this->conf->set('paramNull', null);
ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp';
$this->conf->setConfigFile('tests/utils/config/configTmp');
$this->conf->write(true);
$this->conf->reload();
unlink($this->conf->getConfigFile());
unlink($this->conf->getConfigFileExt());
$this->assertEquals(42, $this->conf->get('paramInt'));
$this->assertEquals('value1', $this->conf->get('paramString'));
@ -73,10 +72,10 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
{
$this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested');
ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp';
$this->conf->setConfigFile('tests/utils/config/configTmp');
$this->conf->write(true);
$this->conf->reload();
unlink($this->conf->getConfigFile());
unlink($this->conf->getConfigFileExt());
$this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
}
@ -110,8 +109,8 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
*/
public function testWriteMissingParameter()
{
ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp';
$this->assertFalse(file_exists($this->conf->getConfigFile()));
$this->conf->setConfigFile('tests/utils/config/configTmp');
$this->assertFalse(file_exists($this->conf->getConfigFileExt()));
$this->conf->reload();
$this->conf->write(true);
@ -151,10 +150,9 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
*/
public function testReset()
{
$conf = $this->conf;
$this->assertTrue($conf === ConfigManager::getInstance());
$this->assertFalse($conf === $this->conf->reset());
$this->assertFalse($conf === ConfigManager::getInstance());
$confIO = $this->conf->getConfigIO();
$this->conf->reset();
$this->assertFalse($confIO === $this->conf->getConfigIO());
}
/**
@ -162,11 +160,11 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase
*/
public function testReload()
{
ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp';
$this->conf->setConfigFile('tests/utils/config/configTmp');
$newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
file_put_contents($this->conf->getConfigFile(), $newConf);
file_put_contents($this->conf->getConfigFileExt(), $newConf);
$this->conf->reload();
unlink($this->conf->getConfigFile());
unlink($this->conf->getConfigFileExt());
// Previous conf no longer exists, and new values have been loaded.
$this->assertFalse($this->conf->exists('credentials.login'));
$this->assertEquals('value', $this->conf->get('key'));