blob: 10539a3af79f55147bd5403494b4e5ea4ccf8077 [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 Krieteae7b3f92011-04-21 01:21:27 -04008
9 protected $ci_config;
10 protected $ci_instance;
11 protected static $ci_test_instance;
Pascal Kriete69c97a72011-04-20 21:44:54 -040012
Pascal Krieteae7b3f92011-04-21 01:21:27 -040013 private $global_map = array(
Pascal Kriete69c97a72011-04-20 21:44:54 -040014 'benchmark' => 'bm',
15 'config' => 'cfg',
16 'hooks' => 'ext',
17 'utf8' => 'uni',
18 'router' => 'rtr',
19 'output' => 'out',
20 'security' => 'sec',
21 'input' => 'in',
22 'lang' => 'lang',
Pascal Kriete69c97a72011-04-20 21:44:54 -040023 // @todo the loader is an edge case
Greg Aker8da69032011-04-21 11:28:27 -050024 'loader' => 'load',
25 'model' => 'model'
Pascal Kriete69c97a72011-04-20 21:44:54 -040026 );
27
Pascal Krietefe372e32011-04-21 00:59:45 -040028 public function __construct()
Pascal Kriete69c97a72011-04-20 21:44:54 -040029 {
30 parent::__construct();
Pascal Krieteae7b3f92011-04-21 01:21:27 -040031
32 $this->ci_config = array();
Pascal Kriete69c97a72011-04-20 21:44:54 -040033 }
34
35 // --------------------------------------------------------------------
36
Pascal Krieteae7b3f92011-04-21 01:21:27 -040037 function ci_set_config($key, $val = '')
Pascal Kriete69c97a72011-04-20 21:44:54 -040038 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -040039 if (is_array($key))
40 {
41 $this->ci_config = $key;
42 }
43 else
44 {
45 $this->ci_config[$key] = $val;
46 }
Pascal Krietefe372e32011-04-21 00:59:45 -040047 }
48
49 // --------------------------------------------------------------------
50
51 function ci_instance($obj = FALSE)
52 {
53 if ( ! is_object($obj))
54 {
55 return $this->ci_instance;
56 }
57
Pascal Kriete69c97a72011-04-20 21:44:54 -040058 $this->ci_instance = $obj;
59 }
60
61 // --------------------------------------------------------------------
62
Pascal Krietefe372e32011-04-21 00:59:45 -040063 function ci_instance_var($name, $obj = FALSE)
Pascal Kriete69c97a72011-04-20 21:44:54 -040064 {
Pascal Krietefe372e32011-04-21 00:59:45 -040065 if ( ! is_object($obj))
66 {
67 return $this->ci_instance->$name;
68 }
69
Pascal Kriete69c97a72011-04-20 21:44:54 -040070 $this->ci_instance->$name =& $obj;
71 }
72
73 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -040074
75 /**
76 * Grab a core class
77 *
78 * Loads the correct core class without extensions
79 * and returns a reference to the class name in the
80 * globals array with the correct key. This way the
81 * test can modify the variable it assigns to and
82 * still maintain the global.
83 */
84 function &ci_core_class($name)
85 {
86 $name = strtolower($name);
87
Pascal Krieteae7b3f92011-04-21 01:21:27 -040088 if (isset($this->global_map[$name]))
Pascal Kriete69c97a72011-04-20 21:44:54 -040089 {
90 $class_name = ucfirst($name);
Pascal Krieteae7b3f92011-04-21 01:21:27 -040091 $global_name = $this->global_map[$name];
Pascal Kriete69c97a72011-04-20 21:44:54 -040092 }
Pascal Krieteae7b3f92011-04-21 01:21:27 -040093 elseif (in_array($name, $this->global_map))
Pascal Kriete69c97a72011-04-20 21:44:54 -040094 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -040095 $class_name = ucfirst(array_search($name, $this->global_map));
Pascal Kriete69c97a72011-04-20 21:44:54 -040096 $global_name = $name;
97 }
98 else
99 {
100 throw new Exception('Not a valid core class.');
101 }
102
103 if ( ! class_exists('CI_'.$class_name))
104 {
105 require_once BASEPATH.'core/'.$class_name.'.php';
106 }
107
108 $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
109 return $GLOBALS[strtoupper($global_name)];
110 }
111
112 // --------------------------------------------------------------------
113
114 // convenience function for global mocks
115 function ci_set_core_class($name, $obj)
116 {
117 $orig =& $this->ci_core_class($name);
118 $orig = $obj;
119 }
120
121 // --------------------------------------------------------------------
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400122 // Internals
123 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -0400124
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400125 /**
126 * Overwrite runBare
127 *
128 * PHPUnit instantiates the test classes before
129 * running them individually. So right before a test
130 * runs we set our instance. Normally this step would
131 * happen in setUp, but someone is bound to forget to
132 * call the parent method and debugging this is no fun.
133 */
134 public function runBare()
Pascal Krietefe372e32011-04-21 00:59:45 -0400135 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400136 self::$ci_test_instance = $this;
137 parent::runBare();
Pascal Krietefe372e32011-04-21 00:59:45 -0400138 }
139
140 // --------------------------------------------------------------------
141
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400142 public static function instance()
143 {
144 return self::$ci_test_instance;
145 }
146
147 // --------------------------------------------------------------------
148
149 function ci_get_config()
Pascal Krietefe372e32011-04-21 00:59:45 -0400150 {
151 return $this->ci_config;
152 }
Pascal Kriete69c97a72011-04-20 21:44:54 -0400153}
154
155// EOF