/** * Load configuration from a file and set it to the EZPDO manager. * * If config file is not specified, it tries to load config.xml first. * Then config.ini if config.xml not found from the current directory. * * @param string $file * @return bool */ function epLoadConfig($file = false) { // use default config file? if (!$file) { // try config.ini first if (file_exists($file = 'config.ini')) { $file = 'config.ini'; } else { if (file_exists('config.xml')) { $file = 'config.xml'; } else { return false; } } } else { // check if the specified config file exists if (!file_exists($file)) { return false; } } // load the config file include_once EP_SRC_BASE . '/epConfig.php'; if (!($cfg =& epConfig::load($file))) { return false; } // set config to the EZPDO manager return epManager::instance()->setConfig($cfg); }
/** * Return the DSN for a database type from config.xml * @return string */ function _getDsn($dbtype) { // load the config file if (!$this->cfg) { // load config.xml $this->cfg =& epConfig::load(dirname(__FILE__) . "/config.xml"); $this->assertTrue(!empty($this->cfg)); } $this->assertTrue($dsn = $this->cfg->get('dbs/' . strtolower($dbtype))); return $dsn; }
/** * setup before each test * @param string $dbal (adodb or peardb) * @param string $db (mysql, pgsql, or sqlite) */ function _setUp($dbal, $db) { // destroy singletons $this->_destroy(); // load config.xml for compiler include_once EP_SRC_BASE . '/epConfig.php'; $cfg =& epConfig::load(dirname(__FILE__) . "/config.xml"); $this->assertTrue(!empty($cfg)); // set dblib $cfg->set('db_lib', $dbal); // make input/output path absolute (fixed) $source_dirs = EP_TESTS . '/classes/'; $cfg->set('source_dirs', $source_dirs); // set compiled dir switch ($db) { case 'mysql': $compiled_file = $cfg->get('test/compiled_file/mysql'); $default_dsn = $cfg->get('test/default_dsn/mysql'); break; case 'pgsql': $compiled_file = $cfg->get('test/compiled_file/pgsql'); $default_dsn = $cfg->get('test/default_dsn/pgsql'); $cfg->set('default_oid_column', 'eoid'); // oid is special in pgsql break; case 'sqlite': $compiled_file = $cfg->get('test/compiled_file/sqlite/' . $dbal); $default_dsn = $cfg->get('test/default_dsn/sqlite/' . $dbal); break; } $cfg->set('compiled_file', $compiled_file); $cfg->set('default_dsn', $default_dsn); // force compile so default_dsn gets into class map $cfg->set('force_compile', true); // get epManager instance include_once EP_SRC_RUNTIME . '/epManager.php'; $this->m = null; // force a new instance $this->m =& epManager::instance(); $this->assertTrue($this->m); // set config to manager $this->assertTrue($this->m->setConfig($cfg)); // assert source_dirs is correct $this->assertTrue($this->m->getConfigOption('source_dirs') === $source_dirs); // assert source_dirs is correct $this->assertTrue($this->m->getConfigOption('compiled_file') === $compiled_file); // assert default_dsn is correct $this->assertTrue($this->m->getConfigOption('default_dsn') === $default_dsn); }
/** * Methods runs before every test */ function setUp() { // setup compiler not already if (!$this->c) { // load config.xml for compiler include_once EP_SRC_BASE . '/epConfig.php'; $this->assertTrue($cfg = epConfig::load(realpath(dirname(__FILE__)) . "/config.xml")); $this->assertTrue(!empty($cfg)); // create a compiler object include_once EP_SRC_COMPILER . '/epCompiler.php'; $this->assertTrue($this->c = new epClassCompiler($cfg)); // no validation to suppress errors $this->c->setConfigOption('validate_after_compile', false); } }
/** * test epLog */ function testLog() { // load config.xml $log_cfg =& epConfig::load(realpath(dirname(__FILE__)) . "/input/config.xml"); $this->assertTrue(!empty($log_cfg)); // make log file absolute path $log_file = realpath(dirname(__FILE__)) . "/output/test.log"; // delete it first so it won't accumulate and become a huge file @unlink($log_file); $this->assertTrue($log_cfg->set('log_file', $log_file)); $this->assertTrue($log_file == $log_cfg->get('log_file')); // config log with the log section only $logger =& epLog::instance(); $this->assertTrue($logger); $logger->setConfig($log_cfg); // generate all messages $msgs = array(); for ($i = 0; $i < 10; $i++) { $msg = 'log test message id (' . rand(0, 100000) . ') at ' . date('H:i:s'); $msgs[] = $msg; } // log all messages foreach ($msgs as $msg) { $status = $logger->log($msg); $this->assertTrue($status); } // figure out the log file $log_file = $logger->getConfigOption('log_file'); $this->assertTrue(file_exists($log_file)); // check if the messages are in the log file $content = file_get_contents($log_file); $this->assertTrue(!empty($content)); foreach ($msgs as $msg) { $this->assertTrue(strstr($content, $content) != false); } }
/** * test epConfig serialization * epConfig to be read from input/config.xml * and written to output/config.xml */ function testConfigSerialization() { // start from input/config.xml $cfg =& epConfig::load(realpath(dirname(__FILE__)) . '/input/config.xml'); $this->assertTrue($cfg != null); // check options not empty $options =& $cfg->options(); $this->assertTrue(!empty($options)); $count0 = count($options); // add new options to cfg for ($i = 0; $i < 10; $i++) { $option = 'option' . $i; $value_set = 'value' . $i; $cfg->set($option, $value_set); } $options =& $cfg->options(); $count1 = count($options); $this->assertTrue($count1 - $count0 == 10); // write to output/config.xml $status = $cfg->store('output/config.xml'); $this->assertTrue($status); // done with cfg $cfg = null; // load output/config.xml into cfg2 $cfg2 =& epConfig::load('output/config.xml'); $this->assertTrue($cfg2 != null); // check new options in cfg2 for ($i = 0; $i < 10; $i++) { $option = 'option' . $i; $value_set = 'value' . $i; $value_get = $cfg2->get($option); $this->assertTrue($value_set == $value_get); } // remove new options for ($i = 0; $i < 10; $i++) { $option = 'option' . $i; $value_get = $cfg2->remove($option); } // write to output/config.xml $status = $cfg2->store('output/config.xml'); $this->assertTrue($status); // done with cfg2 $cfg2 = null; // load output/config.xml into cfg3 $cfg3 =& epConfig::load('output/config.xml'); $this->assertTrue($cfg3 != null); // check if new options are removed in cfg3 for ($i = 0; $i < 10; $i++) { $option = 'option' . $i; $value_get = $cfg3->get($option); $this->assertTrue(!isset($value_get)); } }