blob: 04216e2a88eb73feee1c5d373ff5c96f856aca34 [file] [log] [blame]
Pascal Kriete69c97a72011-04-20 21:44:54 -04001<?php
2
3
4// Need a way to change dependencies (core libs and laoded libs)
5// Need a way to set the CI class
6
Pascal Krietefe372e32011-04-21 00:59:45 -04007class CI_TestCase extends PHPUnit_Framework_TestCase {
Pascal Kriete69c97a72011-04-20 21:44:54 -04008
Pascal Kriete69c97a72011-04-20 21:44:54 -04009 public static $global_map = array(
10 'benchmark' => 'bm',
11 'config' => 'cfg',
12 'hooks' => 'ext',
13 'utf8' => 'uni',
14 'router' => 'rtr',
15 'output' => 'out',
16 'security' => 'sec',
17 'input' => 'in',
18 'lang' => 'lang',
19
20 // @todo the loader is an edge case
21 'loader' => 'load'
22 );
23
Pascal Krietefe372e32011-04-21 00:59:45 -040024 protected $ci_config = array();
25
26 protected $ci_instance;
27 protected static $ci_test_instance;
28
29
30 public function __construct()
Pascal Kriete69c97a72011-04-20 21:44:54 -040031 {
32 parent::__construct();
33 }
34
35 // --------------------------------------------------------------------
36
Pascal Krietefe372e32011-04-21 00:59:45 -040037 /**
38 * Overwrite runBare
39 *
40 * PHPUnit instantiates the test classes before
41 * running them individually. So right before a test
42 * runs we set our instance. Normally this step would
43 * happen in setUp, but someone is bound to forget to
44 * call the parent method and debugging this is no fun.
45 */
46 public function runBare()
Pascal Kriete69c97a72011-04-20 21:44:54 -040047 {
Pascal Krietefe372e32011-04-21 00:59:45 -040048 self::$ci_test_instance = $this;
49 parent::runBare();
50 }
51
52 // --------------------------------------------------------------------
53
54 public static function instance()
55 {
56 return self::$ci_test_instance;
57 }
58
59 // --------------------------------------------------------------------
60
61 function ci_instance($obj = FALSE)
62 {
63 if ( ! is_object($obj))
64 {
65 return $this->ci_instance;
66 }
67
Pascal Kriete69c97a72011-04-20 21:44:54 -040068 $this->ci_instance = $obj;
69 }
70
71 // --------------------------------------------------------------------
72
Pascal Krietefe372e32011-04-21 00:59:45 -040073 function ci_instance_var($name, $obj = FALSE)
Pascal Kriete69c97a72011-04-20 21:44:54 -040074 {
Pascal Krietefe372e32011-04-21 00:59:45 -040075 if ( ! is_object($obj))
76 {
77 return $this->ci_instance->$name;
78 }
79
Pascal Kriete69c97a72011-04-20 21:44:54 -040080 $this->ci_instance->$name =& $obj;
81 }
82
83 // --------------------------------------------------------------------
84
85 // Set a class to a mock before it is loaded
86 function ci_library($name)
87 {
88
89 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Grab a core class
95 *
96 * Loads the correct core class without extensions
97 * and returns a reference to the class name in the
98 * globals array with the correct key. This way the
99 * test can modify the variable it assigns to and
100 * still maintain the global.
101 */
102 function &ci_core_class($name)
103 {
104 $name = strtolower($name);
105
106 if (isset(self::$global_map[$name]))
107 {
108 $class_name = ucfirst($name);
109 $global_name = self::$global_map[$name];
110 }
111 elseif (in_array($name, self::$global_map))
112 {
113 $class_name = ucfirst(array_search($name, self::$global_map));
114 $global_name = $name;
115 }
116 else
117 {
118 throw new Exception('Not a valid core class.');
119 }
120
121 if ( ! class_exists('CI_'.$class_name))
122 {
123 require_once BASEPATH.'core/'.$class_name.'.php';
124 }
125
126 $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
127 return $GLOBALS[strtoupper($global_name)];
128 }
129
130 // --------------------------------------------------------------------
131
132 // convenience function for global mocks
133 function ci_set_core_class($name, $obj)
134 {
135 $orig =& $this->ci_core_class($name);
136 $orig = $obj;
137 }
138
139 // --------------------------------------------------------------------
140
Pascal Krietefe372e32011-04-21 00:59:45 -0400141 function ci_set_config($key, $val = '')
142 {
143 if (is_array($key))
144 {
145 $this->ci_config = $key;
146 }
147 else
148 {
149 $this->ci_config[$key] = $val;
150 }
151 }
152
153 // --------------------------------------------------------------------
154
155 function ci_config_array()
156 {
157 return $this->ci_config;
158 }
159
160 // --------------------------------------------------------------------
161
162 function ci_config_item($item)
Pascal Kriete69c97a72011-04-20 21:44:54 -0400163 {
164 return '';
165 }
166}
167
168// EOF