Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.3.1 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Unit Testing Class |
| 20 | * |
| 21 | * Simple testing class |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @category UnitTesting |
| 26 | * @author ExpressionEngine Dev Team |
| 27 | * @link http://codeigniter.com/user_guide/libraries/uri.html |
| 28 | */ |
| 29 | class CI_Unit_test { |
| 30 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 31 | var $active = TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 32 | var $results = array(); |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 33 | var $strict = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 34 | var $_template = NULL; |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 35 | var $_template_rows = NULL; |
| 36 | var $_test_items_visible = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame^] | 38 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 40 | // These are the default items visible when a test is run. |
| 41 | $this->_test_items_visible = array ( |
| 42 | 'test_name', |
| 43 | 'test_datatype', |
| 44 | 'res_datatype', |
| 45 | 'result', |
| 46 | 'file', |
| 47 | 'line', |
| 48 | 'notes' |
| 49 | ); |
| 50 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | log_message('debug', "Unit Testing Class Initialized"); |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 52 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | |
| 54 | // -------------------------------------------------------------------- |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Run the tests |
| 58 | * |
| 59 | * Runs the supplied tests |
| 60 | * |
| 61 | * @access public |
| 62 | * @param array |
| 63 | * @return void |
| 64 | */ |
| 65 | function set_test_items($items = array()) |
| 66 | { |
| 67 | if ( ! empty($items) AND is_array($items)) |
| 68 | { |
| 69 | $this->_test_items_visible = $items; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // -------------------------------------------------------------------- |
| 74 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Run the tests |
| 77 | * |
| 78 | * Runs the supplied tests |
| 79 | * |
| 80 | * @access public |
| 81 | * @param mixed |
| 82 | * @param mixed |
| 83 | * @param string |
| 84 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | */ |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 86 | function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | { |
| 88 | if ($this->active == FALSE) |
| 89 | { |
| 90 | return FALSE; |
| 91 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Derek Allard | 82d6ec4 | 2009-12-22 13:53:53 +0000 | [diff] [blame] | 93 | if (in_array($expected, array('is_object', 'is_string', 'is_bool', 'is_true', 'is_false', 'is_int', 'is_numeric', 'is_float', 'is_double', 'is_array', 'is_null'), TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
| 95 | $expected = str_replace('is_float', 'is_double', $expected); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | $result = ($expected($test)) ? TRUE : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected)); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | if ($this->strict == TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | $result = ($test === $expected) ? TRUE : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | $result = ($test == $expected) ? TRUE : FALSE; |
| 105 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | $extype = gettype($expected); |
| 107 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 108 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | $back = $this->_backtrace(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | $report[] = array ( |
| 112 | 'test_name' => $test_name, |
| 113 | 'test_datatype' => gettype($test), |
| 114 | 'res_datatype' => $extype, |
| 115 | 'result' => ($result === TRUE) ? 'passed' : 'failed', |
| 116 | 'file' => $back['file'], |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 117 | 'line' => $back['line'], |
| 118 | 'notes' => $notes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | ); |
| 120 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 121 | $this->results[] = $report; |
| 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | return($this->report($this->result($report))); |
| 124 | } |
| 125 | |
| 126 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | /** |
| 129 | * Generate a report |
| 130 | * |
| 131 | * Displays a table with the test data |
| 132 | * |
| 133 | * @access public |
| 134 | * @return string |
| 135 | */ |
| 136 | function report($result = array()) |
| 137 | { |
| 138 | if (count($result) == 0) |
| 139 | { |
| 140 | $result = $this->result(); |
| 141 | } |
| 142 | |
| 143 | $CI =& get_instance(); |
| 144 | $CI->load->language('unit_test'); |
| 145 | |
| 146 | $this->_parse_template(); |
| 147 | |
| 148 | $r = ''; |
| 149 | foreach ($result as $res) |
| 150 | { |
| 151 | $table = ''; |
| 152 | |
| 153 | foreach ($res as $key => $val) |
| 154 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | if ($key == $CI->lang->line('ut_result')) |
| 156 | { |
| 157 | if ($val == $CI->lang->line('ut_passed')) |
| 158 | { |
| 159 | $val = '<span style="color: #0C0;">'.$val.'</span>'; |
| 160 | } |
| 161 | elseif ($val == $CI->lang->line('ut_failed')) |
| 162 | { |
| 163 | $val = '<span style="color: #C00;">'.$val.'</span>'; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | $temp = $this->_template_rows; |
| 168 | $temp = str_replace('{item}', $key, $temp); |
| 169 | $temp = str_replace('{result}', $val, $temp); |
| 170 | $table .= $temp; |
| 171 | } |
| 172 | |
| 173 | $r .= str_replace('{rows}', $table, $this->_template); |
| 174 | } |
| 175 | |
| 176 | return $r; |
| 177 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | /** |
| 182 | * Use strict comparison |
| 183 | * |
| 184 | * Causes the evaluation to use === rather than == |
| 185 | * |
| 186 | * @access public |
| 187 | * @param bool |
| 188 | * @return null |
| 189 | */ |
| 190 | function use_strict($state = TRUE) |
| 191 | { |
| 192 | $this->strict = ($state == FALSE) ? FALSE : TRUE; |
| 193 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | /** |
| 198 | * Make Unit testing active |
| 199 | * |
| 200 | * Enables/disables unit testing |
| 201 | * |
| 202 | * @access public |
| 203 | * @param bool |
| 204 | * @return null |
| 205 | */ |
| 206 | function active($state = TRUE) |
| 207 | { |
| 208 | $this->active = ($state == FALSE) ? FALSE : TRUE; |
| 209 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 212 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | /** |
| 214 | * Result Array |
| 215 | * |
| 216 | * Returns the raw result data |
| 217 | * |
| 218 | * @access public |
| 219 | * @return array |
| 220 | */ |
| 221 | function result($results = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 222 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | $CI =& get_instance(); |
| 224 | $CI->load->language('unit_test'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | if (count($results) == 0) |
| 227 | { |
| 228 | $results = $this->results; |
| 229 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | $retval = array(); |
| 232 | foreach ($results as $result) |
| 233 | { |
| 234 | $temp = array(); |
| 235 | foreach ($result as $key => $val) |
| 236 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 237 | if ( ! in_array($key, $this->_test_items_visible)) |
| 238 | { |
| 239 | continue; |
| 240 | } |
| 241 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | if (is_array($val)) |
| 243 | { |
| 244 | foreach ($val as $k => $v) |
| 245 | { |
| 246 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v)))) |
| 247 | { |
| 248 | $v = $line; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 249 | } |
| 250 | $temp[$CI->lang->line('ut_'.$k)] = $v; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val)))) |
| 256 | { |
| 257 | $val = $line; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 258 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | $temp[$CI->lang->line('ut_'.$key)] = $val; |
| 260 | } |
| 261 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 262 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | $retval[] = $temp; |
| 264 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 265 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | return $retval; |
| 267 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 268 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | /** |
| 272 | * Set the template |
| 273 | * |
| 274 | * This lets us set the template to be used to display results |
| 275 | * |
| 276 | * @access public |
| 277 | * @param string |
| 278 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | function set_template($template) |
| 281 | { |
| 282 | $this->_template = $template; |
| 283 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 286 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | /** |
| 288 | * Generate a backtrace |
| 289 | * |
| 290 | * This lets us show file names and line numbers |
| 291 | * |
| 292 | * @access private |
| 293 | * @return array |
| 294 | */ |
| 295 | function _backtrace() |
| 296 | { |
| 297 | if (function_exists('debug_backtrace')) |
| 298 | { |
| 299 | $back = debug_backtrace(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 300 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file']; |
| 302 | $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | return array('file' => $file, 'line' => $line); |
| 305 | } |
| 306 | return array('file' => 'Unknown', 'line' => 'Unknown'); |
| 307 | } |
| 308 | |
| 309 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 310 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | /** |
| 312 | * Get Default Template |
| 313 | * |
| 314 | * @access private |
| 315 | * @return string |
| 316 | */ |
| 317 | function _default_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">'; |
| 320 | $this->_template .= '{rows}'; |
| 321 | $this->_template .= "\n".'</table>'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | $this->_template_rows = "\n\t".'<tr>'; |
| 324 | $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'; |
| 325 | $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | $this->_template_rows .= "\n\t".'</tr>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 328 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | // -------------------------------------------------------------------- |
| 330 | |
| 331 | /** |
| 332 | * Parse Template |
| 333 | * |
| 334 | * Harvests the data within the template {pseudo-variables} |
| 335 | * |
| 336 | * @access private |
| 337 | * @return void |
| 338 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | function _parse_template() |
| 340 | { |
| 341 | if ( ! is_null($this->_template_rows)) |
| 342 | { |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | if (is_null($this->_template)) |
| 347 | { |
| 348 | $this->_default_template(); |
| 349 | return; |
| 350 | } |
| 351 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match)) |
| 353 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 354 | $this->_default_template(); |
| 355 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | $this->_template_rows = $match['1']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 359 | $this->_template = str_replace($match['0'], '{rows}', $this->_template); |
| 360 | } |
| 361 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | } |
| 363 | // END Unit_test Class |
| 364 | |
| 365 | /** |
| 366 | * Helper functions to test boolean true/false |
| 367 | * |
| 368 | * |
| 369 | * @access private |
| 370 | * @return bool |
| 371 | */ |
| 372 | function is_true($test) |
| 373 | { |
| 374 | return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE; |
| 375 | } |
| 376 | function is_false($test) |
| 377 | { |
| 378 | return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | /* End of file Unit_test.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 383 | /* Location: ./system/libraries/Unit_test.php */ |