CI_Unit_test Class Reference

List of all members.


Public Member Functions

 CI_Unit_test ()
 run ($test, $expected=TRUE, $test_name= 'undefined')
 Run the tests.
 report ($result=array())
 Generate a report.
 use_strict ($state=TRUE)
 Use strict comparison.
 active ($state=TRUE)
 Make Unit testing active.
 result ($results=array())
 Result Array.
 set_template ($template)
 Set the template.
 _backtrace ()
 Generate a backtrace.
 _default_template ()
 Get Default Template.
 _parse_template ()
 Parse Template.

Public Attributes

 $active = TRUE
 $results = array()
 $strict = FALSE
 $_template = NULL
 $_template_rows = NULL

Detailed Description

Definition at line 29 of file Unit_test.php.


Member Function Documentation

CI_Unit_test::_backtrace (  ) 

Generate a backtrace.

This lets us show file names and line numbers

private

Returns:
array

Definition at line 259 of file Unit_test.php.

Referenced by run().

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         }

Here is the caller graph for this function:

CI_Unit_test::_default_template (  ) 

Get Default Template.

private

Returns:
string

Definition at line 281 of file Unit_test.php.

Referenced by _parse_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         }

Here is the caller graph for this function:

CI_Unit_test::_parse_template (  ) 

Parse Template.

Harvests the data within the template {pseudo-variables}

private

Returns:
void

Definition at line 303 of file Unit_test.php.

References _default_template().

Referenced by report().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Unit_test::active ( state = TRUE  ) 

Make Unit testing active.

Enables/disables unit testing

public

Parameters:
bool 
Returns:
null

Definition at line 175 of file Unit_test.php.

Referenced by run().

00176         {
00177                 $this->active = ($state == FALSE) ? FALSE : TRUE;
00178         }

Here is the caller graph for this function:

CI_Unit_test::CI_Unit_test (  ) 

Definition at line 37 of file Unit_test.php.

References log_message().

00038         {
00039                 log_message('debug', "Unit Testing Class Initialized");
00040         }       

Here is the call graph for this function:

CI_Unit_test::report ( result = array()  ) 

Generate a report.

Displays a table with the test data

public

Returns:
string

Definition at line 104 of file Unit_test.php.

References $CI, _parse_template(), get_instance(), and result().

Referenced by run().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Unit_test::result ( results = array()  ) 

Result Array.

Returns the raw result data

public

Returns:
array

Definition at line 190 of file Unit_test.php.

References $CI, $results, and get_instance().

Referenced by report(), and run().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Unit_test::run ( test,
expected = TRUE,
test_name = 'undefined' 
)

Run the tests.

Runs the supplied tests

public

Parameters:
mixed 
mixed 
string 
Returns:
string

Definition at line 55 of file Unit_test.php.

References _backtrace(), active(), report(), and result().

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         }

Here is the call graph for this function:

CI_Unit_test::set_template ( template  ) 

Set the template.

This lets us set the template to be used to display results

public

Parameters:
string 
Returns:
void

Definition at line 244 of file Unit_test.php.

00245         {
00246                 $this->_template = $template;
00247         }

CI_Unit_test::use_strict ( state = TRUE  ) 

Use strict comparison.

Causes the evaluation to use === rather then ==

public

Parameters:
bool 
Returns:
null

Definition at line 159 of file Unit_test.php.

00160         {
00161                 $this->strict = ($state == FALSE) ? FALSE : TRUE;
00162         }


Member Data Documentation

CI_Unit_test::$_template = NULL

Definition at line 34 of file Unit_test.php.

CI_Unit_test::$_template_rows = NULL

Definition at line 35 of file Unit_test.php.

CI_Unit_test::$active = TRUE

Definition at line 31 of file Unit_test.php.

CI_Unit_test::$results = array()

Definition at line 32 of file Unit_test.php.

Referenced by result().

CI_Unit_test::$strict = FALSE

Definition at line 33 of file Unit_test.php.


The documentation for this class was generated from the following file: