diff options
author | Phil Sturgeon <email@philsturgeon.co.uk> | 2012-03-30 10:29:56 +0200 |
---|---|---|
committer | Phil Sturgeon <email@philsturgeon.co.uk> | 2012-03-30 10:29:56 +0200 |
commit | 3396d531e2f3864d06860cdbdf7320bc121f77f1 (patch) | |
tree | cbc9ff0e1e574db87bbb3a31c783eb4dcca86dae /tests/mocks/database/db.php | |
parent | 64bfa0634c991ef98ec3d2d44d862ea99d839854 (diff) | |
parent | 2c4c5e1946293e1c7a834112a8d271e890cc1c71 (diff) |
Merge pull request #1228 from toopay/database-test
Multiple database drivers testing
Diffstat (limited to 'tests/mocks/database/db.php')
-rw-r--r-- | tests/mocks/database/db.php | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php new file mode 100644 index 000000000..43a0d391f --- /dev/null +++ b/tests/mocks/database/db.php @@ -0,0 +1,101 @@ +<?php + +class Mock_Database_DB { + + /** + * @var array DB configuration + */ + private $config = array(); + + /** + * Prepare database configuration skeleton + * + * @param array DB configuration to set + * @return void + */ + 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])) + { + throw new InvalidArgumentException('Group '.$group.' not exists'); + } + + $params = array( + 'dbprefix' => '', + 'pconnect' => FALSE, + 'db_debug' => FALSE, + 'cache_on' => FALSE, + 'cachedir' => '', + 'char_set' => 'utf8', + 'dbcollat' => 'utf8_general_ci', + 'swap_pre' => '', + 'autoinit' => TRUE, + 'stricton' => FALSE, + ); + + $config = array_merge($this->config[$group], $params); + + if ( ! empty($config['dsn'])) + { + $dsn = $config['dsn']; + } + else + { + $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] + .'@'.$config['hostname'].'/'.$config['database']; + + } + + $other_params = array_slice($config, 6); + + 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'); + + try + { + $db = DB($group, $query_builder); + } + catch (Exception $e) + { + throw new InvalidArgumentException($e->getMessage()); + } + + return $db; + } +}
\ No newline at end of file |