summaryrefslogtreecommitdiffstats
path: root/tests/mocks/ci_testcase.php
blob: 5c83974b3faf31c867210d41384ff3fa02602caf (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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

class CI_TestCase extends PHPUnit_Framework_TestCase {
	
	protected $ci_config;
	protected $ci_instance;
	protected static $ci_test_instance;
		
	private $global_map = array(
		'benchmark'	=> 'bm',
		'config'	=> 'cfg',
		'hooks'		=> 'ext',
		'utf8'		=> 'uni',
		'router'	=> 'rtr',
		'output'	=> 'out',
		'security'	=> 'sec',
		'input'		=> 'in',
		'lang'		=> 'lang',
		'loader'	=> 'load',
		'model'		=> 'model'
	);
	
	// --------------------------------------------------------------------
	
	public function __construct()
	{
		parent::__construct();
		
		$this->ci_config = array();
	}
	
	// --------------------------------------------------------------------
	
	public function setUp()
	{
		if (method_exists($this, 'set_up'))
		{
			$this->set_up();
		}
	}
	
	// --------------------------------------------------------------------
	
	public function tearDown() 
	{
		if (method_exists($this, 'tear_down'))
		{
			$this->tear_down();
		}
	}
	
	// --------------------------------------------------------------------
	
	function ci_set_config($key, $val = '')
	{
		if (is_array($key))
		{
			$this->ci_config = $key;
		}
		else
		{
			$this->ci_config[$key] = $val;
		}
	}
	
	// --------------------------------------------------------------------
	
	function ci_instance($obj = FALSE)
	{
		if ( ! is_object($obj))
		{
			return $this->ci_instance;
		}
		
		$this->ci_instance = $obj;
	}
	
	// --------------------------------------------------------------------
	
	function ci_instance_var($name, $obj = FALSE)
	{
		if ( ! is_object($obj))
		{
			return $this->ci_instance->$name;
		}
		
		$this->ci_instance->$name =& $obj;
	}
	
	// --------------------------------------------------------------------

	/**
	 * Grab a core class
	 *
	 * Loads the correct core class without extensions
	 * and returns a reference to the class name in the
	 * globals array with the correct key. This way the
	 * test can modify the variable it assigns to and
	 * still maintain the global.
	 */
	function &ci_core_class($name)
	{
		$name = strtolower($name);
		
		if (isset($this->global_map[$name]))
		{
			$class_name = ucfirst($name);
			$global_name = $this->global_map[$name];
		}
		elseif (in_array($name, $this->global_map))
		{
			$class_name = ucfirst(array_search($name, $this->global_map));
			$global_name = $name;
		}
		else
		{
			throw new Exception('Not a valid core class.');
		}
		
		if ( ! class_exists('CI_'.$class_name))
		{
			require_once BASEPATH.'core/'.$class_name.'.php';
		}
		
		$GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
		return $GLOBALS[strtoupper($global_name)];
	}
	
	// --------------------------------------------------------------------
	
	// convenience function for global mocks
	function ci_set_core_class($name, $obj)
	{
		$orig =& $this->ci_core_class($name);
		$orig = $obj;
	}
	
	// --------------------------------------------------------------------
	// Internals
	// --------------------------------------------------------------------
	
	/**
	 * Overwrite runBare
	 *
	 * PHPUnit instantiates the test classes before
	 * running them individually. So right before a test
	 * runs we set our instance. Normally this step would
	 * happen in setUp, but someone is bound to forget to
	 * call the parent method and debugging this is no fun.
	 */
	public function runBare()
	{
		self::$ci_test_instance = $this;
		parent::runBare();
	}
	
	// --------------------------------------------------------------------
	
	public static function instance()
	{
		return self::$ci_test_instance;
	}
	
	// --------------------------------------------------------------------
	
	function ci_get_config()
	{
		return $this->ci_config;
	}

	// --------------------------------------------------------------------
	
	/**
	 * This overload is useful to create a stub, that need to have a specific method.
	 */
	function __call($method, $args)
	{
		if ($this->{$method} instanceof Closure) 
		{
			return call_user_func_array($this->{$method},$args);
		} 
		else 
		{
			return parent::__call($method, $args);
		}
	}
}

// EOF