blob: fd3880d296cb03f913863ade1202d7bc4f00f4a2 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
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 Allard2067d1a2008-11-13 22:59:24 +000023 * @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 Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Gerry33c9c3f2011-09-25 00:32:38 +080039 * @link http://codeigniter.com/user_guide/libraries/unit_testing.html
Derek Allard2067d1a2008-11-13 22:59:24 +000040 */
41class CI_Unit_test {
42
Andrey Andreev2a27d312011-12-25 17:08:24 +020043 public $active = TRUE;
44 public $results = array();
45 public $strict = FALSE;
Andrey Andreev114586f2011-12-26 16:27:17 +020046 protected $_template = NULL;
47 protected $_template_rows = NULL;
48 protected $_test_items_visible = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000049
Greg Akera9263282010-11-10 15:26:43 -060050 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000051 {
Derek Allard0ce73ef2010-01-18 15:48:25 +000052 // 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 Allard2067d1a2008-11-13 22:59:24 +000063 log_message('debug', "Unit Testing Class Initialized");
Derek Allard0ce73ef2010-01-18 15:48:25 +000064 }
Derek Allard2067d1a2008-11-13 22:59:24 +000065
66 // --------------------------------------------------------------------
Derek Allard0ce73ef2010-01-18 15:48:25 +000067
68 /**
69 * Run the tests
70 *
71 * Runs the supplied tests
72 *
Derek Allard0ce73ef2010-01-18 15:48:25 +000073 * @param array
74 * @return void
75 */
Andrey Andreev2a27d312011-12-25 17:08:24 +020076 public function set_test_items($items = array())
Derek Allard0ce73ef2010-01-18 15:48:25 +000077 {
78 if ( ! empty($items) AND is_array($items))
79 {
80 $this->_test_items_visible = $items;
81 }
82 }
83
84 // --------------------------------------------------------------------
85
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /**
87 * Run the tests
88 *
89 * Runs the supplied tests
90 *
Derek Allard2067d1a2008-11-13 22:59:24 +000091 * @param mixed
92 * @param mixed
93 * @param string
94 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020095 */
Andrey Andreev2a27d312011-12-25 17:08:24 +020096 public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 if ($this->active == FALSE)
99 {
100 return FALSE;
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard82d6ec42009-12-22 13:53:53 +0000103 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 +0000104 {
105 $expected = str_replace('is_float', 'is_double', $expected);
Barry Mienydd671972010-10-04 16:33:58 +0200106 $result = ($expected($test)) ? TRUE : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
108 }
109 else
110 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200111 $result = ($this->strict == TRUE) ? ($test === $expected) : ($test == $expected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 $extype = gettype($expected);
113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 $back = $this->_backtrace();
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 $report[] = array (
118 'test_name' => $test_name,
119 'test_datatype' => gettype($test),
120 'res_datatype' => $extype,
121 'result' => ($result === TRUE) ? 'passed' : 'failed',
122 'file' => $back['file'],
Derek Allard0ce73ef2010-01-18 15:48:25 +0000123 'line' => $back['line'],
124 'notes' => $notes
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 );
126
Derek Allard0ce73ef2010-01-18 15:48:25 +0000127 $this->results[] = $report;
128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 return($this->report($this->result($report)));
130 }
131
132 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 /**
135 * Generate a report
136 *
137 * Displays a table with the test data
138 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @return string
140 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200141 public function report($result = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200143 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 $result = $this->result();
146 }
147
148 $CI =& get_instance();
149 $CI->load->language('unit_test');
150
151 $this->_parse_template();
152
153 $r = '';
154 foreach ($result as $res)
155 {
156 $table = '';
157
158 foreach ($res as $key => $val)
159 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 if ($key == $CI->lang->line('ut_result'))
161 {
162 if ($val == $CI->lang->line('ut_passed'))
163 {
164 $val = '<span style="color: #0C0;">'.$val.'</span>';
165 }
166 elseif ($val == $CI->lang->line('ut_failed'))
167 {
168 $val = '<span style="color: #C00;">'.$val.'</span>';
169 }
170 }
171
Andrey Andreev2a27d312011-12-25 17:08:24 +0200172 $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174
175 $r .= str_replace('{rows}', $table, $this->_template);
176 }
177
178 return $r;
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 /**
184 * Use strict comparison
185 *
186 * Causes the evaluation to use === rather than ==
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @param bool
189 * @return null
190 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200191 public function use_strict($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200193 $this->strict = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 /**
199 * Make Unit testing active
200 *
201 * Enables/disables unit testing
202 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * @param bool
204 * @return null
205 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200206 public function active($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200208 $this->active = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 /**
214 * Result Array
215 *
216 * Returns the raw result data
217 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * @return array
219 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200220 public function result($results = array())
Barry Mienydd671972010-10-04 16:33:58 +0200221 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $CI =& get_instance();
223 $CI->load->language('unit_test');
Barry Mienydd671972010-10-04 16:33:58 +0200224
Andrey Andreev2a27d312011-12-25 17:08:24 +0200225 if (count($results) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 $results = $this->results;
228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $retval = array();
231 foreach ($results as $result)
232 {
233 $temp = array();
234 foreach ($result as $key => $val)
235 {
Derek Allard0ce73ef2010-01-18 15:48:25 +0000236 if ( ! in_array($key, $this->_test_items_visible))
237 {
238 continue;
239 }
240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 if (is_array($val))
242 {
243 foreach ($val as $k => $v)
244 {
245 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
246 {
247 $v = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200248 }
249 $temp[$CI->lang->line('ut_'.$k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251 }
252 else
253 {
254 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
255 {
256 $val = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200257 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 $temp[$CI->lang->line('ut_'.$key)] = $val;
259 }
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 $retval[] = $temp;
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 return $retval;
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 /**
271 * Set the template
272 *
273 * This lets us set the template to be used to display results
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string
276 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200277 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200278 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $this->_template = $template;
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 /**
286 * Generate a backtrace
287 *
288 * This lets us show file names and line numbers
289 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * @return array
291 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200292 protected function _backtrace()
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 if (function_exists('debug_backtrace'))
295 {
296 $back = debug_backtrace();
Andrey Andreev2a27d312011-12-25 17:08:24 +0200297 return array(
298 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
299 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
300 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302 return array('file' => 'Unknown', 'line' => 'Unknown');
303 }
304
305 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 /**
308 * Get Default Template
309 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * @return string
311 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200312 protected function _default_template()
Barry Mienydd671972010-10-04 16:33:58 +0200313 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200314 $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 +0200315
Andrey Andreev2a27d312011-12-25 17:08:24 +0200316 $this->_template_rows = "\n\t<tr>\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'
317 . "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // --------------------------------------------------------------------
321
322 /**
323 * Parse Template
324 *
325 * Harvests the data within the template {pseudo-variables}
326 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 * @return void
328 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200329 protected function _parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200330 {
331 if ( ! is_null($this->_template_rows))
332 {
333 return;
334 }
335
Andrey Andreev2a27d312011-12-25 17:08:24 +0200336 if (is_null($this->_template) OR ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
Barry Mienydd671972010-10-04 16:33:58 +0200337 {
338 $this->_default_template();
339 return;
340 }
341
Andrey Andreev2a27d312011-12-25 17:08:24 +0200342 $this->_template_rows = $match[1];
343 $this->_template = str_replace($match[0], '{rows}', $this->_template);
Barry Mienydd671972010-10-04 16:33:58 +0200344 }
345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346}
347// END Unit_test Class
348
349/**
350 * Helper functions to test boolean true/false
351 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 * @return bool
353 */
354function is_true($test)
355{
356 return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
357}
358function is_false($test)
359{
360 return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
361}
362
363
364/* End of file Unit_test.php */
Gerry33c9c3f2011-09-25 00:32:38 +0800365/* Location: ./system/libraries/Unit_test.php */