diff options
author | Andrey Andreev <narf@bofh.bg> | 2012-05-02 12:27:30 +0200 |
---|---|---|
committer | Andrey Andreev <narf@bofh.bg> | 2012-05-02 12:27:30 +0200 |
commit | 14aa3178e12f285bed006511902e50aaae69bada (patch) | |
tree | a33410e21a5de095070fc5cc6d14b2649954e0a7 /tests/mocks/database/db.php | |
parent | 3ea6b1fd8fb0492d84c4879e039d11713c9f39a5 (diff) | |
parent | 963c96c5507ceb8b5c3de50d0ab959d21dcc8cd1 (diff) |
Merge upstream branch
Diffstat (limited to 'tests/mocks/database/db.php')
-rw-r--r-- | tests/mocks/database/db.php | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/mocks/database/db.php b/tests/mocks/database/db.php new file mode 100644 index 000000000..59028ed9c --- /dev/null +++ b/tests/mocks/database/db.php @@ -0,0 +1,100 @@ +<?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); + $dsnstring = ( ! empty($config['dsn'])) ? $config['dsn'] : FALSE; + $pdodriver = ( ! empty($config['pdodriver'])) ? $config['pdodriver'] : FALSE; + $failover = ( ! empty($config['failover'])) ? $config['failover'] : FALSE; + + $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] + .'@'.$config['hostname'].'/'.$config['database']; + + // Build the parameter + $other_params = array_slice($config, 6); + if ($dsnstring) $other_params['dsn'] = $dsnstring; + if ($pdodriver) $other_params['pdodriver'] = $pdodriver; + if ($failover) $other_params['failover'] = $failover; + + 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 |