Add a common method for configuration loading.
authorJan Schneider <jan@horde.org>
Mon, 3 Jan 2011 12:39:27 +0000 (13:39 +0100)
committerJan Schneider <jan@horde.org>
Mon, 3 Jan 2011 12:39:27 +0000 (13:39 +0100)
.gitignore
framework/Alarm/test/Horde/Alarm/SqlTest.php
framework/Db/test/Horde/Db/Adapter/MysqlSuite.php
framework/Db/test/Horde/Db/Adapter/MysqliSuite.php
framework/Db/test/Horde/Db/Adapter/Pdo/MysqlSuite.php
framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlSuite.php
framework/Db/test/Horde/Db/Adapter/conf.php.dist
framework/Ldap/test/Horde/Ldap/TestBase.php
framework/Test/lib/Horde/Test/Case.php

index 337fac5..36506d7 100644 (file)
@@ -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
 
index eb6e077..2bdb13e 100644 (file)
@@ -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;
index c982df6..d1ea190 100644 (file)
@@ -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);
index e0b4f2e..2e499c8 100644 (file)
@@ -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);
index f781378..5cf046b 100644 (file)
@@ -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);
index a1fe5c5..c5e9a0c 100644 (file)
@@ -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);
index d9bd935..bb3a527 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+// IMPORTANT: the conf.php version of this file must be both in this directory
+// and in the the Pdo/ subdirectory. A symlink is fine too.
 $conf['db']['adapter']['mysql']['test']['host'] = 'localhost';
 $conf['db']['adapter']['mysql']['test']['username'] = '';
 $conf['db']['adapter']['mysql']['test']['password'] = '';
index 7571e5d..e382628 100644 (file)
@@ -7,7 +7,7 @@
  * @license    http://www.gnu.org/copyleft/lesser.html LGPL
  */
 
-class Horde_Ldap_TestBase extends PHPUnit_Framework_TestCase
+class Horde_Ldap_TestBase extends Horde_Test_Case
 {
     protected static $ldapcfg;
 
@@ -20,11 +20,10 @@ class Horde_Ldap_TestBase extends PHPUnit_Framework_TestCase
             $this->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;
     }
 }
index 84b1d9e..fe91e28 100644 (file)
@@ -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;
+    }
 }