From: Jan Schneider Date: Mon, 3 Jan 2011 12:39:27 +0000 (+0100) Subject: Add a common method for configuration loading. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=4f469acadac1ea2c8bd505640d33e891045ff9a9;p=horde.git Add a common method for configuration loading. --- diff --git a/.gitignore b/.gitignore index 337fac575..36506d752 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ framework/*/test/*/*/*/*.out !framework/XML_WBXML/tests/decode.php framework/Alarm/test/Horde/Alarm/conf.php framework/Db/test/Horde/Db/Adapter/conf.php +framework/Db/test/Horde/Db/Adapter/Pdo/conf.php framework/Kolab_Storage/test/Horde/Kolab/Storage/conf.php framework/Ldap/test/Horde/Ldap/conf.php diff --git a/framework/Alarm/test/Horde/Alarm/SqlTest.php b/framework/Alarm/test/Horde/Alarm/SqlTest.php index eb6e0778c..2bdb13ea8 100644 --- a/framework/Alarm/test/Horde/Alarm/SqlTest.php +++ b/framework/Alarm/test/Horde/Alarm/SqlTest.php @@ -7,7 +7,7 @@ * @subpackage UnitTests */ -class Horde_Alarm_SqlTest extends PHPUnit_Framework_TestCase +class Horde_Alarm_SqlTest extends Horde_Test_Case { protected static $skip = false; protected static $db; @@ -21,13 +21,7 @@ class Horde_Alarm_SqlTest extends PHPUnit_Framework_TestCase // @fixme $GLOBALS['language'] = 'en_US'; - $config = getenv('ALARM_TEST_CONFIG'); - if ($config === false) { - $config = dirname(__FILE__) . '/conf.php'; - } - if (file_exists($config)) { - require $config; - } + $conf = self::getConfig('ALARM_TEST_CONFIG'); if (!isset($conf['alarm']['test'])) { self::$skip = true; return; diff --git a/framework/Db/test/Horde/Db/Adapter/MysqlSuite.php b/framework/Db/test/Horde/Db/Adapter/MysqlSuite.php index c982df6f2..d1ea19054 100644 --- a/framework/Db/test/Horde/Db/Adapter/MysqlSuite.php +++ b/framework/Db/test/Horde/Db/Adapter/MysqlSuite.php @@ -61,20 +61,16 @@ class Horde_Db_Adapter_MysqlSuite extends PHPUnit_Framework_TestSuite public function getConnection() { - $config = getenv('DB_ADAPTER_MYSQL_TEST_CONFIG'); - if ($config && !is_dir(dirname($config))) { - $config = array_merge(array('host' => 'localhost', 'username' => '', 'password' => '', 'dbname' => 'test'), json_decode($config, true)); - } else { - if (!$config) { - $config = dirname(__FILE__) . '/conf.php'; - } - if (file_exists($config)) { - require $config; - } - if (!isset($conf['db']['adapter']['mysql']['test'])) { - throw new Exception('No configuration for mysql test'); - } - $config = $conf['db']['adapter']['mysql']['test']; + $config = Horde_Test_Case::getConfig('DB_ADAPTER_MYSQL_TEST_CONFIG', + array('host' => 'localhost', + 'username' => '', + 'password' => '', + 'dbname' => 'test')); + if (isset($config['db']['adapter']['mysql']['test'])) { + $config = $config['db']['adapter']['mysql']['test']; + } + if (!is_array($config)) { + throw new Exception('No configuration for mysql test'); } $conn = new Horde_Db_Adapter_Mysql($config); diff --git a/framework/Db/test/Horde/Db/Adapter/MysqliSuite.php b/framework/Db/test/Horde/Db/Adapter/MysqliSuite.php index e0b4f2ea5..2e499c835 100644 --- a/framework/Db/test/Horde/Db/Adapter/MysqliSuite.php +++ b/framework/Db/test/Horde/Db/Adapter/MysqliSuite.php @@ -61,20 +61,16 @@ class Horde_Db_Adapter_MysqliSuite extends PHPUnit_Framework_TestSuite public function getConnection() { - $config = getenv('DB_ADAPTER_MYSQLI_TEST_CONFIG'); - if ($config && !is_dir(dirname($config))) { - $config = array_merge(array('host' => 'localhost', 'username' => '', 'password' => '', 'dbname' => 'test'), json_decode($config, true)); - } else { - if (!$config) { - $config = dirname(__FILE__) . '/conf.php'; - } - if (file_exists($config)) { - require $config; - } - if (!isset($conf['db']['adapter']['mysqli']['test'])) { - throw new Exception('No configuration for mysqli test'); - } - $config = $conf['db']['adapter']['mysqli']['test']; + $config = Horde_Test_Case::getConfig('DB_ADAPTER_MYSQLI_TEST_CONFIG', + array('host' => 'localhost', + 'username' => '', + 'password' => '', + 'dbname' => 'test')); + if (isset($config['db']['adapter']['mysqli']['test'])) { + $config = $config['db']['adapter']['mysqli']['test']; + } + if (!is_array($config)) { + throw new Exception('No configuration for mysql test'); } $conn = new Horde_Db_Adapter_Mysqli($config); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlSuite.php b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlSuite.php index f78137853..5cf046bc0 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlSuite.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlSuite.php @@ -61,20 +61,16 @@ class Horde_Db_Adapter_Pdo_MysqlSuite extends PHPUnit_Framework_TestSuite public function getConnection() { - $config = getenv('DB_ADAPTER_PDO_MYSQL_TEST_CONFIG'); - if ($config && !is_dir(dirname($config))) { - $config = array_merge(array('host' => 'localhost', 'username' => '', 'password' => '', 'dbname' => 'test'), json_decode($config, true)); - } else { - if (!$config) { - $config = dirname(__FILE__) . '/../conf.php'; - } - if (file_exists($config)) { - require $config; - } - if (!isset($conf['db']['adapter']['pdo']['mysql']['test'])) { - throw new Exception('No configuration for pdo_mysql test'); - } - $config = $conf['db']['adapter']['pdo']['mysql']['test']; + $config = Horde_Test_Case::getConfig('DB_ADAPTER_PDO_MYSQL_TEST_CONFIG', + array('host' => 'localhost', + 'username' => '', + 'password' => '', + 'dbname' => 'test')); + if (isset($config['db']['adapter']['pdo']['mysql']['test'])) { + $config = $config['db']['adapter']['pdo']['mysql']['test']; + } + if (!is_array($config)) { + throw new Exception('No configuration for pdo_mysql test'); } $conn = new Horde_Db_Adapter_Pdo_Mysql($config); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlSuite.php b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlSuite.php index a1fe5c5fa..c5e9a0c36 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlSuite.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlSuite.php @@ -67,20 +67,15 @@ class Horde_Db_Adapter_Pdo_PgsqlSuite extends PHPUnit_Framework_TestSuite return self::$conn; } - $config = getenv('DB_ADAPTER_PDO_PGSQL_TEST_CONFIG'); - if ($config && !is_dir(dirname($config))) { - $config = array_merge(array('username' => '', 'password' => '', 'dbname' => 'test'), json_decode($config, true)); - } else { - if (!$config) { - $config = dirname(__FILE__) . '/../conf.php'; - } - if (file_exists($config)) { - require $config; - } - if (!isset($conf['db']['adapter']['pdo']['pgsql']['test'])) { - throw new Exception('No configuration for pdo_pgsql test'); - } - $config = $conf['db']['adapter']['pdo']['pgsql']['test']; + $config = Horde_Test_Case::getConfig('DB_ADAPTER_PDO_PGSQL_TEST_CONFIG', + array('username' => '', + 'password' => '', + 'dbname' => 'test')); + if (isset($config['db']['adapter']['pdo']['pgsql']['test'])) { + $config = $config['db']['adapter']['pdo']['pgsql']['test']; + } + if (!is_array($config)) { + throw new Exception('No configuration for pdo_pgsql test'); } $conn = new Horde_Db_Adapter_Pdo_Pgsql($config); diff --git a/framework/Db/test/Horde/Db/Adapter/conf.php.dist b/framework/Db/test/Horde/Db/Adapter/conf.php.dist index d9bd935f5..bb3a5274f 100644 --- a/framework/Db/test/Horde/Db/Adapter/conf.php.dist +++ b/framework/Db/test/Horde/Db/Adapter/conf.php.dist @@ -1,4 +1,6 @@ markTestSkipped($e->getMessage()); } - $file = dirname(__FILE__) . '/conf.php'; - if (!file_exists($file) || !is_readable($file)) { - $this->markTestSkipped('conf.php cannot be opened.'); + $config = $this->getConfig('LDAP_TEST_CONFIG'); + if (!$config) { + $this->markTestSkipped('No configuration for LDAP tests.'); } - include $file; - self::$ldapcfg = $conf; + self::$ldapcfg = $config; } } diff --git a/framework/Test/lib/Horde/Test/Case.php b/framework/Test/lib/Horde/Test/Case.php index 84b1d9ed6..fe91e28e2 100644 --- a/framework/Test/lib/Horde/Test/Case.php +++ b/framework/Test/lib/Horde/Test/Case.php @@ -8,4 +8,38 @@ class Horde_Test_Case extends PHPUnit_Framework_TestCase { return $this->getMock($className, $methods, $arguments, $mockClassName, /* $callOriginalConstructor */ false); } + + /** + * Helper method for loading test configuration from a file. + * + * The configuration can be specified by an environment variable. If the + * variable content is a file name, the configuration is loaded from the + * file. Otherwise it's assumed to be a json encoded configuration hash. If + * the environment variable is not set, the method tries to load a conf.php + * file from the same directory as the test case. + * + * @param string $env An environment variable name. + * @param array $default Some default values that are merged into the + * configuration if specified as a json hash. + * + * @return mixed The value of the configuration file's $conf variable, or + * null. + */ + static public function getConfig($env, $default = array()) + { + $conf = null; + $config = getenv($env); + if ($config && !is_dir(dirname($config))) { + $conf = Horde_Array::replaceRecursive($default, + json_decode($config, true)); + } elseif ($config === false) { + $backtrace = new Horde_Support_Backtrace(); + $caller = $backtrace->getCurrentContext(); + $config = dirname($caller['file']) . '/conf.php'; + } + if (file_exists($config)) { + require $config; + } + return $conf; + } }