blob: 968476dea7bd00ffe73ac1d847484ae215fb9abf [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' => '',
59 'autoinit' => TRUE,
60 'stricton' => FALSE,
Taufan Adityaa8a2e332012-03-29 03:56:46 +070061 );
62
63 $config = array_merge($this->config[$group], $params);
Andrey Andreevfbba54e2012-06-23 20:26:31 +030064 $dsnstring = empty($config['dsn']) ? FALSE : $config['dsn'];
65 $subdriver = empty($config['subdriver']) ? FALSE: $config['subdriver'];
66 $failover = empty($config['failover']) ? FALSE : $config['failover'];
Taufan Adityaa8a2e332012-03-29 03:56:46 +070067
Taufan Aditya19725142012-04-04 23:46:23 +070068 $dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password']
dchill42e9435dc2012-10-14 15:44:39 -040069 .'@'.$config['hostname'].'/'.$config['database'];
Taufan Aditya655a89f2012-03-29 03:00:56 +070070
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070071 // Build the parameter
Taufan Adityaa8a2e332012-03-29 03:56:46 +070072 $other_params = array_slice($config, 6);
Taufan Aditya19725142012-04-04 23:46:23 +070073 if ($dsnstring) $other_params['dsn'] = $dsnstring;
Andrey Andreevfbba54e2012-06-23 20:26:31 +030074 if ($subdriver) $other_params['subdriver'] = $subdriver;
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070075 if ($failover) $other_params['failover'] = $failover;
Taufan Aditya655a89f2012-03-29 03:00:56 +070076
Taufan Adityaee2f5d02012-03-30 06:29:11 +070077 return $dsn.'?'.http_build_query($other_params);
Taufan Adityaa8a2e332012-03-29 03:56:46 +070078 }
79
Taufan Adityaee2f5d02012-03-30 06:29:11 +070080 /**
81 * Return a database config array
82 *
83 * @see ./config
Andrey Andreevf243ce12012-06-09 23:34:21 +030084 * @param string Driver based configuration
85 * @return array
Taufan Adityaee2f5d02012-03-30 06:29:11 +070086 */
87 public static function config($driver)
88 {
89 $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
Taufan Adityaee2f5d02012-03-30 06:29:11 +070090 return include($dir.'config'.DIRECTORY_SEPARATOR.$driver.'.php');
91 }
92
93 /**
94 * Main DB method wrapper
95 *
Andrey Andreevf243ce12012-06-09 23:34:21 +030096 * @param string Group or DSN string
97 * @param bool
98 * @return object
Taufan Adityaee2f5d02012-03-30 06:29:11 +070099 */
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700100 public static function DB($group, $query_builder = FALSE)
101 {
dchill42e9435dc2012-10-14 15:44:39 -0400102 // 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' => '',
Andrey Andreev306ad862013-07-15 13:32:55 +0300109 'DB_result.php' => '',
dchill42e9435dc2012-10-14 15:44:39 -0400110 'DB_forge.php' => '',
111 'DB_query_builder.php' => ''
112 ), '', $case->ci_base_root, 'database');
113 if (file_exists(SYSTEM_PATH.'database/drivers/'.$driver.'/'.$driver.'_driver.php'))
114 {
115 $case->ci_vfs_create(array(
116 $driver.'_driver.php' => '',
Andrey Andreev306ad862013-07-15 13:32:55 +0300117 $driver.'_result.php' => '',
dchill42e9435dc2012-10-14 15:44:39 -0400118 $driver.'_forge.php' => ''
119 ), '', $case->ci_base_root, 'database/drivers/'.$driver);
120 }
121 if ($subdriver)
122 {
123 $case->ci_vfs_create(array(
124 $driver.'_'.$subdriver.'_driver.php' => '',
125 $driver.'_'.$subdriver.'_forge.php' => ''
126 ), '', $case->ci_base_root, 'database/drivers/'.$driver.'/subdrivers');
127 }
128
129 include_once(SYSTEM_PATH.'database/DB.php');
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700130
Andrey Andreevf243ce12012-06-09 23:34:21 +0300131 try
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700132 {
133 $db = DB($group, $query_builder);
134 }
135 catch (Exception $e)
136 {
137 throw new InvalidArgumentException($e->getMessage());
138 }
139
140 return $db;
Taufan Aditya655a89f2012-03-29 03:00:56 +0700141 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300142
Taufan Aditya655a89f2012-03-29 03:00:56 +0700143}