blob: 7b99dee0de3912f784f6394b1e938d2d758801f1 [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 Andreev5fd3ae82012-10-24 14:55:35 +030041 public $active = TRUE;
42 public $results = array();
43 public $strict = FALSE;
44 protected $_template = NULL;
45 protected $_template_rows = NULL;
Andrey Andreev114586f2011-12-26 16:27:17 +020046 protected $_test_items_visible = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030048 /**
49 * Constructor
50 *
51 * @return void
52 */
Greg Akera9263282010-11-10 15:26:43 -060053 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
Derek Allard0ce73ef2010-01-18 15:48:25 +000055 // These are the default items visible when a test is run.
56 $this->_test_items_visible = array (
57 'test_name',
58 'test_datatype',
59 'res_datatype',
60 'result',
61 'file',
62 'line',
63 'notes'
64 );
65
Andrey Andreev1b815532012-04-03 16:06:03 +030066 log_message('debug', 'Unit Testing Class Initialized');
Derek Allard0ce73ef2010-01-18 15:48:25 +000067 }
Derek Allard2067d1a2008-11-13 22:59:24 +000068
69 // --------------------------------------------------------------------
Derek Allard0ce73ef2010-01-18 15:48:25 +000070
71 /**
72 * Run the tests
73 *
74 * Runs the supplied tests
75 *
Derek Allard0ce73ef2010-01-18 15:48:25 +000076 * @param array
77 * @return void
78 */
Andrey Andreev2a27d312011-12-25 17:08:24 +020079 public function set_test_items($items = array())
Derek Allard0ce73ef2010-01-18 15:48:25 +000080 {
Andrey Andreev1b815532012-04-03 16:06:03 +030081 if ( ! empty($items) && is_array($items))
Derek Allard0ce73ef2010-01-18 15:48:25 +000082 {
83 $this->_test_items_visible = $items;
84 }
85 }
86
87 // --------------------------------------------------------------------
88
Derek Allard2067d1a2008-11-13 22:59:24 +000089 /**
90 * Run the tests
91 *
92 * Runs the supplied tests
93 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030094 * @param mixed $test
95 * @param mixed $expected = TRUE
96 * @param string $test_name = 'undefined'
97 * @param string $notes = ''
Derek Allard2067d1a2008-11-13 22:59:24 +000098 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200100 public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100102 if ($this->active === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
104 return FALSE;
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Allard82d6ec42009-12-22 13:53:53 +0000107 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 +0000108 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300109 $expected = str_replace('is_double', 'is_float', $expected);
110 $result = $expected($test);
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
112 }
113 else
114 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100115 $result = ($this->strict === TRUE) ? ($test === $expected) : ($test === $expected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 $extype = gettype($expected);
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 $back = $this->_backtrace();
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 $report[] = array (
122 'test_name' => $test_name,
123 'test_datatype' => gettype($test),
124 'res_datatype' => $extype,
125 'result' => ($result === TRUE) ? 'passed' : 'failed',
126 'file' => $back['file'],
Derek Allard0ce73ef2010-01-18 15:48:25 +0000127 'line' => $back['line'],
128 'notes' => $notes
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 );
130
Derek Allard0ce73ef2010-01-18 15:48:25 +0000131 $this->results[] = $report;
132
Andrey Andreevc839d282012-06-07 14:35:27 +0300133 return $this->report($this->result($report));
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
135
136 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 /**
139 * Generate a report
140 *
141 * Displays a table with the test data
142 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300143 * @param array $result = array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @return string
145 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200146 public function report($result = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200148 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
150 $result = $this->result();
151 }
152
153 $CI =& get_instance();
154 $CI->load->language('unit_test');
155
156 $this->_parse_template();
157
158 $r = '';
159 foreach ($result as $res)
160 {
161 $table = '';
162
163 foreach ($res as $key => $val)
164 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100165 if ($key === $CI->lang->line('ut_result'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100167 if ($val === $CI->lang->line('ut_passed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 $val = '<span style="color: #0C0;">'.$val.'</span>';
170 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100171 elseif ($val === $CI->lang->line('ut_failed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
173 $val = '<span style="color: #C00;">'.$val.'</span>';
174 }
175 }
176
Andrey Andreev2a27d312011-12-25 17:08:24 +0200177 $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
179
180 $r .= str_replace('{rows}', $table, $this->_template);
181 }
182
183 return $r;
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 /**
189 * Use strict comparison
190 *
191 * Causes the evaluation to use === rather than ==
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300194 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200196 public function use_strict($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200198 $this->strict = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 /**
204 * Make Unit testing active
205 *
206 * Enables/disables unit testing
207 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300209 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200211 public function active($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200213 $this->active = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 /**
219 * Result Array
220 *
221 * Returns the raw result data
222 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300223 * @param array $results = array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * @return array
225 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200226 public function result($results = array())
Barry Mienydd671972010-10-04 16:33:58 +0200227 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $CI =& get_instance();
229 $CI->load->language('unit_test');
Barry Mienydd671972010-10-04 16:33:58 +0200230
Andrey Andreev2a27d312011-12-25 17:08:24 +0200231 if (count($results) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
233 $results = $this->results;
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 $retval = array();
237 foreach ($results as $result)
238 {
239 $temp = array();
240 foreach ($result as $key => $val)
241 {
Derek Allard0ce73ef2010-01-18 15:48:25 +0000242 if ( ! in_array($key, $this->_test_items_visible))
243 {
244 continue;
245 }
246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 if (is_array($val))
248 {
249 foreach ($val as $k => $v)
250 {
Kyle Johnsonc4a3c3c2012-10-03 14:34:37 -0700251 if ( ! in_array($k, $this->_test_items_visible))
252 {
253 continue;
254 }
Andrey Andreev9438e262012-10-05 13:16:27 +0300255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
257 {
258 $v = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200259 }
260 $temp[$CI->lang->line('ut_'.$k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
262 }
263 else
264 {
265 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
266 {
267 $val = $line;
Barry Mienydd671972010-10-04 16:33:58 +0200268 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 $temp[$CI->lang->line('ut_'.$key)] = $val;
270 }
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 $retval[] = $temp;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 return $retval;
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 /**
282 * Set the template
283 *
284 * This lets us set the template to be used to display results
285 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @param string
287 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200288 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200289 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 $this->_template = $template;
292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 /**
297 * Generate a backtrace
298 *
299 * This lets us show file names and line numbers
300 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 * @return array
302 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200303 protected function _backtrace()
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Andrey Andreevc839d282012-06-07 14:35:27 +0300305 $back = debug_backtrace();
306 return array(
307 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
308 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
309 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311
312 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 /**
315 * Get Default Template
316 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @return string
318 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200319 protected function _default_template()
Barry Mienydd671972010-10-04 16:33:58 +0200320 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300321 $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 +0200322
Andrey Andreev2a27d312011-12-25 17:08:24 +0200323 $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 +0300324 ."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
328
329 /**
330 * Parse Template
331 *
332 * Harvests the data within the template {pseudo-variables}
333 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @return void
335 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200336 protected function _parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200337 {
338 if ( ! is_null($this->_template_rows))
339 {
340 return;
341 }
342
Andrey Andreev1b815532012-04-03 16:06:03 +0300343 if (is_null($this->_template) OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
Barry Mienydd671972010-10-04 16:33:58 +0200344 {
345 $this->_default_template();
346 return;
347 }
348
Andrey Andreev2a27d312011-12-25 17:08:24 +0200349 $this->_template_rows = $match[1];
350 $this->_template = str_replace($match[0], '{rows}', $this->_template);
Barry Mienydd671972010-10-04 16:33:58 +0200351 }
352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353}
Derek Allard2067d1a2008-11-13 22:59:24 +0000354
355/**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300356 * Helper function to test boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300358 * @param mixed $test
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @return bool
360 */
361function is_true($test)
362{
Andrey Andreev56454792012-05-17 14:32:19 +0300363 return ($test === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000364}
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300365
366/**
367 * Helper function to test boolean FALSE
368 *
369 * @param mixed $test
370 * @return bool
371 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000372function is_false($test)
373{
Andrey Andreev56454792012-05-17 14:32:19 +0300374 return ($test === FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375}
376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377/* End of file Unit_test.php */
Andrey Andreev1b815532012-04-03 16:06:03 +0300378/* Location: ./system/libraries/Unit_test.php */