Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Mock_Database_DB { |
| 4 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 5 | /** |
| 6 | * @var array DB configuration |
| 7 | */ |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 8 | private $config = array(); |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 9 | |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 10 | /** |
dchill42 | e9435dc | 2012-10-14 15:44:39 -0400 | [diff] [blame] | 11 | * @var string DB driver name |
| 12 | */ |
| 13 | private static $dbdriver = ''; |
| 14 | |
| 15 | /** |
| 16 | * @var string DB sub-driver name |
| 17 | */ |
| 18 | private static $subdriver = ''; |
| 19 | |
| 20 | /** |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 21 | * Prepare database configuration skeleton |
| 22 | * |
| 23 | * @param array DB configuration to set |
| 24 | * @return void |
| 25 | */ |
| 26 | public function __construct($config = array()) |
| 27 | { |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 28 | $this->config = $config; |
| 29 | } |
| 30 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 31 | /** |
| 32 | * Build DSN connection string for DB driver instantiate process |
| 33 | * |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 34 | * @param string Group name |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 35 | * @return string DSN Connection string |
| 36 | */ |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 37 | public function set_dsn($group = 'default') |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 38 | { |
| 39 | if ( ! isset($this->config[$group])) |
| 40 | { |
| 41 | throw new InvalidArgumentException('Group '.$group.' not exists'); |
| 42 | } |
| 43 | |
dchill42 | e9435dc | 2012-10-14 15:44:39 -0400 | [diff] [blame] | 44 | self::$dbdriver = $this->config[$group]['dbdriver']; |
| 45 | if (isset($this->config[$group]['subdriver'])) |
| 46 | { |
| 47 | self::$subdriver = $this->config[$group]['subdriver']; |
| 48 | } |
| 49 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 50 | $params = array( |
| 51 | 'dbprefix' => '', |
| 52 | 'pconnect' => FALSE, |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 53 | 'db_debug' => FALSE, |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 54 | 'cache_on' => FALSE, |
| 55 | 'cachedir' => '', |
| 56 | 'char_set' => 'utf8', |
| 57 | 'dbcollat' => 'utf8_general_ci', |
| 58 | 'swap_pre' => '', |
| 59 | 'autoinit' => TRUE, |
| 60 | 'stricton' => FALSE, |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 61 | ); |
| 62 | |
| 63 | $config = array_merge($this->config[$group], $params); |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 64 | $dsnstring = empty($config['dsn']) ? FALSE : $config['dsn']; |
| 65 | $subdriver = empty($config['subdriver']) ? FALSE: $config['subdriver']; |
| 66 | $failover = empty($config['failover']) ? FALSE : $config['failover']; |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 67 | |
Taufan Aditya | 1972514 | 2012-04-04 23:46:23 +0700 | [diff] [blame] | 68 | $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password'] |
dchill42 | e9435dc | 2012-10-14 15:44:39 -0400 | [diff] [blame] | 69 | .'@'.$config['hostname'].'/'.$config['database']; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 70 | |
Taufan Aditya | f4c6c9b | 2012-04-04 23:24:09 +0700 | [diff] [blame] | 71 | // Build the parameter |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 72 | $other_params = array_slice($config, 6); |
Taufan Aditya | 1972514 | 2012-04-04 23:46:23 +0700 | [diff] [blame] | 73 | if ($dsnstring) $other_params['dsn'] = $dsnstring; |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 74 | if ($subdriver) $other_params['subdriver'] = $subdriver; |
Taufan Aditya | f4c6c9b | 2012-04-04 23:24:09 +0700 | [diff] [blame] | 75 | if ($failover) $other_params['failover'] = $failover; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 76 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 77 | return $dsn.'?'.http_build_query($other_params); |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 78 | } |
| 79 | |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 80 | /** |
| 81 | * Return a database config array |
| 82 | * |
| 83 | * @see ./config |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 84 | * @param string Driver based configuration |
| 85 | * @return array |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 86 | */ |
| 87 | public static function config($driver) |
| 88 | { |
| 89 | $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 90 | return include($dir.'config'.DIRECTORY_SEPARATOR.$driver.'.php'); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Main DB method wrapper |
| 95 | * |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 96 | * @param string Group or DSN string |
| 97 | * @param bool |
| 98 | * @return object |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 99 | */ |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 100 | public static function DB($group, $query_builder = FALSE) |
| 101 | { |
dchill42 | e9435dc | 2012-10-14 15:44:39 -0400 | [diff] [blame] | 102 | // Create dummy driver and builder files to "load" - the mocks have |
| 103 | // already triggered autoloading of the real files |
| 104 | $case = CI_TestCase::instance(); |
| 105 | $driver = self::$dbdriver; |
| 106 | $subdriver = self::$subdriver; |
| 107 | $case->ci_vfs_create(array( |
| 108 | 'DB_driver.php' => '', |
| 109 | 'DB_forge.php' => '', |
| 110 | 'DB_query_builder.php' => '' |
| 111 | ), '', $case->ci_base_root, 'database'); |
| 112 | if (file_exists(SYSTEM_PATH.'database/drivers/'.$driver.'/'.$driver.'_driver.php')) |
| 113 | { |
| 114 | $case->ci_vfs_create(array( |
| 115 | $driver.'_driver.php' => '', |
| 116 | $driver.'_forge.php' => '' |
| 117 | ), '', $case->ci_base_root, 'database/drivers/'.$driver); |
| 118 | } |
| 119 | if ($subdriver) |
| 120 | { |
| 121 | $case->ci_vfs_create(array( |
| 122 | $driver.'_'.$subdriver.'_driver.php' => '', |
| 123 | $driver.'_'.$subdriver.'_forge.php' => '' |
| 124 | ), '', $case->ci_base_root, 'database/drivers/'.$driver.'/subdrivers'); |
| 125 | } |
| 126 | |
| 127 | include_once(SYSTEM_PATH.'database/DB.php'); |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 128 | |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 129 | try |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 130 | { |
| 131 | $db = DB($group, $query_builder); |
| 132 | } |
| 133 | catch (Exception $e) |
| 134 | { |
| 135 | throw new InvalidArgumentException($e->getMessage()); |
| 136 | } |
| 137 | |
| 138 | return $db; |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 139 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 140 | |
Taufan Aditya | 655a89f | 2012-03-29 03:00:56 +0700 | [diff] [blame] | 141 | } |