Unit_test.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class CI_Unit_test {
00030
00031 var $active = TRUE;
00032 var $results = array();
00033 var $strict = FALSE;
00034 var $_template = NULL;
00035 var $_template_rows = NULL;
00036
00037 function CI_Unit_test()
00038 {
00039 log_message('debug', "Unit Testing Class Initialized");
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 function run($test, $expected = TRUE, $test_name = 'undefined')
00056 {
00057 if ($this->active == FALSE)
00058 {
00059 return FALSE;
00060 }
00061
00062 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))
00063 {
00064 $expected = str_replace('is_float', 'is_double', $expected);
00065 $result = ($expected($test)) ? TRUE : FALSE;
00066 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
00067 }
00068 else
00069 {
00070 if ($this->strict == TRUE)
00071 $result = ($test === $expected) ? TRUE : FALSE;
00072 else
00073 $result = ($test == $expected) ? TRUE : FALSE;
00074
00075 $extype = gettype($expected);
00076 }
00077
00078 $back = $this->_backtrace();
00079
00080 $report[] = array (
00081 'test_name' => $test_name,
00082 'test_datatype' => gettype($test),
00083 'res_datatype' => $extype,
00084 'result' => ($result === TRUE) ? 'passed' : 'failed',
00085 'file' => $back['file'],
00086 'line' => $back['line']
00087 );
00088
00089 $this->results[] = $report;
00090
00091 return($this->report($this->result($report)));
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 function report($result = array())
00105 {
00106 if (count($result) == 0)
00107 {
00108 $result = $this->result();
00109 }
00110
00111 $CI =& get_instance();
00112 $CI->load->language('unit_test');
00113
00114 $this->_parse_template();
00115
00116 $r = '';
00117 foreach ($result as $res)
00118 {
00119 $table = '';
00120
00121 foreach ($res as $key => $val)
00122 {
00123
00124 if ($key == $CI->lang->line('ut_result'))
00125 {
00126 if ($val == $CI->lang->line('ut_passed'))
00127 {
00128 $val = '<span style="color: #0C0;">'.$val.'</span>';
00129 }
00130 elseif ($val == $CI->lang->line('ut_failed'))
00131 {
00132 $val = '<span style="color: #C00;">'.$val.'</span>';
00133 }
00134 }
00135
00136 $temp = $this->_template_rows;
00137 $temp = str_replace('{item}', $key, $temp);
00138 $temp = str_replace('{result}', $val, $temp);
00139 $table .= $temp;
00140 }
00141
00142 $r .= str_replace('{rows}', $table, $this->_template);
00143 }
00144
00145 return $r;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 function use_strict($state = TRUE)
00160 {
00161 $this->strict = ($state == FALSE) ? FALSE : TRUE;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 function active($state = TRUE)
00176 {
00177 $this->active = ($state == FALSE) ? FALSE : TRUE;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 function result($results = array())
00191 {
00192 $CI =& get_instance();
00193 $CI->load->language('unit_test');
00194
00195 if (count($results) == 0)
00196 {
00197 $results = $this->results;
00198 }
00199
00200 $retval = array();
00201 foreach ($results as $result)
00202 {
00203 $temp = array();
00204 foreach ($result as $key => $val)
00205 {
00206 if (is_array($val))
00207 {
00208 foreach ($val as $k => $v)
00209 {
00210 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
00211 {
00212 $v = $line;
00213 }
00214 $temp[$CI->lang->line('ut_'.$k)] = $v;
00215 }
00216 }
00217 else
00218 {
00219 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
00220 {
00221 $val = $line;
00222 }
00223 $temp[$CI->lang->line('ut_'.$key)] = $val;
00224 }
00225 }
00226
00227 $retval[] = $temp;
00228 }
00229
00230 return $retval;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 function set_template($template)
00245 {
00246 $this->_template = $template;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 function _backtrace()
00260 {
00261 if (function_exists('debug_backtrace'))
00262 {
00263 $back = debug_backtrace();
00264
00265 $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
00266 $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
00267
00268 return array('file' => $file, 'line' => $line);
00269 }
00270 return array('file' => 'Unknown', 'line' => 'Unknown');
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 function _default_template()
00282 {
00283 $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';
00284 $this->_template .= '{rows}';
00285 $this->_template .= "\n".'</table>';
00286
00287 $this->_template_rows = "\n\t".'<tr>';
00288 $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';
00289 $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';
00290 $this->_template_rows .= "\n\t".'</tr>';
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 function _parse_template()
00304 {
00305 if ( ! is_null($this->_template_rows))
00306 {
00307 return;
00308 }
00309
00310 if (is_null($this->_template))
00311 {
00312 $this->_default_template();
00313 return;
00314 }
00315
00316 if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
00317 {
00318 $this->_default_template();
00319 return;
00320 }
00321
00322 $this->_template_rows = $match['1'];
00323 $this->_template = str_replace($match['0'], '{rows}', $this->_template);
00324 }
00325
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 function is_true($test)
00337 {
00338 return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
00339 }
00340 function is_false($test)
00341 {
00342 return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
00343 }
00344
00345
00346
00347