testing file config, pass

This commit is contained in:
Guillermo Dev
2018-10-11 01:40:58 +02:00
parent 40d96a8911
commit 97bdda2894
3 changed files with 51 additions and 10 deletions

Binary file not shown.

View File

@@ -8,6 +8,11 @@ class Configuration {
*/ */
private static $instance = null; private static $instance = null;
/**
* @var
*/
private static $customConfigFilePath;
/** /**
* ! WARNING ! * ! WARNING !
* *
@@ -23,8 +28,9 @@ class Configuration {
*/ */
private $values = array(); private $values = array();
private $customConfigFilename = 'configuration.local.php'; /**
*
*/
private function __construct() private function __construct()
{ {
if (!isset($this->values['session'])) { if (!isset($this->values['session'])) {
@@ -34,27 +40,47 @@ class Configuration {
if (!isset($this->values['session']['save_path'])) { if (!isset($this->values['session']['save_path'])) {
$this->values['session']['save_path'] = session_save_path(); $this->values['session']['save_path'] = session_save_path();
} }
if (file_exists(self::getConfigFilePath())) {
$this->loadConfigFromFile();
}
}
/**
* The path to the file including file name
* @param string $filePath
* @return void
*/
public static function setConfigFilePath($filePath)
{
if (!file_exists($filePath)) {
throw new \RuntimeException("The file path $filePath, does not exist");
}
self::$customConfigFilePath = $filePath;
} }
/** /**
* *
* @return String * @return String
*/ */
public function getCustomConfigFilePath() public static function getConfigFilePath()
{ {
return realpath(dirname(__FILE__) . '/../../../../../config/') . $this->customConfigFilename; if (null === self::$customConfigFilePath) {
self::$customConfigFilePath = realpath(dirname(__FILE__) . '/../../../../../config/configuration.local.php');
}
return self::$customConfigFilePath;
} }
/** /**
* @return array * @return array
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function getCustomConfigFromFile() public function getConfigFromFile()
{ {
if (!file_exists($this->getCustomConfigFilePath())) { if (!file_exists(self::getConfigFilePath())) {
throw new \RuntimeException("The file : {$this->getCustomConfigFilePath()} does not exist"); throw new \RuntimeException("The file : {self::getConfigFilePath()} does not exist");
} }
return include $this->getCustomConfigFilePath(); return include self::getConfigFilePath();
} }
/** /**
@@ -62,9 +88,9 @@ class Configuration {
* @return void * @return void
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function loadCustomConfigFromFile() public function loadConfigFromFile()
{ {
$this->setCustomConfig($this->getCustomConfigFromFile()); $this->setCustomConfig($this->getConfigFromFile());
} }
/** /**

View File

@@ -16,10 +16,12 @@ class ConfigurationTest extends TestCase
); );
protected $defaultConfig; protected $defaultConfig;
protected $dummyConfigFilePath;
public function setUp() public function setUp()
{ {
$this->defaultConfig = array('session' => array('save_path' => session_save_path())); $this->defaultConfig = array('session' => array('save_path' => session_save_path()));
$this->dummyConfigFilePath = realpath(dirname(__FILE__) . '/../../config/configuration.local.php');
} }
public function testConfigurationInstanceIsTheSame() public function testConfigurationInstanceIsTheSame()
@@ -113,4 +115,17 @@ class ConfigurationTest extends TestCase
Configuration::getInstance()->setCustomConfig($this->testConfig); Configuration::getInstance()->setCustomConfig($this->testConfig);
$this->assertSame(Configuration::get(), $this->testConfig); $this->assertSame(Configuration::get(), $this->testConfig);
} }
public function testDummyConfigFileExists()
{
$this->assertEquals(file_exists($this->dummyConfigFilePath), true);
}
public function testFileCustomConfigGetsLoadedIfFileExists()
{
$configPriorToFileLoading = Configuration::get();
Configuration::setConfigFilePath($this->dummyConfigFilePath);
Configuration::getInstance()->loadConfigFromFile();
$this->assertSame(Configuration::get(), array_replace_recursive($this->testConfig, include $this->dummyConfigFilePath));
}
} }