blob: 6ec2dcd5da7fbe56e1d527c3635f1856a423874a [file] [log] [blame]
Andrey Andreev2a27d312011-12-25 17:08:24 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev2a27d312011-12-25 17:08:24 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev2a27d312011-12-25 17:08:24 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.3.1
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Unit Testing Class
30 *
31 * Simple testing class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category UnitTesting
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Gerry33c9c3f2011-09-25 00:32:38 +080037 * @link http://codeigniter.com/user_guide/libraries/unit_testing.html
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
39class CI_Unit_test {
40
Andrey Andreev2a27d312011-12-25 17:08:24 +020041 public $active = TRUE;
42 public $results = array();
43 public $strict = FALSE;
Andrey Andreev114586f2011-12-26 16:27:17 +020044 protected $_template = NULL;
45 protected $_template_rows = NULL;
46 protected $_test_items_visible = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Greg Akera9263282010-11-10 15:26:43 -060048 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000049 {
Derek Allard0ce73ef2010-01-18 15:48:25 +000050 // These are the default items visible when a test is run.
51 $this->_test_items_visible = array (
52 'test_name',
53 'test_datatype',
54 'res_datatype',
55 'result',
56 'file',
57 'line',
58 'notes'
59 );
60
Andrey Andreev1b815532012-04-03 16:06:03 +030061 log_message('debug', 'Unit Testing Class Initialized');
Derek Allard0ce73ef2010-01-18 15:48:25 +000062 }
Derek Allard2067d1a2008-11-13 22:59:24 +000063
64 // --------------------------------------------------------------------
Derek Allard0ce73ef2010-01-18 15:48:25 +000065
66 /**
67 * Run the tests
68 *
69 * Runs the supplied tests
70 *
Derek Allard0ce73ef2010-01-18 15:48:25 +000071 * @param array
72 * @return void
73 */
Andrey Andreev2a27d312011-12-25 17:08:24 +020074 public function set_test_items($items = array())
Derek Allard0ce73ef2010-01-18 15:48:25 +000075 {
Andrey Andreev1b815532012-04-03 16:06:03 +030076 if ( ! empty($items) && is_array($items))
Derek Allard0ce73ef2010-01-18 15:48:25 +000077 {
78 $this->_test_items_visible = $items;
79 }
80 }
81
82 // --------------------------------------------------------------------
83
Derek Allard2067d1a2008-11-13 22:59:24 +000084 /**
85 * Run the tests
86 *
87 * Runs the supplied tests
88 *
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * @param mixed
90 * @param mixed
91 * @param string
92 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020093 */
Andrey Andreev2a27d312011-12-25 17:08:24 +020094 public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
96 if ($this->active == FALSE)
97 {
98 return FALSE;
99 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Allard82d6ec42009-12-22 13:53:53 +0000101 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 Allard2067d1a2008-11-13 22:59:24 +0000102 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300103 $expected = str_replace('is_double', 'is_float', $expected);
104 $result = $expected($test);
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
106 }
107 else
108 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200109 $result = ($this->strict == TRUE) ? ($test === $expected) : ($test == $expected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 $extype = gettype($expected);
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 $back = $this->_backtrace();
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 $report[] = array (
116 'test_name' => $test_name,
117 'test_datatype' => gettype($test),
118 'res_datatype' => $extype,
119 'result' => ($result === TRUE) ? 'passed' : 'failed',
120 'file' => $back['file'],
Derek Allard0ce73ef2010-01-18 15:48:25 +0000121 'line' => $back['line'],
122 'notes' => $notes
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 );
124
Derek Allard0ce73ef2010-01-18 15:48:25 +0000125 $this->results[] = $report;
126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 return($this->report($this->result($report)));
128 }
129
130 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 /**
133 * Generate a report
134 *
135 * Displays a table with the test data
136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @return string
138 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200139 public function report($result = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200141 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 $result = $this->result();
144 }
145
146 $CI =& get_instance();
147 $CI->load->language('unit_test');
148
149 $this->_parse_template();
150
151 $r = '';
152 foreach ($result as $res)
153 {
154 $table = '';
155
156 foreach ($res as $key => $val)
157 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 if ($key == $CI->lang->line('ut_result'))
159 {
160 if ($val == $CI->lang->line('ut_passed'))
161 {
162 $val = '<span style="color: #0C0;">'.$val.'</span>';
163 }
164 elseif ($val == $CI->lang->line('ut_failed'))
165 {
166 $val = '<span style="color: #C00;">'.$val.'</span>';
167 }
168 }
169
Andrey Andreev2a27d312011-12-25 17:08:24 +0200170 $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172
173 $r .= str_replace('{rows}', $table, $this->_template);
174 }
175
176 return $r;
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 /**
182 * Use strict comparison
183 *
184 * Causes the evaluation to use === rather than ==
185 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300187 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200189 public function use_strict($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200191 $this->strict = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 /**
197 * Make Unit testing active
198 *
199 * Enables/disables unit testing
200 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300202 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200204 public function active($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200206 $this->active = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 /**
212 * Result Array
213 *
214 * Returns the raw result data
215 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 * @return array
217 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200218 public function result($results = array())
Barry Mienydd671972010-10-04 16:33:58 +0200219 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $CI =& get_instance();
221 $CI->load->language('unit_test');
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreev2a27d312011-12-25 17:08:24 +0200223 if (count($results) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 $results = $this->results;
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $retval = array();
229 foreach ($results as $result)
230 {
231 $temp = array();
232 foreach ($result as $key => $val)
233 {
Derek Allard0ce73ef2010-01-18 15:48:25 +0000234 if ( ! in_array($key, $this->_test_items_visible))
235 {
236 continue;
237 }
238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 if (is_array($val))
240 {
241 foreach ($val as $k => $v)
242 {
243 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
244 {
245 $v = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200246 }
247 $temp[$CI->lang->line('ut_'.$k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249 }
250 else
251 {
252 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
253 {
254 $val = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200255 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 $temp[$CI->lang->line('ut_'.$key)] = $val;
257 }
258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 $retval[] = $temp;
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 return $retval;
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 /**
269 * Set the template
270 *
271 * This lets us set the template to be used to display results
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param string
274 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200275 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200276 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 $this->_template = $template;
279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 /**
284 * Generate a backtrace
285 *
286 * This lets us show file names and line numbers
287 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @return array
289 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200290 protected function _backtrace()
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 if (function_exists('debug_backtrace'))
293 {
294 $back = debug_backtrace();
Andrey Andreev2a27d312011-12-25 17:08:24 +0200295 return array(
296 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
297 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
298 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300 return array('file' => 'Unknown', 'line' => 'Unknown');
301 }
302
303 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 /**
306 * Get Default Template
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @return string
309 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200310 protected function _default_template()
Barry Mienydd671972010-10-04 16:33:58 +0200311 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300312 $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">{rows}'."\n</table>";
Barry Mienydd671972010-10-04 16:33:58 +0200313
Andrey Andreev2a27d312011-12-25 17:08:24 +0200314 $this->_template_rows = "\n\t<tr>\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'
Andrey Andreev1b815532012-04-03 16:06:03 +0300315 ."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
319
320 /**
321 * Parse Template
322 *
323 * Harvests the data within the template {pseudo-variables}
324 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @return void
326 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200327 protected function _parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200328 {
329 if ( ! is_null($this->_template_rows))
330 {
331 return;
332 }
333
Andrey Andreev1b815532012-04-03 16:06:03 +0300334 if (is_null($this->_template) OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
Barry Mienydd671972010-10-04 16:33:58 +0200335 {
336 $this->_default_template();
337 return;
338 }
339
Andrey Andreev2a27d312011-12-25 17:08:24 +0200340 $this->_template_rows = $match[1];
341 $this->_template = str_replace($match[0], '{rows}', $this->_template);
Barry Mienydd671972010-10-04 16:33:58 +0200342 }
343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344}
Derek Allard2067d1a2008-11-13 22:59:24 +0000345
346/**
347 * Helper functions to test boolean true/false
348 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @return bool
350 */
351function is_true($test)
352{
Andrey Andreev56454792012-05-17 14:32:19 +0300353 return ($test === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000354}
355function is_false($test)
356{
Andrey Andreev56454792012-05-17 14:32:19 +0300357 return ($test === FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000358}
359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360/* End of file Unit_test.php */
Andrey Andreev1b815532012-04-03 16:06:03 +0300361/* Location: ./system/libraries/Unit_test.php */