blob: 2710b551392cdd79831235b812e0e642cefecfd0 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Unit Testing Class
31 *
32 * Simple testing class
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category UnitTesting
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Gerry33c9c3f2011-09-25 00:32:38 +080038 * @link http://codeigniter.com/user_guide/libraries/unit_testing.html
Derek Allard2067d1a2008-11-13 22:59:24 +000039 */
40class CI_Unit_test {
41
Andrey Andreev597ea272012-11-01 22:56:26 +020042 /**
43 * Active flag
44 *
45 * @var bool
46 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030047 public $active = TRUE;
Andrey Andreev597ea272012-11-01 22:56:26 +020048
49 /**
50 * Test results
51 *
52 * @var array
53 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030054 public $results = array();
Andrey Andreev597ea272012-11-01 22:56:26 +020055
56 /**
57 * Strict comparison flag
58 *
59 * Whether to use === or == when comparing
60 *
61 * @var bool
62 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030063 public $strict = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +020064
65 /**
66 * Template
67 *
68 * @var string
69 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030070 protected $_template = NULL;
Andrey Andreev597ea272012-11-01 22:56:26 +020071
72 /**
73 * Template rows
74 *
75 * @var string
76 */
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030077 protected $_template_rows = NULL;
Andrey Andreev597ea272012-11-01 22:56:26 +020078
79 /**
80 * List of visible test items
81 *
82 * @var array
83 */
Andrey Andreev114586f2011-12-26 16:27:17 +020084 protected $_test_items_visible = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000085
Andrey Andreev597ea272012-11-01 22:56:26 +020086 // --------------------------------------------------------------------
87
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030088 /**
89 * Constructor
90 *
91 * @return void
92 */
Greg Akera9263282010-11-10 15:26:43 -060093 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
Derek Allard0ce73ef2010-01-18 15:48:25 +000095 // These are the default items visible when a test is run.
96 $this->_test_items_visible = array (
97 'test_name',
98 'test_datatype',
99 'res_datatype',
100 'result',
101 'file',
102 'line',
103 'notes'
104 );
105
Andrey Andreev1b815532012-04-03 16:06:03 +0300106 log_message('debug', 'Unit Testing Class Initialized');
Derek Allard0ce73ef2010-01-18 15:48:25 +0000107 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
109 // --------------------------------------------------------------------
Derek Allard0ce73ef2010-01-18 15:48:25 +0000110
111 /**
112 * Run the tests
113 *
114 * Runs the supplied tests
115 *
Derek Allard0ce73ef2010-01-18 15:48:25 +0000116 * @param array
117 * @return void
118 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200119 public function set_test_items($items = array())
Derek Allard0ce73ef2010-01-18 15:48:25 +0000120 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300121 if ( ! empty($items) && is_array($items))
Derek Allard0ce73ef2010-01-18 15:48:25 +0000122 {
123 $this->_test_items_visible = $items;
124 }
125 }
126
127 // --------------------------------------------------------------------
128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 /**
130 * Run the tests
131 *
132 * Runs the supplied tests
133 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300134 * @param mixed $test
Andrey Andreev597ea272012-11-01 22:56:26 +0200135 * @param mixed $expected
136 * @param string $test_name
137 * @param string $notes
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200139 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200140 public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100142 if ($this->active === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 return FALSE;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard82d6ec42009-12-22 13:53:53 +0000147 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 +0000148 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300149 $expected = str_replace('is_double', 'is_float', $expected);
150 $result = $expected($test);
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
152 }
153 else
154 {
Andrey Andreev597ea272012-11-01 22:56:26 +0200155 $result = ($this->strict === TRUE) ? ($test === $expected) : ($test == $expected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 $extype = gettype($expected);
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 $back = $this->_backtrace();
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 $report[] = array (
162 'test_name' => $test_name,
163 'test_datatype' => gettype($test),
164 'res_datatype' => $extype,
165 'result' => ($result === TRUE) ? 'passed' : 'failed',
166 'file' => $back['file'],
Derek Allard0ce73ef2010-01-18 15:48:25 +0000167 'line' => $back['line'],
168 'notes' => $notes
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 );
170
Derek Allard0ce73ef2010-01-18 15:48:25 +0000171 $this->results[] = $report;
172
Andrey Andreevc839d282012-06-07 14:35:27 +0300173 return $this->report($this->result($report));
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 /**
179 * Generate a report
180 *
181 * Displays a table with the test data
182 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200183 * @param array $result
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @return string
185 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200186 public function report($result = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200188 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 $result = $this->result();
191 }
192
193 $CI =& get_instance();
194 $CI->load->language('unit_test');
195
196 $this->_parse_template();
197
198 $r = '';
199 foreach ($result as $res)
200 {
201 $table = '';
202
203 foreach ($res as $key => $val)
204 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100205 if ($key === $CI->lang->line('ut_result'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100207 if ($val === $CI->lang->line('ut_passed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
209 $val = '<span style="color: #0C0;">'.$val.'</span>';
210 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100211 elseif ($val === $CI->lang->line('ut_failed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 $val = '<span style="color: #C00;">'.$val.'</span>';
214 }
215 }
216
Andrey Andreev2a27d312011-12-25 17:08:24 +0200217 $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 $r .= str_replace('{rows}', $table, $this->_template);
221 }
222
223 return $r;
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 /**
229 * Use strict comparison
230 *
231 * Causes the evaluation to use === rather than ==
232 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300234 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200236 public function use_strict($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200238 $this->strict = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 /**
244 * Make Unit testing active
245 *
246 * Enables/disables unit testing
247 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300249 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200251 public function active($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200253 $this->active = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 /**
259 * Result Array
260 *
261 * Returns the raw result data
262 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200263 * @param array $results
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @return array
265 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200266 public function result($results = array())
Barry Mienydd671972010-10-04 16:33:58 +0200267 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 $CI =& get_instance();
269 $CI->load->language('unit_test');
Barry Mienydd671972010-10-04 16:33:58 +0200270
Andrey Andreev2a27d312011-12-25 17:08:24 +0200271 if (count($results) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 $results = $this->results;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 $retval = array();
277 foreach ($results as $result)
278 {
279 $temp = array();
280 foreach ($result as $key => $val)
281 {
Derek Allard0ce73ef2010-01-18 15:48:25 +0000282 if ( ! in_array($key, $this->_test_items_visible))
283 {
284 continue;
285 }
286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if (is_array($val))
288 {
289 foreach ($val as $k => $v)
290 {
Kyle Johnsonc4a3c3c2012-10-03 14:34:37 -0700291 if ( ! in_array($k, $this->_test_items_visible))
292 {
293 continue;
294 }
Andrey Andreev9438e262012-10-05 13:16:27 +0300295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
297 {
298 $v = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200299 }
300 $temp[$CI->lang->line('ut_'.$k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302 }
303 else
304 {
305 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
306 {
307 $val = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200308 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 $temp[$CI->lang->line('ut_'.$key)] = $val;
310 }
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 $retval[] = $temp;
314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 return $retval;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 /**
322 * Set the template
323 *
324 * This lets us set the template to be used to display results
325 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @param string
327 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200328 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200329 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
331 $this->_template = $template;
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 /**
337 * Generate a backtrace
338 *
339 * This lets us show file names and line numbers
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @return array
342 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200343 protected function _backtrace()
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Andrey Andreevc839d282012-06-07 14:35:27 +0300345 $back = debug_backtrace();
346 return array(
347 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
348 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
349 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351
352 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 /**
355 * Get Default Template
356 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * @return string
358 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200359 protected function _default_template()
Barry Mienydd671972010-10-04 16:33:58 +0200360 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300361 $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 +0200362
Andrey Andreev2a27d312011-12-25 17:08:24 +0200363 $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 +0300364 ."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // --------------------------------------------------------------------
368
369 /**
370 * Parse Template
371 *
372 * Harvests the data within the template {pseudo-variables}
373 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * @return void
375 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200376 protected function _parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200377 {
378 if ( ! is_null($this->_template_rows))
379 {
380 return;
381 }
382
Andrey Andreev1b815532012-04-03 16:06:03 +0300383 if (is_null($this->_template) OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
Barry Mienydd671972010-10-04 16:33:58 +0200384 {
385 $this->_default_template();
386 return;
387 }
388
Andrey Andreev2a27d312011-12-25 17:08:24 +0200389 $this->_template_rows = $match[1];
390 $this->_template = str_replace($match[0], '{rows}', $this->_template);
Barry Mienydd671972010-10-04 16:33:58 +0200391 }
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393}
Derek Allard2067d1a2008-11-13 22:59:24 +0000394
395/**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300396 * Helper function to test boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300398 * @param mixed $test
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @return bool
400 */
401function is_true($test)
402{
Andrey Andreev56454792012-05-17 14:32:19 +0300403 return ($test === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000404}
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300405
406/**
407 * Helper function to test boolean FALSE
408 *
409 * @param mixed $test
410 * @return bool
411 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000412function is_false($test)
413{
Andrey Andreev56454792012-05-17 14:32:19 +0300414 return ($test === FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000415}
416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417/* End of file Unit_test.php */
Andrey Andreev1b815532012-04-03 16:06:03 +0300418/* Location: ./system/libraries/Unit_test.php */