diff options
-rw-r--r-- | .travis.yml | 2 | ||||
-rwxr-xr-x | system/database/DB.php | 10 | ||||
-rw-r--r-- | tests/codeigniter/database/DB_test.php | 30 | ||||
-rwxr-xr-x | tests/mocks/database/ci_test.sqlite | bin | 0 -> 17408 bytes | |||
-rw-r--r-- | tests/mocks/database/config/mysql.php | 34 | ||||
-rw-r--r-- | tests/mocks/database/config/pgsql.php | 34 | ||||
-rw-r--r-- | tests/mocks/database/config/sqlite.php | 34 | ||||
-rw-r--r-- | tests/mocks/database/db.php | 35 | ||||
-rw-r--r-- | tests/phpunit.xml | 1 | ||||
-rw-r--r-- | tests/travis/mysql.phpunit.xml | 37 | ||||
-rw-r--r-- | tests/travis/pgsql.phpunit.xml | 37 | ||||
-rw-r--r-- | tests/travis/sqlite.phpunit.xml | 37 |
12 files changed, 265 insertions, 26 deletions
diff --git a/.travis.yml b/.travis.yml index c6a4b5ece..84029b964 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ before_script: - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database ci_test;' -U postgres; fi" - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS ci_test;'; fi" -script: phpunit --configuration tests/phpunit.xml +script: phpunit --configuration tests/travis/$DB.phpunit.xml branches: only: diff --git a/system/database/DB.php b/system/database/DB.php index e9434231c..0d81e40d3 100755 --- a/system/database/DB.php +++ b/system/database/DB.php @@ -92,16 +92,12 @@ function &DB($params = '', $active_record_override = NULL) if (isset($dsn['query'])) { parse_str($dsn['query'], $extra); + foreach ($extra as $key => $val) { - // booleans please - if (strtoupper($val) === 'TRUE') - { - $val = TRUE; - } - elseif (strtoupper($val) === 'FALSE') + if (is_string($val) && in_array(strtoupper($val), array('TRUE', 'FALSE', 'NULL'))) { - $val = FALSE; + $val = var_export($val); } $params[$key] = $val; diff --git a/tests/codeigniter/database/DB_test.php b/tests/codeigniter/database/DB_test.php index 10e8dec64..9b93e223d 100644 --- a/tests/codeigniter/database/DB_test.php +++ b/tests/codeigniter/database/DB_test.php @@ -6,7 +6,7 @@ class DB_test extends CI_TestCase { public function test_db_invalid() { - $db_config = new Mock_Database_DB(array( + $connection = new Mock_Database_DB(array( 'undefined' => array( 'dsn' => '', 'hostname' => 'undefined', @@ -19,29 +19,31 @@ class DB_test extends CI_TestCase { $this->setExpectedException('InvalidArgumentException', 'CI Error: Invalid DB driver'); - Mock_Database_DB::DB($db_config->set_dsn('undefined'), TRUE); + Mock_Database_DB::DB($connection->set_dsn('undefined'), TRUE); } // ------------------------------------------------------------------------ public function test_db_valid() { - $db_config = new Mock_Database_DB(array( - 'mysql' => array( - 'dsn' => '', - 'hostname' => 'localhost', - 'username' => 'travis', - 'password' => '', - 'database' => 'ci_test', - 'dbdriver' => 'mysql', - ), - )); + $config = Mock_Database_DB::config(DB_DRIVER); + $connection = new Mock_Database_DB($config); + $db = Mock_Database_DB::DB($connection->set_dsn(DB_DRIVER), TRUE); + + $this->assertTrue($db instanceof CI_DB); + $this->assertTrue($db instanceof CI_DB_Driver); + } - $db = Mock_Database_DB::DB($db_config->set_dsn('mysql'), TRUE); + // ------------------------------------------------------------------------ + + public function test_db_failover() + { + $config = Mock_Database_DB::config(DB_DRIVER); + $connection = new Mock_Database_DB($config); + $db = Mock_Database_DB::DB($connection->set_dsn(DB_DRIVER.'_failover'), TRUE); $this->assertTrue($db instanceof CI_DB); $this->assertTrue($db instanceof CI_DB_Driver); - $this->assertTrue($db instanceof CI_DB_mysql_driver); } }
\ No newline at end of file diff --git a/tests/mocks/database/ci_test.sqlite b/tests/mocks/database/ci_test.sqlite Binary files differnew file mode 100755 index 000000000..37ce4f870 --- /dev/null +++ b/tests/mocks/database/ci_test.sqlite diff --git a/tests/mocks/database/config/mysql.php b/tests/mocks/database/config/mysql.php new file mode 100644 index 000000000..ace0a31b1 --- /dev/null +++ b/tests/mocks/database/config/mysql.php @@ -0,0 +1,34 @@ +<?php + +return array( + + // Typical Database configuration + 'mysql' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'travis', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'mysql', + ), + + // Database configuration with failover + 'mysql_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'not_travis', + 'password' => 'wrong password', + 'database' => 'not_ci_test', + 'dbdriver' => 'mysql', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'travis', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'mysql', + ), + ), + ), +);
\ No newline at end of file diff --git a/tests/mocks/database/config/pgsql.php b/tests/mocks/database/config/pgsql.php new file mode 100644 index 000000000..c06af8ce0 --- /dev/null +++ b/tests/mocks/database/config/pgsql.php @@ -0,0 +1,34 @@ +<?php + +return array( + + // Typical Database configuration + 'pgsql' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'postgres', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'postgre', + ), + + // Database configuration with failover + 'pgsql_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'not_travis', + 'password' => 'wrong password', + 'database' => 'not_ci_test', + 'dbdriver' => 'postgre', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'postgres', + 'password' => '', + 'database' => 'ci_test', + 'dbdriver' => 'postgre', + ), + ), + ), +);
\ No newline at end of file diff --git a/tests/mocks/database/config/sqlite.php b/tests/mocks/database/config/sqlite.php new file mode 100644 index 000000000..cf428f473 --- /dev/null +++ b/tests/mocks/database/config/sqlite.php @@ -0,0 +1,34 @@ +<?php + +return array( + + // Typical Database configuration + 'sqlite' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', + 'dbdriver' => 'sqlite', + ), + + // Database configuration with failover + 'sqlite_failover' => array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => '../not_exists.sqlite', + 'dbdriver' => 'sqlite', + 'failover' => array( + array( + 'dsn' => '', + 'hostname' => 'localhost', + 'username' => 'sqlite', + 'password' => 'sqlite', + 'database' => realpath(__DIR__.'/..').'/ci_testf.sqlite', + 'dbdriver' => 'sqlite', + ), + ), + ), +);
\ No newline at end of file diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php index 11e4a93bd..43a0d391f 100644 --- a/tests/mocks/database/db.php +++ b/tests/mocks/database/db.php @@ -2,6 +2,9 @@ class Mock_Database_DB { + /** + * @var array DB configuration + */ private $config = array(); /** @@ -15,6 +18,12 @@ class Mock_Database_DB { $this->config = $config; } + /** + * Build DSN connection string for DB driver instantiate process + * + * @param string Group name + * @return string DSN Connection string + */ public function set_dsn($group = 'default') { if ( ! isset($this->config[$group])) @@ -25,7 +34,7 @@ class Mock_Database_DB { $params = array( 'dbprefix' => '', 'pconnect' => FALSE, - 'db_debug' => TRUE, + 'db_debug' => FALSE, 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', @@ -33,7 +42,6 @@ class Mock_Database_DB { 'swap_pre' => '', 'autoinit' => TRUE, 'stricton' => FALSE, - 'failover' => array() ); $config = array_merge($this->config[$group], $params); @@ -51,9 +59,30 @@ class Mock_Database_DB { $other_params = array_slice($config, 6); - return $dsn.http_build_query($other_params); + return $dsn.'?'.http_build_query($other_params); } + /** + * Return a database config array + * + * @see ./config + * @param string Driver based configuration + * @return array + */ + public static function config($driver) + { + $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; + + return include($dir.'config'.DIRECTORY_SEPARATOR.$driver.'.php'); + } + + /** + * Main DB method wrapper + * + * @param string Group or DSN string + * @param bool + * @return object + */ public static function DB($group, $query_builder = FALSE) { include_once(BASEPATH.'database/DB.php'); diff --git a/tests/phpunit.xml b/tests/phpunit.xml index ffd2a1f0f..ffe460d9c 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -15,7 +15,6 @@ <directory suffix="test.php">codeigniter/core</directory> <directory suffix="test.php">codeigniter/helpers</directory> <directory suffix="test.php">codeigniter/libraries</directory> - <directory suffix="test.php">codeigniter/database</directory> </testsuite> </testsuites> <filters> diff --git a/tests/travis/mysql.phpunit.xml b/tests/travis/mysql.phpunit.xml new file mode 100644 index 000000000..44d6d6ed9 --- /dev/null +++ b/tests/travis/mysql.phpunit.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit + bootstrap="../Bootstrap.php" + colors="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + stopOnError="false" + stopOnFailure="false" + stopOnIncomplete="false" + stopOnSkipped="false"> + <php> + <const name="DB_DRIVER" value="mysql"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <file>../codeigniter/Setup_test.php</file> + <directory suffix="test.php">../codeigniter/core</directory> + <directory suffix="test.php">../codeigniter/helpers</directory> + <directory suffix="test.php">../codeigniter/libraries</directory> + <directory suffix="test.php">../codeigniter/database</directory> + </testsuite> + </testsuites> + <filters> + <blacklist> + <directory suffix=".php">PEAR_INSTALL_DIR</directory> + <directory suffix=".php">PHP_LIBDIR</directory> + <directory suffix=".php">PROJECT_BASE.'tests'</directory> + <directory suffix=".php">'../../system/core/CodeIgniter.php'</directory> + </blacklist> + <whitelist> + <!-- + <directory suffix=".php">'../system/core'</directory> + --> + </whitelist> + </filters> +</phpunit>
\ No newline at end of file diff --git a/tests/travis/pgsql.phpunit.xml b/tests/travis/pgsql.phpunit.xml new file mode 100644 index 000000000..9f52b40ae --- /dev/null +++ b/tests/travis/pgsql.phpunit.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit + bootstrap="../Bootstrap.php" + colors="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + stopOnError="false" + stopOnFailure="false" + stopOnIncomplete="false" + stopOnSkipped="false"> + <php> + <const name="DB_DRIVER" value="pgsql"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <file>../codeigniter/Setup_test.php</file> + <directory suffix="test.php">../codeigniter/core</directory> + <directory suffix="test.php">../codeigniter/helpers</directory> + <directory suffix="test.php">../codeigniter/libraries</directory> + <directory suffix="test.php">../codeigniter/database</directory> + </testsuite> + </testsuites> + <filters> + <blacklist> + <directory suffix=".php">PEAR_INSTALL_DIR</directory> + <directory suffix=".php">PHP_LIBDIR</directory> + <directory suffix=".php">PROJECT_BASE.'tests'</directory> + <directory suffix=".php">'../../system/core/CodeIgniter.php'</directory> + </blacklist> + <whitelist> + <!-- + <directory suffix=".php">'../system/core'</directory> + --> + </whitelist> + </filters> +</phpunit>
\ No newline at end of file diff --git a/tests/travis/sqlite.phpunit.xml b/tests/travis/sqlite.phpunit.xml new file mode 100644 index 000000000..74ebb482b --- /dev/null +++ b/tests/travis/sqlite.phpunit.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit + bootstrap="../Bootstrap.php" + colors="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + stopOnError="false" + stopOnFailure="false" + stopOnIncomplete="false" + stopOnSkipped="false"> + <php> + <const name="DB_DRIVER" value="sqlite"/> + </php> + <testsuites> + <testsuite name="CodeIgniter Core Test Suite"> + <file>../codeigniter/Setup_test.php</file> + <directory suffix="test.php">../codeigniter/core</directory> + <directory suffix="test.php">../codeigniter/helpers</directory> + <directory suffix="test.php">../codeigniter/libraries</directory> + <directory suffix="test.php">../codeigniter/database</directory> + </testsuite> + </testsuites> + <filters> + <blacklist> + <directory suffix=".php">PEAR_INSTALL_DIR</directory> + <directory suffix=".php">PHP_LIBDIR</directory> + <directory suffix=".php">PROJECT_BASE.'tests'</directory> + <directory suffix=".php">'../../system/core/CodeIgniter.php'</directory> + </blacklist> + <whitelist> + <!-- + <directory suffix=".php">'../system/core'</directory> + --> + </whitelist> + </filters> +</phpunit>
\ No newline at end of file |