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