blob: c2c01758e3e616b82e425dc39f890b3b2366b1f3 [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 {
Alex Bilbied261b1e2012-06-02 11:12:16 +010096 if ($this->active === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
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 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100109 $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
Andrey Andreevc839d282012-06-07 14:35:27 +0300127 return $this->report($this->result($report));
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
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 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100158 if ($key === $CI->lang->line('ut_result'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100160 if ($val === $CI->lang->line('ut_passed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 $val = '<span style="color: #0C0;">'.$val.'</span>';
163 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100164 elseif ($val === $CI->lang->line('ut_failed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
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 {
Kyle Johnsonc4a3c3c2012-10-03 14:34:37 -0700243 if ( ! in_array($k, $this->_test_items_visible))
244 {
245 continue;
246 }
Andrey Andreev9438e262012-10-05 13:16:27 +0300247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
249 {
250 $v = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200251 }
252 $temp[$CI->lang->line('ut_'.$k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254 }
255 else
256 {
257 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
258 {
259 $val = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200260 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 $temp[$CI->lang->line('ut_'.$key)] = $val;
262 }
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $retval[] = $temp;
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 return $retval;
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 /**
274 * Set the template
275 *
276 * This lets us set the template to be used to display results
277 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @param string
279 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200280 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200281 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 $this->_template = $template;
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 /**
289 * Generate a backtrace
290 *
291 * This lets us show file names and line numbers
292 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 * @return array
294 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200295 protected function _backtrace()
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Andrey Andreevc839d282012-06-07 14:35:27 +0300297 $back = debug_backtrace();
298 return array(
299 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
300 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
301 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303
304 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 /**
307 * Get Default Template
308 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @return string
310 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200311 protected function _default_template()
Barry Mienydd671972010-10-04 16:33:58 +0200312 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300313 $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 +0200314
Andrey Andreev2a27d312011-12-25 17:08:24 +0200315 $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 +0300316 ."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
320
321 /**
322 * Parse Template
323 *
324 * Harvests the data within the template {pseudo-variables}
325 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @return void
327 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200328 protected function _parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200329 {
330 if ( ! is_null($this->_template_rows))
331 {
332 return;
333 }
334
Andrey Andreev1b815532012-04-03 16:06:03 +0300335 if (is_null($this->_template) OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
Barry Mienydd671972010-10-04 16:33:58 +0200336 {
337 $this->_default_template();
338 return;
339 }
340
Andrey Andreev2a27d312011-12-25 17:08:24 +0200341 $this->_template_rows = $match[1];
342 $this->_template = str_replace($match[0], '{rows}', $this->_template);
Barry Mienydd671972010-10-04 16:33:58 +0200343 }
344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345}
Derek Allard2067d1a2008-11-13 22:59:24 +0000346
347/**
348 * Helper functions to test boolean true/false
349 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @return bool
351 */
352function is_true($test)
353{
Andrey Andreev56454792012-05-17 14:32:19 +0300354 return ($test === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000355}
356function is_false($test)
357{
Andrey Andreev56454792012-05-17 14:32:19 +0300358 return ($test === FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000359}
360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361/* End of file Unit_test.php */
Andrey Andreev1b815532012-04-03 16:06:03 +0300362/* Location: ./system/libraries/Unit_test.php */