1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
<?php
class Mock_Database_DB {
/**
* @var array DB configuration
*/
private $config = array();
/**
* @var string DB driver name
*/
private static $dbdriver = '';
/**
* @var string DB sub-driver name
*/
private static $subdriver = '';
/**
* 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');
}
self::$dbdriver = $this->config[$group]['dbdriver'];
if (isset($this->config[$group]['subdriver']))
{
self::$subdriver = $this->config[$group]['subdriver'];
}
$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']) ? FALSE : $config['dsn'];
$subdriver = empty($config['subdriver']) ? FALSE: $config['subdriver'];
$failover = empty($config['failover']) ? FALSE : $config['failover'];
$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 ($subdriver) $other_params['subdriver'] = $subdriver;
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)
{
// Create dummy driver and builder files to "load" - the mocks have
// already triggered autoloading of the real files
$case = CI_TestCase::instance();
$driver = self::$dbdriver;
$subdriver = self::$subdriver;
$case->ci_vfs_create(array(
'DB_driver.php' => '',
'DB_forge.php' => '',
'DB_query_builder.php' => ''
), '', $case->ci_base_root, 'database');
if (file_exists(SYSTEM_PATH.'database/drivers/'.$driver.'/'.$driver.'_driver.php'))
{
$case->ci_vfs_create(array(
$driver.'_driver.php' => '',
$driver.'_forge.php' => ''
), '', $case->ci_base_root, 'database/drivers/'.$driver);
}
if ($subdriver)
{
$case->ci_vfs_create(array(
$driver.'_'.$subdriver.'_driver.php' => '',
$driver.'_'.$subdriver.'_forge.php' => ''
), '', $case->ci_base_root, 'database/drivers/'.$driver.'/subdrivers');
}
include_once(SYSTEM_PATH.'database/DB.php');
try
{
$db = DB($group, $query_builder);
}
catch (Exception $e)
{
throw new InvalidArgumentException($e->getMessage());
}
return $db;
}
}
|