1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.3.1
\r
16 // ------------------------------------------------------------------------
\r
19 * Unit Testing Class
\r
21 * Simple testing class
\r
23 * @package CodeIgniter
\r
24 * @subpackage Libraries
\r
25 * @category UnitTesting
\r
26 * @author ExpressionEngine Dev Team
\r
27 * @link http://codeigniter.com/user_guide/libraries/uri.html
\r
29 class CI_Unit_test {
\r
32 var $results = array();
\r
33 var $strict = FALSE;
\r
34 var $_template = NULL;
\r
35 var $_template_rows = NULL;
\r
37 function CI_Unit_test()
\r
39 log_message('debug', "Unit Testing Class Initialized");
\r
42 // --------------------------------------------------------------------
\r
47 * Runs the supplied tests
\r
55 function run($test, $expected = TRUE, $test_name = 'undefined')
\r
57 if ($this->active == FALSE)
\r
62 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))
\r
64 $expected = str_replace('is_float', 'is_double', $expected);
\r
65 $result = ($expected($test)) ? TRUE : FALSE;
\r
66 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
\r
70 if ($this->strict == TRUE)
\r
71 $result = ($test === $expected) ? TRUE : FALSE;
\r
73 $result = ($test == $expected) ? TRUE : FALSE;
\r
75 $extype = gettype($expected);
\r
78 $back = $this->_backtrace();
\r
81 'test_name' => $test_name,
\r
82 'test_datatype' => gettype($test),
\r
83 'res_datatype' => $extype,
\r
84 'result' => ($result === TRUE) ? 'passed' : 'failed',
\r
85 'file' => $back['file'],
\r
86 'line' => $back['line']
\r
89 $this->results[] = $report;
\r
91 return($this->report($this->result($report)));
\r
94 // --------------------------------------------------------------------
\r
99 * Displays a table with the test data
\r
104 function report($result = array())
\r
106 if (count($result) == 0)
\r
108 $result = $this->result();
\r
111 $CI =& get_instance();
\r
112 $CI->load->language('unit_test');
\r
114 $this->_parse_template();
\r
117 foreach ($result as $res)
\r
121 foreach ($res as $key => $val)
\r
124 if ($key == $CI->lang->line('ut_result'))
\r
126 if ($val == $CI->lang->line('ut_passed'))
\r
128 $val = '<span style="color: #0C0;">'.$val.'</span>';
\r
130 elseif ($val == $CI->lang->line('ut_failed'))
\r
132 $val = '<span style="color: #C00;">'.$val.'</span>';
\r
136 $temp = $this->_template_rows;
\r
137 $temp = str_replace('{item}', $key, $temp);
\r
138 $temp = str_replace('{result}', $val, $temp);
\r
142 $r .= str_replace('{rows}', $table, $this->_template);
\r
148 // --------------------------------------------------------------------
\r
151 * Use strict comparison
\r
153 * Causes the evaluation to use === rather than ==
\r
159 function use_strict($state = TRUE)
\r
161 $this->strict = ($state == FALSE) ? FALSE : TRUE;
\r
164 // --------------------------------------------------------------------
\r
167 * Make Unit testing active
\r
169 * Enables/disables unit testing
\r
175 function active($state = TRUE)
\r
177 $this->active = ($state == FALSE) ? FALSE : TRUE;
\r
180 // --------------------------------------------------------------------
\r
185 * Returns the raw result data
\r
190 function result($results = array())
\r
192 $CI =& get_instance();
\r
193 $CI->load->language('unit_test');
\r
195 if (count($results) == 0)
\r
197 $results = $this->results;
\r
201 foreach ($results as $result)
\r
204 foreach ($result as $key => $val)
\r
206 if (is_array($val))
\r
208 foreach ($val as $k => $v)
\r
210 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
\r
214 $temp[$CI->lang->line('ut_'.$k)] = $v;
\r
219 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
\r
223 $temp[$CI->lang->line('ut_'.$key)] = $val;
\r
233 // --------------------------------------------------------------------
\r
238 * This lets us set the template to be used to display results
\r
244 function set_template($template)
\r
246 $this->_template = $template;
\r
249 // --------------------------------------------------------------------
\r
252 * Generate a backtrace
\r
254 * This lets us show file names and line numbers
\r
259 function _backtrace()
\r
261 if (function_exists('debug_backtrace'))
\r
263 $back = debug_backtrace();
\r
265 $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
\r
266 $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
\r
268 return array('file' => $file, 'line' => $line);
\r
270 return array('file' => 'Unknown', 'line' => 'Unknown');
\r
273 // --------------------------------------------------------------------
\r
276 * Get Default Template
\r
281 function _default_template()
\r
283 $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';
\r
284 $this->_template .= '{rows}';
\r
285 $this->_template .= "\n".'</table>';
\r
287 $this->_template_rows = "\n\t".'<tr>';
\r
288 $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';
\r
289 $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';
\r
290 $this->_template_rows .= "\n\t".'</tr>';
\r
293 // --------------------------------------------------------------------
\r
298 * Harvests the data within the template {pseudo-variables}
\r
303 function _parse_template()
\r
305 if ( ! is_null($this->_template_rows))
\r
310 if (is_null($this->_template))
\r
312 $this->_default_template();
\r
316 if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
\r
318 $this->_default_template();
\r
322 $this->_template_rows = $match['1'];
\r
323 $this->_template = str_replace($match['0'], '{rows}', $this->_template);
\r
327 // END Unit_test Class
\r
330 * Helper functions to test boolean true/false
\r
336 function is_true($test)
\r
338 return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
\r
340 function is_false($test)
\r
342 return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
\r
346 /* End of file Unit_test.php */
\r
347 /* Location: ./system/libraries/Unit_test.php */