1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, 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;
36 var $_test_items_visible = array();
38 public function __construct()
40 // These are the default items visible when a test is run.
41 $this->_test_items_visible = array (
51 log_message('debug', "Unit Testing Class Initialized");
54 // --------------------------------------------------------------------
59 * Runs the supplied tests
65 function set_test_items($items = array())
67 if ( ! empty($items) AND is_array($items))
69 $this->_test_items_visible = $items;
73 // --------------------------------------------------------------------
78 * Runs the supplied tests
86 function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
88 if ($this->active == FALSE)
93 if (in_array($expected, array('is_object', 'is_string', 'is_bool', 'is_true', 'is_false', 'is_int', 'is_numeric', 'is_float', 'is_double', 'is_array', 'is_null'), TRUE))
95 $expected = str_replace('is_float', 'is_double', $expected);
96 $result = ($expected($test)) ? TRUE : FALSE;
97 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
101 if ($this->strict == TRUE)
102 $result = ($test === $expected) ? TRUE : FALSE;
104 $result = ($test == $expected) ? TRUE : FALSE;
106 $extype = gettype($expected);
109 $back = $this->_backtrace();
112 'test_name' => $test_name,
113 'test_datatype' => gettype($test),
114 'res_datatype' => $extype,
115 'result' => ($result === TRUE) ? 'passed' : 'failed',
116 'file' => $back['file'],
117 'line' => $back['line'],
121 $this->results[] = $report;
123 return($this->report($this->result($report)));
126 // --------------------------------------------------------------------
131 * Displays a table with the test data
136 function report($result = array())
138 if (count($result) == 0)
140 $result = $this->result();
143 $CI =& get_instance();
144 $CI->load->language('unit_test');
146 $this->_parse_template();
149 foreach ($result as $res)
153 foreach ($res as $key => $val)
155 if ($key == $CI->lang->line('ut_result'))
157 if ($val == $CI->lang->line('ut_passed'))
159 $val = '<span style="color: #0C0;">'.$val.'</span>';
161 elseif ($val == $CI->lang->line('ut_failed'))
163 $val = '<span style="color: #C00;">'.$val.'</span>';
167 $temp = $this->_template_rows;
168 $temp = str_replace('{item}', $key, $temp);
169 $temp = str_replace('{result}', $val, $temp);
173 $r .= str_replace('{rows}', $table, $this->_template);
179 // --------------------------------------------------------------------
182 * Use strict comparison
184 * Causes the evaluation to use === rather than ==
190 function use_strict($state = TRUE)
192 $this->strict = ($state == FALSE) ? FALSE : TRUE;
195 // --------------------------------------------------------------------
198 * Make Unit testing active
200 * Enables/disables unit testing
206 function active($state = TRUE)
208 $this->active = ($state == FALSE) ? FALSE : TRUE;
211 // --------------------------------------------------------------------
216 * Returns the raw result data
221 function result($results = array())
223 $CI =& get_instance();
224 $CI->load->language('unit_test');
226 if (count($results) == 0)
228 $results = $this->results;
232 foreach ($results as $result)
235 foreach ($result as $key => $val)
237 if ( ! in_array($key, $this->_test_items_visible))
244 foreach ($val as $k => $v)
246 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
250 $temp[$CI->lang->line('ut_'.$k)] = $v;
255 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
259 $temp[$CI->lang->line('ut_'.$key)] = $val;
269 // --------------------------------------------------------------------
274 * This lets us set the template to be used to display results
280 function set_template($template)
282 $this->_template = $template;
285 // --------------------------------------------------------------------
288 * Generate a backtrace
290 * This lets us show file names and line numbers
295 function _backtrace()
297 if (function_exists('debug_backtrace'))
299 $back = debug_backtrace();
301 $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
302 $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
304 return array('file' => $file, 'line' => $line);
306 return array('file' => 'Unknown', 'line' => 'Unknown');
309 // --------------------------------------------------------------------
312 * Get Default Template
317 function _default_template()
319 $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';
320 $this->_template .= '{rows}';
321 $this->_template .= "\n".'</table>';
323 $this->_template_rows = "\n\t".'<tr>';
324 $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';
325 $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';
326 $this->_template_rows .= "\n\t".'</tr>';
329 // --------------------------------------------------------------------
334 * Harvests the data within the template {pseudo-variables}
339 function _parse_template()
341 if ( ! is_null($this->_template_rows))
346 if (is_null($this->_template))
348 $this->_default_template();
352 if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
354 $this->_default_template();
358 $this->_template_rows = $match['1'];
359 $this->_template = str_replace($match['0'], '{rows}', $this->_template);
363 // END Unit_test Class
366 * Helper functions to test boolean true/false
372 function is_true($test)
374 return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
376 function is_false($test)
378 return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
382 /* End of file Unit_test.php */
383 /* Location: ./system/libraries/Unit_test.php */