summaryrefslogtreecommitdiffstats
path: root/tests/mocks/database/db.php
blob: 11e4a93bd98523f279fac6c30dcf62069036e48b (plain)
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
<?php

class Mock_Database_DB {

	private $config = array();
	
	/**
	 * Prepare database configuration skeleton
	 *
	 * @param  array 	DB configuration to set
	 * @return void
	 */
	public function __construct($config = array())
	{
		$this->config = $config;
	}

	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' => TRUE,
			'cache_on' => FALSE,
			'cachedir' => '',
			'char_set' => 'utf8',
			'dbcollat' => 'utf8_general_ci',
			'swap_pre' => '',
			'autoinit' => TRUE,
			'stricton' => FALSE,
			'failover' => array()
		);

		$config = array_merge($this->config[$group], $params);

		if ( ! empty($config['dsn']))
		{
			$dsn = $config['dsn'];
		}
		else
		{
			$dsn = $config['dbdriver'].'://'.$config['username'].':'.$config['password']
			       .'@'.$config['hostname'].'/'.$config['database'];

		}

		$other_params = array_slice($config, 6);

		return $dsn.http_build_query($other_params);
	}

	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;
	}
}