From 655a89f4059ebae017d1c4ec5f26aeb2cf4a3bae Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 29 Mar 2012 03:00:56 +0700 Subject: Preliminary Database Test --- tests/mocks/database/.gitkeep | 0 tests/mocks/database/db.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) delete mode 100644 tests/mocks/database/.gitkeep create mode 100644 tests/mocks/database/db.php (limited to 'tests/mocks/database') diff --git a/tests/mocks/database/.gitkeep b/tests/mocks/database/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php new file mode 100644 index 000000000..d7a6351a0 --- /dev/null +++ b/tests/mocks/database/db.php @@ -0,0 +1,43 @@ +config = $config; + } + + public function set_config($group = 'default') + { + if ( ! isset($this->config[$group])) + { + throw new InvalidArgumentException('Group '.$group.' not exists'); + } + + if ( ! empty($this->config[$group]['dsn'])) + { + $dsn = $this->config[$group]['dsn']; + } + else + { + $config = $this->config[$group]; + $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] + .'@'.$config['hostname'].'/'.$config['database']; + + } + + $params = array_slice($this->config[$group], 6); + + return $dsn.http_build_query($params); + } +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From a8a2e3325c128ccdc941daba3bba10b78bf2d098 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 29 Mar 2012 03:56:46 +0700 Subject: Travis setup and minor cleanup --- tests/mocks/database/db.php | 45 +++++++++++++++++++++++++++++------- tests/mocks/database/models/.gitkeep | 0 2 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 tests/mocks/database/models/.gitkeep (limited to 'tests/mocks/database') diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php index d7a6351a0..11e4a93bd 100644 --- a/tests/mocks/database/db.php +++ b/tests/mocks/database/db.php @@ -12,32 +12,61 @@ class Mock_Database_DB { */ public function __construct($config = array()) { - include_once(BASEPATH.'database/DB.php'); - $this->config = $config; } - public function set_config($group = 'default') + public function set_dsn($group = 'default') { if ( ! isset($this->config[$group])) { throw new InvalidArgumentException('Group '.$group.' not exists'); } - if ( ! empty($this->config[$group]['dsn'])) + $params = array( + 'dbprefix' => '', + 'pconnect' => FALSE, + 'db_debug' => TRUE, + 'cache_on' => FALSE, + 'cachedir' => '', + 'char_set' => 'utf8', + 'dbcollat' => 'utf8_general_ci', + 'swap_pre' => '', + 'autoinit' => TRUE, + 'stricton' => FALSE, + 'failover' => array() + ); + + $config = array_merge($this->config[$group], $params); + + if ( ! empty($config['dsn'])) { - $dsn = $this->config[$group]['dsn']; + $dsn = $config['dsn']; } else { - $config = $this->config[$group]; $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] .'@'.$config['hostname'].'/'.$config['database']; } - $params = array_slice($this->config[$group], 6); + $other_params = array_slice($config, 6); + + return $dsn.http_build_query($other_params); + } + + public static function DB($group, $query_builder = FALSE) + { + include_once(BASEPATH.'database/DB.php'); + + try + { + $db = DB($group, $query_builder); + } + catch (Exception $e) + { + throw new InvalidArgumentException($e->getMessage()); + } - return $dsn.http_build_query($params); + return $db; } } \ No newline at end of file diff --git a/tests/mocks/database/models/.gitkeep b/tests/mocks/database/models/.gitkeep new file mode 100644 index 000000000..e69de29bb -- cgit v1.2.3-24-g4f1b From dba9437218a5d8bedb75464b943e8f920d220a25 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Thu, 29 Mar 2012 04:09:37 +0700 Subject: Add schema folder --- tests/mocks/database/models/.gitkeep | 0 tests/mocks/database/schema/.gitkeep | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/mocks/database/models/.gitkeep create mode 100644 tests/mocks/database/schema/.gitkeep (limited to 'tests/mocks/database') diff --git a/tests/mocks/database/models/.gitkeep b/tests/mocks/database/models/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/mocks/database/schema/.gitkeep b/tests/mocks/database/schema/.gitkeep new file mode 100644 index 000000000..e69de29bb -- cgit v1.2.3-24-g4f1b From ee2f5d08c64d96b7abc7195bcd1b6a3fd67b5b42 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Fri, 30 Mar 2012 06:29:11 +0700 Subject: Multi database setup --- tests/mocks/database/ci_test.sqlite | Bin 0 -> 17408 bytes tests/mocks/database/config/mysql.php | 34 ++++++++++++++++++++++++++++++++ tests/mocks/database/config/pgsql.php | 34 ++++++++++++++++++++++++++++++++ tests/mocks/database/config/sqlite.php | 34 ++++++++++++++++++++++++++++++++ tests/mocks/database/db.php | 35 ++++++++++++++++++++++++++++++--- 5 files changed, 134 insertions(+), 3 deletions(-) create mode 100755 tests/mocks/database/ci_test.sqlite create mode 100644 tests/mocks/database/config/mysql.php create mode 100644 tests/mocks/database/config/pgsql.php create mode 100644 tests/mocks/database/config/sqlite.php (limited to 'tests/mocks/database') diff --git a/tests/mocks/database/ci_test.sqlite b/tests/mocks/database/ci_test.sqlite new file mode 100755 index 000000000..37ce4f870 Binary files /dev/null and b/tests/mocks/database/ci_test.sqlite differ 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 @@ + 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 @@ + 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 @@ + 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'); -- cgit v1.2.3-24-g4f1b From 6dfe76dfe4634971faeef197aa724c924a72d515 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 3 Apr 2012 12:42:59 +0300 Subject: Alter SQLite tests config to use the sqlite3 driver under PHP 5.4+ --- tests/mocks/database/config/sqlite.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'tests/mocks/database') diff --git a/tests/mocks/database/config/sqlite.php b/tests/mocks/database/config/sqlite.php index cf428f473..8665e208d 100644 --- a/tests/mocks/database/config/sqlite.php +++ b/tests/mocks/database/config/sqlite.php @@ -1,7 +1,8 @@ array( 'dsn' => '', @@ -9,7 +10,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_test.sqlite', - 'dbdriver' => 'sqlite', + 'dbdriver' => $dbdriver, ), // Database configuration with failover @@ -19,7 +20,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => '../not_exists.sqlite', - 'dbdriver' => 'sqlite', + 'dbdriver' => $dbdriver, 'failover' => array( array( 'dsn' => '', @@ -27,7 +28,7 @@ return array( 'username' => 'sqlite', 'password' => 'sqlite', 'database' => realpath(__DIR__.'/..').'/ci_testf.sqlite', - 'dbdriver' => 'sqlite', + 'dbdriver' => $dbdriver, ), ), ), -- cgit v1.2.3-24-g4f1b