Skip to content

Commit

Permalink
Multi database setup
Browse files Browse the repository at this point in the history
  • Loading branch information
toopay committed Mar 29, 2012
1 parent dba9437 commit ee2f5d0
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 3 additions & 7 deletions system/database/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 16 additions & 14 deletions tests/codeigniter/database/DB_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -19,29 +19,31 @@ public function test_db_invalid()

$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);
}

}
Binary file added tests/mocks/database/ci_test.sqlite
Binary file not shown.
34 changes: 34 additions & 0 deletions tests/mocks/database/config/mysql.php
Original file line number Diff line number Diff line change
@@ -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',
),
),
),
);
34 changes: 34 additions & 0 deletions tests/mocks/database/config/pgsql.php
Original file line number Diff line number Diff line change
@@ -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',
),
),
),
);
34 changes: 34 additions & 0 deletions tests/mocks/database/config/sqlite.php
Original file line number Diff line number Diff line change
@@ -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',
),
),
),
);
35 changes: 32 additions & 3 deletions tests/mocks/database/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

class Mock_Database_DB {

/**
* @var array DB configuration
*/
private $config = array();

/**
Expand All @@ -15,6 +18,12 @@ public function __construct($config = array())
$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]))
Expand All @@ -25,15 +34,14 @@ public function set_dsn($group = 'default')
$params = array(
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'db_debug' => FALSE,
'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);
Expand All @@ -51,9 +59,30 @@ public function set_dsn($group = 'default')

$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');
Expand Down
1 change: 0 additions & 1 deletion tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
37 changes: 37 additions & 0 deletions tests/travis/mysql.phpunit.xml
Original file line number Diff line number Diff line change
@@ -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>
37 changes: 37 additions & 0 deletions tests/travis/pgsql.phpunit.xml
Original file line number Diff line number Diff line change
@@ -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>
37 changes: 37 additions & 0 deletions tests/travis/sqlite.phpunit.xml
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit ee2f5d0

Please sign in to comment.