Commit 78af83bf authored by Carsten  Rose's avatar Carsten Rose
Browse files

Store.php: extend constructor to respect optional configFileName - this makes...

Store.php: extend constructor to respect optional configFileName - this makes it easier to implement various configfile cheks.
StoreTest.php: new phpUnit checks to configfile syntax and default values.
parent 2e26f55b
......@@ -82,7 +82,7 @@ class Store {
/**
* @param string $bodytext
*/
private function __construct($bodytext = '') {
private function __construct($bodytext = '', $fileConfigIni = '') {
// self::$session = Session::getInstance(self::$phpUnit);
......@@ -157,13 +157,12 @@ class Store {
STORE_EXTRA => false
];
self::fillSystemStore();
self::fillSystemStore($fileConfigIni);
self::fillStoreTypo3($bodytext);
self::fillStoreClient();
self::fillStoreSip();
self::fillStoreExtra();
}
/**
......@@ -172,24 +171,22 @@ class Store {
* @throws CodeException
* @throws qfq\UserFormException
*/
private static function fillSystemStore() {
private static function fillSystemStore($fileConfigIni = '') {
// PHPUnit Path to CONFIG_INI
$configIni = __DIR__ . '/../../../' . CONFIG_INI;
if (!file_exists($configIni)) {
if ($fileConfigIni == '') {
// Production Path to CONFIG_INI
$configIni = __DIR__ . '/../../../../../' . CONFIG_INI;
if (!file_exists($configIni)) {
throw new qfq\UserFormException ("Config not found: " . getcwd() . "/" . $configIni, ERROR_IO_READ_FILE);
$fileConfigIni = __DIR__ . '/../../../../../' . CONFIG_INI;
if (!file_exists($fileConfigIni)) {
// PHPUnit Path to CONFIG_INI
$fileConfigIni = __DIR__ . '/../../../' . CONFIG_INI;
}
}
try {
$config = parse_ini_file($configIni, false);
$config = parse_ini_file($fileConfigIni, false);
} catch (\Exception $e) {
throw new qfq\UserFormException ("Error read file " . CONFIG_INI . ": " . $e->getMessage(), ERROR_IO_READ_FILE);
throw new qfq\UserFormException ("Error read file " . $fileConfigIni . ": " . $e->getMessage(), ERROR_IO_READ_FILE);
}
$config = self::renameConfigElements($config);
......@@ -213,7 +210,7 @@ class Store {
$config[SYSTEM_SQL_LOG] = $config[SYSTEM_PATH_EXT] . '/' . $config[SYSTEM_SQL_LOG];
}
// Verify existence
// Check mandatory config vars.
$names = array('DB_USER', 'DB_SERVER', 'DB_PASSWORD', 'DB_NAME', 'SQL_LOG', 'SQL_LOG_MODE');
foreach ($names as $name) {
if (!isset($config[$name])) {
......@@ -231,7 +228,7 @@ class Store {
* @param array $config
* @return array
*/
private function renameConfigElements(array $config) {
private static function renameConfigElements(array $config) {
// oldname > newname
$setting = [
......@@ -504,11 +501,15 @@ class Store {
*
* @param string $bodytext
* @param bool|false $phpUnit
* @return null|\qfq\Store
* @param string $fileConfigIni
* @return null|Store
* @throws UserFormException
* @throws \qfq\CodeException
*/
public static function getInstance($bodytext = '', $phpUnit = false) {
public static function getInstance($bodytext = '', $phpUnit = false, $fileConfigIni = '') {
if ($phpUnit) {
if (self::$instance !== null) {
// fake to have a clean environment for the next test.
self::unsetStore(STORE_TYPO3);
......@@ -517,13 +518,18 @@ class Store {
self::unsetStore(STORE_CLIENT);
self::fillStoreClient();
}
// Testing different config files means initialize completely
if ($fileConfigIni != '') {
self::$instance = null;
}
}
// Design Pattern: Singleton
if (self::$instance === null) {
self::$phpUnit = $phpUnit;
self::$instance = new self($bodytext);
self::$instance = new self($bodytext, $fileConfigIni);
} else {
// Class Store seems to be presistent over multiple QFQ instantiation. Set bodytext again, with every new request (if bodytext is given).
if ($bodytext !== '')
......
......@@ -176,6 +176,98 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store.");
}
/**
* @expectedException \qfq\UserFormException
*
*/
public function testConfigMandatoryValues() {
$fileName = $this->createFile('');
$this->store = Store::getInstance('', true, $fileName);
unlink($fileName);
$this->assertEquals(false, $this->store->getStore(STORE_SYSTEM), "Retrieve system store.");
}
/**
* @param $body
* @return string
*/
private function createFile($body) {
$tmpFileName = tempnam('/tmp', 'config.qfq.ini.');
$handle = fopen($tmpFileName, "w");
fwrite($handle, $body);
fclose($handle);
return $tmpFileName;
}
public function testConfigIniDefaultValues() {
$body = <<< EOT
; Comment
DB_USER = <DBUSER>
DB_SERVER = <DBSERVER>
DB_PASSWORD = <DBPW>
DB_NAME = <DB>
DB_NAME_TEST = <TESTDB>
DB_INIT = set names utf8
SQL_LOG = sql.log
; all, modify
SQL_LOG_MODE = modify
EOT;
$expect = [
SYSTEM_DB_USER => '<DBUSER>',
SYSTEM_DB_SERVER => '<DBSERVER>',
SYSTEM_DB_PASSWORD => '<DBPW>',
SYSTEM_DB_NAME => '<DB>',
SYSTEM_DB_NAME_TEST => '<TESTDB>',
SYSTEM_DB_INIT => 'set names utf8',
SYSTEM_SQL_LOG_MODE => 'modify',
SYSTEM_DATE_FORMAT => 'yyyy-mm-dd',
SYSTEM_SHOW_DEBUG_INFO => 'no',
F_BS_LABEL_COLUMNS => '3',
F_BS_INPUT_COLUMNS => '6',
F_BS_NOTE_COLUMNS => '3',
];
$fileName = $this->createFile($body);
$this->store = Store::getInstance('', true, $fileName);
unlink($fileName);
$value = $this->store->getStore(STORE_SYSTEM);
unset($value[SYSTEM_SQL_LOG]);
unset($value[SYSTEM_PATH_EXT]);
unset($value[SYSTEM_SITE_PATH]);
// check default values
$this->assertEquals($expect, $value, "Retrieve system store.");
$body .= PHP_EOL . SYSTEM_FORM_BS_LABEL_COLUMNS . ' = 4';
$body .= PHP_EOL . SYSTEM_FORM_BS_INPUT_COLUMNS . ' = 5';
$body .= PHP_EOL . SYSTEM_FORM_BS_NOTE_COLUMNS . ' = 6';
$expect[F_BS_LABEL_COLUMNS] = '4';
$expect[F_BS_INPUT_COLUMNS] = '5';
$expect[F_BS_NOTE_COLUMNS] = '6';
$fileName = $this->createFile($body);
$this->store = Store::getInstance('', true, $fileName);
unlink($fileName);
$value = $this->store->getStore(STORE_SYSTEM);
unset($value[SYSTEM_SQL_LOG]);
unset($value[SYSTEM_PATH_EXT]);
unset($value[SYSTEM_SITE_PATH]);
// check default values
$this->assertEquals($expect, $value, "Check explizit defined values.");
}
/**
* @expectedException \qfq\UserFormException
*
......@@ -184,4 +276,5 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(array(), $this->store->getStore('unknownstore'));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment