admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.3.1 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 16 | // INITIALIZE THE CLASS --------------------------------------------------- |
| 17 | |
| 18 | $obj =& get_instance(); |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame^] | 19 | $obj->init_class('CI_Unit_test'); |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 20 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 | // ------------------------------------------------------------------------ |
| 22 | |
| 23 | /** |
| 24 | * Unit Testing Class |
| 25 | * |
| 26 | * Simple testing class |
| 27 | * |
| 28 | * @package CodeIgniter |
| 29 | * @subpackage Libraries |
| 30 | * @category UnitTesting |
| 31 | * @author Rick Ellis |
| 32 | * @link http://www.codeigniter.com/user_guide/libraries/uri.html |
| 33 | */ |
| 34 | class CI_Unit_test { |
| 35 | |
| 36 | var $active = TRUE; |
| 37 | var $results = array(); |
| 38 | var $strict = FALSE; |
| 39 | var $_template = NULL; |
| 40 | var $_template_rows = NULL; |
| 41 | |
| 42 | function CI_Unit_test() |
| 43 | { |
| 44 | log_message('debug', "Unit Testing Class Initialized"); |
| 45 | } |
| 46 | |
| 47 | // -------------------------------------------------------------------- |
| 48 | |
| 49 | /** |
| 50 | * Run the tests |
| 51 | * |
| 52 | * Runs the supplied tests |
| 53 | * |
| 54 | * @access public |
| 55 | * @param mixed |
| 56 | * @param mixed |
| 57 | * @param string |
| 58 | * @return string |
| 59 | */ |
| 60 | function run($test, $expected = TRUE, $test_name = 'undefined') |
| 61 | { |
| 62 | if ($this->active == FALSE) |
| 63 | return; |
| 64 | |
| 65 | if (in_array($expected, array('is_string', 'is_bool', 'is_true', 'is_false', 'is_int', 'is_numeric', 'is_float', 'is_double', 'is_array', 'is_null'))) |
| 66 | { |
| 67 | $expected = str_replace('is_float', 'is_double', $expected); |
| 68 | $result = ($expected($test)) ? TRUE : FALSE; |
| 69 | $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected)); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | if ($this->strict == TRUE) |
| 74 | $result = ($test === $expected) ? TRUE : FALSE; |
| 75 | else |
| 76 | $result = ($test == $expected) ? TRUE : FALSE; |
| 77 | |
| 78 | $extype = gettype($expected); |
| 79 | } |
| 80 | |
| 81 | $back = $this->_backtrace(); |
| 82 | |
| 83 | $report[] = array ( |
| 84 | 'test_name' => $test_name, |
| 85 | 'test_datatype' => gettype($test), |
| 86 | 'res_datatype' => $extype, |
| 87 | 'result' => ($result === TRUE) ? 'passed' : 'failed', |
| 88 | 'file' => $back['file'], |
| 89 | 'line' => $back['line'] |
| 90 | ); |
| 91 | |
| 92 | $this->results[] = $report; |
| 93 | |
| 94 | return($this->report($this->result($report))); |
| 95 | } |
| 96 | |
| 97 | // -------------------------------------------------------------------- |
| 98 | |
| 99 | /** |
| 100 | * Generate a report |
| 101 | * |
| 102 | * Displays a table with the test data |
| 103 | * |
| 104 | * @access public |
| 105 | * @return string |
| 106 | */ |
| 107 | function report($result = array()) |
| 108 | { |
| 109 | if (count($result) == 0) |
| 110 | { |
| 111 | $result = $this->result(); |
| 112 | } |
| 113 | |
| 114 | $this->_parse_template(); |
| 115 | |
| 116 | $r = ''; |
| 117 | foreach ($result as $res) |
| 118 | { |
| 119 | $table = ''; |
| 120 | |
| 121 | foreach ($res as $key => $val) |
| 122 | { |
| 123 | $temp = $this->_template_rows; |
| 124 | $temp = str_replace('{item}', $key, $temp); |
| 125 | $temp = str_replace('{result}', $val, $temp); |
| 126 | $table .= $temp; |
| 127 | } |
| 128 | |
| 129 | $r .= str_replace('{rows}', $table, $this->_template); |
| 130 | } |
| 131 | |
| 132 | return $r; |
| 133 | } |
| 134 | |
| 135 | // -------------------------------------------------------------------- |
| 136 | |
| 137 | /** |
| 138 | * Use strict comparison |
| 139 | * |
| 140 | * Causes the evaluation to use === rather then == |
| 141 | * |
| 142 | * @access public |
| 143 | * @param bool |
| 144 | * @return null |
| 145 | */ |
| 146 | function use_strict($state = TRUE) |
| 147 | { |
| 148 | $this->strict = ($state == FALSE) ? FALSE : TRUE; |
| 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * Make Unit testing active |
| 155 | * |
| 156 | * Enables/disables unit testing |
| 157 | * |
| 158 | * @access public |
| 159 | * @param bool |
| 160 | * @return null |
| 161 | */ |
| 162 | function active($state = TRUE) |
| 163 | { |
| 164 | $this->active = ($state == FALSE) ? FALSE : TRUE; |
| 165 | } |
| 166 | |
| 167 | // -------------------------------------------------------------------- |
| 168 | |
| 169 | /** |
| 170 | * Result Array |
| 171 | * |
| 172 | * Returns the raw result data |
| 173 | * |
| 174 | * @access public |
| 175 | * @return array |
| 176 | */ |
| 177 | function result($results = array()) |
| 178 | { |
| 179 | $obj =& get_instance(); |
| 180 | $obj->load->language('unit_test'); |
| 181 | |
| 182 | if (count($results) == 0) |
| 183 | { |
| 184 | $results = $this->results; |
| 185 | } |
| 186 | |
| 187 | $retval = array(); |
| 188 | foreach ($results as $result) |
| 189 | { |
| 190 | $temp = array(); |
| 191 | foreach ($result as $key => $val) |
| 192 | { |
| 193 | if (is_array($val)) |
| 194 | { |
| 195 | foreach ($val as $k => $v) |
| 196 | { |
| 197 | if (FALSE !== ($line = $obj->lang->line(strtolower('ut_'.$v)))) |
| 198 | { |
| 199 | $v = $line; |
| 200 | } |
| 201 | $temp[$obj->lang->line('ut_'.$k)] = $v; |
| 202 | } |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | if (FALSE !== ($line = $obj->lang->line(strtolower('ut_'.$val)))) |
| 207 | { |
| 208 | $val = $line; |
| 209 | } |
| 210 | $temp[$obj->lang->line('ut_'.$key)] = $val; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | $retval[] = $temp; |
| 215 | } |
| 216 | |
| 217 | return $retval; |
| 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
| 223 | * Set the template |
| 224 | * |
| 225 | * This lets us set the template to be used to display results |
| 226 | * |
| 227 | * @access public |
| 228 | * @params string |
| 229 | * @return void |
| 230 | */ |
admin | f6d823e | 2006-09-17 18:10:57 +0000 | [diff] [blame] | 231 | function set_template($template) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 232 | { |
admin | f6d823e | 2006-09-17 18:10:57 +0000 | [diff] [blame] | 233 | $this->_template = $template; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
| 239 | * Generate a backtrace |
| 240 | * |
| 241 | * This lets us show file names and line numbers |
| 242 | * |
| 243 | * @access private |
| 244 | * @return array |
| 245 | */ |
| 246 | function _backtrace() |
| 247 | { |
| 248 | if (function_exists('debug_backtrace')) |
| 249 | { |
| 250 | $back = debug_backtrace(); |
| 251 | |
| 252 | $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file']; |
| 253 | $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line']; |
| 254 | |
| 255 | return array('file' => $file, 'line' => $line); |
| 256 | } |
| 257 | return array('file' => 'Unknown', 'line' => 'Unknown'); |
| 258 | } |
| 259 | |
| 260 | // -------------------------------------------------------------------- |
| 261 | |
| 262 | /** |
| 263 | * Get Default Template |
| 264 | * |
| 265 | * @access private |
| 266 | * @return string |
| 267 | */ |
| 268 | function _default_template() |
| 269 | { |
| 270 | $this->_template = ' |
| 271 | <div style="margin:15px;background-color:#ccc;"> |
| 272 | <table border="0" cellpadding="4" cellspacing="1" style="width:100%;"> |
| 273 | {rows} |
| 274 | </table></div>'; |
| 275 | |
| 276 | $this->_template_rows = ' |
| 277 | <tr> |
| 278 | <td style="background-color:#fff;width:140px;font-size:12px;font-weight:bold;">{item}</td> |
| 279 | <td style="background-color:#fff;font-size:12px;">{result}</td> |
| 280 | </tr> |
| 281 | '; |
| 282 | } |
| 283 | |
| 284 | // -------------------------------------------------------------------- |
| 285 | |
| 286 | /** |
| 287 | * Parse Template |
| 288 | * |
| 289 | * Harvests the data within the template {pseudo-variables} |
| 290 | * |
| 291 | * @access private |
| 292 | * @return void |
| 293 | */ |
| 294 | function _parse_template() |
| 295 | { |
| 296 | if ( ! is_null($this->_template_rows)) |
| 297 | { |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | if (is_null($this->_template)) |
| 302 | { |
| 303 | $this->_default_template(); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match)) |
| 308 | { |
| 309 | $this->_default_template(); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | $this->_template_rows = $match['1']; |
| 314 | $this->_template = str_replace($match['0'], '{rows}', $this->_template); |
| 315 | } |
| 316 | |
| 317 | } |
| 318 | // END Unit_test Class |
| 319 | |
| 320 | /** |
| 321 | * Helper functions to test boolean true/false |
| 322 | * |
| 323 | * |
| 324 | * @access private |
| 325 | * @return bool |
| 326 | */ |
| 327 | function is_true($test) |
| 328 | { |
| 329 | return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE; |
| 330 | } |
| 331 | function is_false($test) |
| 332 | { |
| 333 | return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE; |
| 334 | } |
| 335 | |
| 336 | ?> |