blob: 00dd884b00f8dcc6a2c67389606cb50666916bdd [file] [log] [blame]
Taufan Aditya655a89f2012-03-29 03:00:56 +07001<?php
2
3class Mock_Database_DB {
4
Taufan Adityaee2f5d02012-03-30 06:29:11 +07005 /**
6 * @var array DB configuration
7 */
Taufan Aditya655a89f2012-03-29 03:00:56 +07008 private $config = array();
Andrey Andreevf243ce12012-06-09 23:34:21 +03009
Taufan Aditya655a89f2012-03-29 03:00:56 +070010 /**
dchill42e9435dc2012-10-14 15:44:39 -040011 * @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 Aditya655a89f2012-03-29 03:00:56 +070021 * Prepare database configuration skeleton
22 *
23 * @param array DB configuration to set
24 * @return void
25 */
26 public function __construct($config = array())
27 {
Taufan Aditya655a89f2012-03-29 03:00:56 +070028 $this->config = $config;
29 }
30
Taufan Adityaee2f5d02012-03-30 06:29:11 +070031 /**
32 * Build DSN connection string for DB driver instantiate process
33 *
Andrey Andreevf243ce12012-06-09 23:34:21 +030034 * @param string Group name
Taufan Adityaee2f5d02012-03-30 06:29:11 +070035 * @return string DSN Connection string
36 */
Taufan Adityaa8a2e332012-03-29 03:56:46 +070037 public function set_dsn($group = 'default')
Taufan Aditya655a89f2012-03-29 03:00:56 +070038 {
39 if ( ! isset($this->config[$group]))
40 {
41 throw new InvalidArgumentException('Group '.$group.' not exists');
42 }
43
dchill42e9435dc2012-10-14 15:44:39 -040044 self::$dbdriver = $this->config[$group]['dbdriver'];
45 if (isset($this->config[$group]['subdriver']))
46 {
47 self::$subdriver = $this->config[$group]['subdriver'];
48 }
49
Taufan Adityaa8a2e332012-03-29 03:56:46 +070050 $params = array(
51 'dbprefix' => '',
52 'pconnect' => FALSE,
Taufan Adityaee2f5d02012-03-30 06:29:11 +070053 'db_debug' => FALSE,
Taufan Adityaa8a2e332012-03-29 03:56:46 +070054 'cache_on' => FALSE,
55 'cachedir' => '',
56 'char_set' => 'utf8',
57 'dbcollat' => 'utf8_general_ci',
58 'swap_pre' => '',
Andrey Andreev6c7c8912015-02-19 14:44:18 +020059 'stricton' => FALSE
Taufan Adityaa8a2e332012-03-29 03:56:46 +070060 );
61
62 $config = array_merge($this->config[$group], $params);
Andrey Andreevfbba54e2012-06-23 20:26:31 +030063 $dsnstring = empty($config['dsn']) ? FALSE : $config['dsn'];
64 $subdriver = empty($config['subdriver']) ? FALSE: $config['subdriver'];
65 $failover = empty($config['failover']) ? FALSE : $config['failover'];
Taufan Adityaa8a2e332012-03-29 03:56:46 +070066
Taufan Aditya19725142012-04-04 23:46:23 +070067 $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password']
dchill42e9435dc2012-10-14 15:44:39 -040068 .'@'.$config['hostname'].'/'.$config['database'];
Taufan Aditya655a89f2012-03-29 03:00:56 +070069
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070070 // Build the parameter
Taufan Adityaa8a2e332012-03-29 03:56:46 +070071 $other_params = array_slice($config, 6);
Taufan Aditya19725142012-04-04 23:46:23 +070072 if ($dsnstring) $other_params['dsn'] = $dsnstring;
Andrey Andreevfbba54e2012-06-23 20:26:31 +030073 if ($subdriver) $other_params['subdriver'] = $subdriver;
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070074 if ($failover) $other_params['failover'] = $failover;
Taufan Aditya655a89f2012-03-29 03:00:56 +070075
Taufan Adityaee2f5d02012-03-30 06:29:11 +070076 return $dsn.'?'.http_build_query($other_params);
Taufan Adityaa8a2e332012-03-29 03:56:46 +070077 }
78
Taufan Adityaee2f5d02012-03-30 06:29:11 +070079 /**
80 * Return a database config array
81 *
82 * @see ./config
Andrey Andreevf243ce12012-06-09 23:34:21 +030083 * @param string Driver based configuration
84 * @return array
Taufan Adityaee2f5d02012-03-30 06:29:11 +070085 */
86 public static function config($driver)
87 {
88 $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
Taufan Adityaee2f5d02012-03-30 06:29:11 +070089 return include($dir.'config'.DIRECTORY_SEPARATOR.$driver.'.php');
90 }
91
92 /**
93 * Main DB method wrapper
94 *
Andrey Andreevf243ce12012-06-09 23:34:21 +030095 * @param string Group or DSN string
96 * @param bool
97 * @return object
Taufan Adityaee2f5d02012-03-30 06:29:11 +070098 */
Taufan Adityaa8a2e332012-03-29 03:56:46 +070099 public static function DB($group, $query_builder = FALSE)
100 {
dchill42e9435dc2012-10-14 15:44:39 -0400101 // Create dummy driver and builder files to "load" - the mocks have
102 // already triggered autoloading of the real files
103 $case = CI_TestCase::instance();
104 $driver = self::$dbdriver;
105 $subdriver = self::$subdriver;
106 $case->ci_vfs_create(array(
107 'DB_driver.php' => '',
Andrey Andreev306ad862013-07-15 13:32:55 +0300108 'DB_result.php' => '',
dchill42e9435dc2012-10-14 15:44:39 -0400109 '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' => '',
Andrey Andreev306ad862013-07-15 13:32:55 +0300116 $driver.'_result.php' => '',
dchill42e9435dc2012-10-14 15:44:39 -0400117 $driver.'_forge.php' => ''
118 ), '', $case->ci_base_root, 'database/drivers/'.$driver);
119 }
120 if ($subdriver)
121 {
122 $case->ci_vfs_create(array(
123 $driver.'_'.$subdriver.'_driver.php' => '',
124 $driver.'_'.$subdriver.'_forge.php' => ''
125 ), '', $case->ci_base_root, 'database/drivers/'.$driver.'/subdrivers');
126 }
127
128 include_once(SYSTEM_PATH.'database/DB.php');
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700129
Andrey Andreevf243ce12012-06-09 23:34:21 +0300130 try
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700131 {
132 $db = DB($group, $query_builder);
133 }
134 catch (Exception $e)
135 {
Andrey Andreev54fb6f62015-02-19 15:13:39 +0200136 throw new RuntimeException($e->getMessage());
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700137 }
138
139 return $db;
Taufan Aditya655a89f2012-03-29 03:00:56 +0700140 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300141
Taufan Aditya655a89f2012-03-29 03:00:56 +0700142}