blob: 5c0018d3562f03c68dfc2bc97928a652c7d3f786 [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 Andreev5fd3ae82012-10-24 14:55:35 +030042 public $active = TRUE;
43 public $results = array();
44 public $strict = FALSE;
45 protected $_template = NULL;
46 protected $_template_rows = NULL;
Andrey Andreev114586f2011-12-26 16:27:17 +020047 protected $_test_items_visible = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030049 /**
50 * Constructor
51 *
52 * @return void
53 */
Greg Akera9263282010-11-10 15:26:43 -060054 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Derek Allard0ce73ef2010-01-18 15:48:25 +000056 // These are the default items visible when a test is run.
57 $this->_test_items_visible = array (
58 'test_name',
59 'test_datatype',
60 'res_datatype',
61 'result',
62 'file',
63 'line',
64 'notes'
65 );
66
Andrey Andreev1b815532012-04-03 16:06:03 +030067 log_message('debug', 'Unit Testing Class Initialized');
Derek Allard0ce73ef2010-01-18 15:48:25 +000068 }
Derek Allard2067d1a2008-11-13 22:59:24 +000069
70 // --------------------------------------------------------------------
Derek Allard0ce73ef2010-01-18 15:48:25 +000071
72 /**
73 * Run the tests
74 *
75 * Runs the supplied tests
76 *
Derek Allard0ce73ef2010-01-18 15:48:25 +000077 * @param array
78 * @return void
79 */
Andrey Andreev2a27d312011-12-25 17:08:24 +020080 public function set_test_items($items = array())
Derek Allard0ce73ef2010-01-18 15:48:25 +000081 {
Andrey Andreev1b815532012-04-03 16:06:03 +030082 if ( ! empty($items) && is_array($items))
Derek Allard0ce73ef2010-01-18 15:48:25 +000083 {
84 $this->_test_items_visible = $items;
85 }
86 }
87
88 // --------------------------------------------------------------------
89
Derek Allard2067d1a2008-11-13 22:59:24 +000090 /**
91 * Run the tests
92 *
93 * Runs the supplied tests
94 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030095 * @param mixed $test
96 * @param mixed $expected = TRUE
97 * @param string $test_name = 'undefined'
98 * @param string $notes = ''
Derek Allard2067d1a2008-11-13 22:59:24 +000099 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200100 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200101 public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100103 if ($this->active === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
105 return FALSE;
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard82d6ec42009-12-22 13:53:53 +0000108 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 +0000109 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300110 $expected = str_replace('is_double', 'is_float', $expected);
111 $result = $expected($test);
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
113 }
114 else
115 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100116 $result = ($this->strict === TRUE) ? ($test === $expected) : ($test === $expected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 $extype = gettype($expected);
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 $back = $this->_backtrace();
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 $report[] = array (
123 'test_name' => $test_name,
124 'test_datatype' => gettype($test),
125 'res_datatype' => $extype,
126 'result' => ($result === TRUE) ? 'passed' : 'failed',
127 'file' => $back['file'],
Derek Allard0ce73ef2010-01-18 15:48:25 +0000128 'line' => $back['line'],
129 'notes' => $notes
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 );
131
Derek Allard0ce73ef2010-01-18 15:48:25 +0000132 $this->results[] = $report;
133
Andrey Andreevc839d282012-06-07 14:35:27 +0300134 return $this->report($this->result($report));
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
136
137 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 /**
140 * Generate a report
141 *
142 * Displays a table with the test data
143 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300144 * @param array $result = array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 * @return string
146 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200147 public function report($result = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200149 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
151 $result = $this->result();
152 }
153
154 $CI =& get_instance();
155 $CI->load->language('unit_test');
156
157 $this->_parse_template();
158
159 $r = '';
160 foreach ($result as $res)
161 {
162 $table = '';
163
164 foreach ($res as $key => $val)
165 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100166 if ($key === $CI->lang->line('ut_result'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100168 if ($val === $CI->lang->line('ut_passed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
170 $val = '<span style="color: #0C0;">'.$val.'</span>';
171 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100172 elseif ($val === $CI->lang->line('ut_failed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
174 $val = '<span style="color: #C00;">'.$val.'</span>';
175 }
176 }
177
Andrey Andreev2a27d312011-12-25 17:08:24 +0200178 $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180
181 $r .= str_replace('{rows}', $table, $this->_template);
182 }
183
184 return $r;
185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 /**
190 * Use strict comparison
191 *
192 * Causes the evaluation to use === rather than ==
193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300195 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200197 public function use_strict($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200199 $this->strict = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 /**
205 * Make Unit testing active
206 *
207 * Enables/disables unit testing
208 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300210 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200212 public function active($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200214 $this->active = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 /**
220 * Result Array
221 *
222 * Returns the raw result data
223 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300224 * @param array $results = array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 * @return array
226 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200227 public function result($results = array())
Barry Mienydd671972010-10-04 16:33:58 +0200228 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $CI =& get_instance();
230 $CI->load->language('unit_test');
Barry Mienydd671972010-10-04 16:33:58 +0200231
Andrey Andreev2a27d312011-12-25 17:08:24 +0200232 if (count($results) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 $results = $this->results;
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 $retval = array();
238 foreach ($results as $result)
239 {
240 $temp = array();
241 foreach ($result as $key => $val)
242 {
Derek Allard0ce73ef2010-01-18 15:48:25 +0000243 if ( ! in_array($key, $this->_test_items_visible))
244 {
245 continue;
246 }
247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 if (is_array($val))
249 {
250 foreach ($val as $k => $v)
251 {
Kyle Johnsonc4a3c3c2012-10-03 14:34:37 -0700252 if ( ! in_array($k, $this->_test_items_visible))
253 {
254 continue;
255 }
Andrey Andreev9438e262012-10-05 13:16:27 +0300256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
258 {
259 $v = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200260 }
261 $temp[$CI->lang->line('ut_'.$k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263 }
264 else
265 {
266 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
267 {
268 $val = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200269 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 $temp[$CI->lang->line('ut_'.$key)] = $val;
271 }
272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 $retval[] = $temp;
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 return $retval;
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 /**
283 * Set the template
284 *
285 * This lets us set the template to be used to display results
286 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 * @param string
288 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200289 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200290 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 $this->_template = $template;
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 /**
298 * Generate a backtrace
299 *
300 * This lets us show file names and line numbers
301 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * @return array
303 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200304 protected function _backtrace()
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Andrey Andreevc839d282012-06-07 14:35:27 +0300306 $back = debug_backtrace();
307 return array(
308 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
309 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
310 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312
313 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 /**
316 * Get Default Template
317 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @return string
319 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200320 protected function _default_template()
Barry Mienydd671972010-10-04 16:33:58 +0200321 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300322 $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 +0200323
Andrey Andreev2a27d312011-12-25 17:08:24 +0200324 $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 +0300325 ."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // --------------------------------------------------------------------
329
330 /**
331 * Parse Template
332 *
333 * Harvests the data within the template {pseudo-variables}
334 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 * @return void
336 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200337 protected function _parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200338 {
339 if ( ! is_null($this->_template_rows))
340 {
341 return;
342 }
343
Andrey Andreev1b815532012-04-03 16:06:03 +0300344 if (is_null($this->_template) OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
Barry Mienydd671972010-10-04 16:33:58 +0200345 {
346 $this->_default_template();
347 return;
348 }
349
Andrey Andreev2a27d312011-12-25 17:08:24 +0200350 $this->_template_rows = $match[1];
351 $this->_template = str_replace($match[0], '{rows}', $this->_template);
Barry Mienydd671972010-10-04 16:33:58 +0200352 }
353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354}
Derek Allard2067d1a2008-11-13 22:59:24 +0000355
356/**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300357 * Helper function to test boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300359 * @param mixed $test
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * @return bool
361 */
362function is_true($test)
363{
Andrey Andreev56454792012-05-17 14:32:19 +0300364 return ($test === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000365}
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300366
367/**
368 * Helper function to test boolean FALSE
369 *
370 * @param mixed $test
371 * @return bool
372 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000373function is_false($test)
374{
Andrey Andreev56454792012-05-17 14:32:19 +0300375 return ($test === FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000376}
377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378/* End of file Unit_test.php */
Andrey Andreev1b815532012-04-03 16:06:03 +0300379/* Location: ./system/libraries/Unit_test.php */