Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.3.1 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Unit Testing Class |
| 32 | * |
| 33 | * Simple testing class |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Libraries |
| 37 | * @category UnitTesting |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 38 | * @author EllisLab Dev Team |
Gerry | 33c9c3f | 2011-09-25 00:32:38 +0800 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/libraries/unit_testing.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | */ |
| 41 | class CI_Unit_test { |
| 42 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 43 | var $active = TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | var $results = array(); |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 45 | var $strict = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | var $_template = NULL; |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 47 | var $_template_rows = NULL; |
| 48 | var $_test_items_visible = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 50 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 52 | // These are the default items visible when a test is run. |
| 53 | $this->_test_items_visible = array ( |
| 54 | 'test_name', |
| 55 | 'test_datatype', |
| 56 | 'res_datatype', |
| 57 | 'result', |
| 58 | 'file', |
| 59 | 'line', |
| 60 | 'notes' |
| 61 | ); |
| 62 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | log_message('debug', "Unit Testing Class Initialized"); |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 64 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | |
| 66 | // -------------------------------------------------------------------- |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Run the tests |
| 70 | * |
| 71 | * Runs the supplied tests |
| 72 | * |
| 73 | * @access public |
| 74 | * @param array |
| 75 | * @return void |
| 76 | */ |
| 77 | function set_test_items($items = array()) |
| 78 | { |
| 79 | if ( ! empty($items) AND is_array($items)) |
| 80 | { |
| 81 | $this->_test_items_visible = $items; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | /** |
| 88 | * Run the tests |
| 89 | * |
| 90 | * Runs the supplied tests |
| 91 | * |
| 92 | * @access public |
| 93 | * @param mixed |
| 94 | * @param mixed |
| 95 | * @param string |
| 96 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | */ |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 98 | function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | { |
| 100 | if ($this->active == FALSE) |
| 101 | { |
| 102 | return FALSE; |
| 103 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | |
Derek Allard | 82d6ec4 | 2009-12-22 13:53:53 +0000 | [diff] [blame] | 105 | 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)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | { |
| 107 | $expected = str_replace('is_float', 'is_double', $expected); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 108 | $result = ($expected($test)) ? TRUE : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected)); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | if ($this->strict == TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | $result = ($test === $expected) ? TRUE : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | $result = ($test == $expected) ? TRUE : FALSE; |
| 117 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | $extype = gettype($expected); |
| 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | $back = $this->_backtrace(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | $report[] = array ( |
| 124 | 'test_name' => $test_name, |
| 125 | 'test_datatype' => gettype($test), |
| 126 | 'res_datatype' => $extype, |
| 127 | 'result' => ($result === TRUE) ? 'passed' : 'failed', |
| 128 | 'file' => $back['file'], |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 129 | 'line' => $back['line'], |
| 130 | 'notes' => $notes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | ); |
| 132 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 133 | $this->results[] = $report; |
| 134 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | return($this->report($this->result($report))); |
| 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | /** |
| 141 | * Generate a report |
| 142 | * |
| 143 | * Displays a table with the test data |
| 144 | * |
| 145 | * @access public |
| 146 | * @return string |
| 147 | */ |
| 148 | function report($result = array()) |
| 149 | { |
| 150 | if (count($result) == 0) |
| 151 | { |
| 152 | $result = $this->result(); |
| 153 | } |
| 154 | |
| 155 | $CI =& get_instance(); |
| 156 | $CI->load->language('unit_test'); |
| 157 | |
| 158 | $this->_parse_template(); |
| 159 | |
| 160 | $r = ''; |
| 161 | foreach ($result as $res) |
| 162 | { |
| 163 | $table = ''; |
| 164 | |
| 165 | foreach ($res as $key => $val) |
| 166 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | if ($key == $CI->lang->line('ut_result')) |
| 168 | { |
| 169 | if ($val == $CI->lang->line('ut_passed')) |
| 170 | { |
| 171 | $val = '<span style="color: #0C0;">'.$val.'</span>'; |
| 172 | } |
| 173 | elseif ($val == $CI->lang->line('ut_failed')) |
| 174 | { |
| 175 | $val = '<span style="color: #C00;">'.$val.'</span>'; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | $temp = $this->_template_rows; |
| 180 | $temp = str_replace('{item}', $key, $temp); |
| 181 | $temp = str_replace('{result}', $val, $temp); |
| 182 | $table .= $temp; |
| 183 | } |
| 184 | |
| 185 | $r .= str_replace('{rows}', $table, $this->_template); |
| 186 | } |
| 187 | |
| 188 | return $r; |
| 189 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 190 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | /** |
| 194 | * Use strict comparison |
| 195 | * |
| 196 | * Causes the evaluation to use === rather than == |
| 197 | * |
| 198 | * @access public |
| 199 | * @param bool |
| 200 | * @return null |
| 201 | */ |
| 202 | function use_strict($state = TRUE) |
| 203 | { |
| 204 | $this->strict = ($state == FALSE) ? FALSE : TRUE; |
| 205 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 208 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | /** |
| 210 | * Make Unit testing active |
| 211 | * |
| 212 | * Enables/disables unit testing |
| 213 | * |
| 214 | * @access public |
| 215 | * @param bool |
| 216 | * @return null |
| 217 | */ |
| 218 | function active($state = TRUE) |
| 219 | { |
| 220 | $this->active = ($state == FALSE) ? FALSE : TRUE; |
| 221 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 222 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | /** |
| 226 | * Result Array |
| 227 | * |
| 228 | * Returns the raw result data |
| 229 | * |
| 230 | * @access public |
| 231 | * @return array |
| 232 | */ |
| 233 | function result($results = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | $CI =& get_instance(); |
| 236 | $CI->load->language('unit_test'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 237 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | if (count($results) == 0) |
| 239 | { |
| 240 | $results = $this->results; |
| 241 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | $retval = array(); |
| 244 | foreach ($results as $result) |
| 245 | { |
| 246 | $temp = array(); |
| 247 | foreach ($result as $key => $val) |
| 248 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 249 | if ( ! in_array($key, $this->_test_items_visible)) |
| 250 | { |
| 251 | continue; |
| 252 | } |
| 253 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | if (is_array($val)) |
| 255 | { |
| 256 | foreach ($val as $k => $v) |
| 257 | { |
| 258 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v)))) |
| 259 | { |
| 260 | $v = $line; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | } |
| 262 | $temp[$CI->lang->line('ut_'.$k)] = $v; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val)))) |
| 268 | { |
| 269 | $val = $line; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | $temp[$CI->lang->line('ut_'.$key)] = $val; |
| 272 | } |
| 273 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | $retval[] = $temp; |
| 276 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | return $retval; |
| 279 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | /** |
| 284 | * Set the template |
| 285 | * |
| 286 | * This lets us set the template to be used to display results |
| 287 | * |
| 288 | * @access public |
| 289 | * @param string |
| 290 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 291 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | function set_template($template) |
| 293 | { |
| 294 | $this->_template = $template; |
| 295 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 298 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | /** |
| 300 | * Generate a backtrace |
| 301 | * |
| 302 | * This lets us show file names and line numbers |
| 303 | * |
| 304 | * @access private |
| 305 | * @return array |
| 306 | */ |
| 307 | function _backtrace() |
| 308 | { |
| 309 | if (function_exists('debug_backtrace')) |
| 310 | { |
| 311 | $back = debug_backtrace(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file']; |
| 314 | $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 315 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | return array('file' => $file, 'line' => $line); |
| 317 | } |
| 318 | return array('file' => 'Unknown', 'line' => 'Unknown'); |
| 319 | } |
| 320 | |
| 321 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | /** |
| 324 | * Get Default Template |
| 325 | * |
| 326 | * @access private |
| 327 | * @return string |
| 328 | */ |
| 329 | function _default_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">'; |
| 332 | $this->_template .= '{rows}'; |
| 333 | $this->_template .= "\n".'</table>'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 334 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | $this->_template_rows = "\n\t".'<tr>'; |
| 336 | $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'; |
| 337 | $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | $this->_template_rows .= "\n\t".'</tr>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 340 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | // -------------------------------------------------------------------- |
| 342 | |
| 343 | /** |
| 344 | * Parse Template |
| 345 | * |
| 346 | * Harvests the data within the template {pseudo-variables} |
| 347 | * |
| 348 | * @access private |
| 349 | * @return void |
| 350 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 351 | function _parse_template() |
| 352 | { |
| 353 | if ( ! is_null($this->_template_rows)) |
| 354 | { |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | if (is_null($this->_template)) |
| 359 | { |
| 360 | $this->_default_template(); |
| 361 | return; |
| 362 | } |
| 363 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match)) |
| 365 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 366 | $this->_default_template(); |
| 367 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | $this->_template_rows = $match['1']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 371 | $this->_template = str_replace($match['0'], '{rows}', $this->_template); |
| 372 | } |
| 373 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | } |
| 375 | // END Unit_test Class |
| 376 | |
| 377 | /** |
| 378 | * Helper functions to test boolean true/false |
| 379 | * |
| 380 | * |
| 381 | * @access private |
| 382 | * @return bool |
| 383 | */ |
| 384 | function is_true($test) |
| 385 | { |
| 386 | return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE; |
| 387 | } |
| 388 | function is_false($test) |
| 389 | { |
| 390 | return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | /* End of file Unit_test.php */ |
Gerry | 33c9c3f | 2011-09-25 00:32:38 +0800 | [diff] [blame] | 395 | /* Location: ./system/libraries/Unit_test.php */ |