Unit_test.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.3.1
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Unit Testing Class
00020  *
00021  * Simple testing class
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  Libraries
00025  * @category    UnitTesting
00026  * @author              ExpressionEngine Dev Team
00027  * @link                http://codeigniter.com/user_guide/libraries/uri.html
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          * Run the tests
00046          *
00047          * Runs the supplied tests
00048          *
00049          * @access      public
00050          * @param       mixed
00051          * @param       mixed
00052          * @param       string
00053          * @return      string
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          * Generate a report
00098          *
00099          * Displays a table with the test data
00100          *
00101          * @access      public
00102          * @return      string
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          * Use strict comparison
00152          *
00153          * Causes the evaluation to use === rather then ==
00154          *
00155          * @access      public
00156          * @param       bool
00157          * @return      null
00158          */
00159         function use_strict($state = TRUE)
00160         {
00161                 $this->strict = ($state == FALSE) ? FALSE : TRUE;
00162         }
00163         
00164         // --------------------------------------------------------------------
00165         
00166         /**
00167          * Make Unit testing active
00168          *
00169          * Enables/disables unit testing
00170          *
00171          * @access      public
00172          * @param       bool
00173          * @return      null
00174          */
00175         function active($state = TRUE)
00176         {
00177                 $this->active = ($state == FALSE) ? FALSE : TRUE;
00178         }
00179         
00180         // --------------------------------------------------------------------
00181         
00182         /**
00183          * Result Array
00184          *
00185          * Returns the raw result data
00186          *
00187          * @access      public
00188          * @return      array
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          * Set the template
00237          *
00238          * This lets us set the template to be used to display results
00239          *
00240          * @access      public
00241          * @param       string
00242          * @return      void
00243          */     
00244         function set_template($template)
00245         {
00246                 $this->_template = $template;
00247         }
00248         
00249         // --------------------------------------------------------------------
00250         
00251         /**
00252          * Generate a backtrace
00253          *
00254          * This lets us show file names and line numbers
00255          *
00256          * @access      private
00257          * @return      array
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          * Get Default Template
00277          *
00278          * @access      private
00279          * @return      string
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          * Parse Template
00297          *
00298          * Harvests the data within the template {pseudo-variables}
00299          *
00300          * @access      private
00301          * @return      void
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 // END Unit_test Class
00328 
00329 /**
00330  * Helper functions to test boolean true/false
00331  *
00332  *
00333  * @access      private
00334  * @return      bool
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 /* End of file Unit_test.php */
00347 /* Location: ./system/libraries/Unit_test.php */