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