blob: 53e4dfac47b747390974c332a86ff5f37897a7fd [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 Andreeva6eae872014-01-03 18:25:20 +020084 protected $_test_items_visible = array(
85 'test_name',
86 'test_datatype',
87 'res_datatype',
88 'result',
89 'file',
90 'line',
91 'notes'
92 );
Derek Allard2067d1a2008-11-13 22:59:24 +000093
Andrey Andreev597ea272012-11-01 22:56:26 +020094 // --------------------------------------------------------------------
95
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030096 /**
97 * Constructor
98 *
99 * @return void
100 */
Greg Akera9263282010-11-10 15:26:43 -0600101 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300103 log_message('debug', 'Unit Testing Class Initialized');
Derek Allard0ce73ef2010-01-18 15:48:25 +0000104 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
106 // --------------------------------------------------------------------
Derek Allard0ce73ef2010-01-18 15:48:25 +0000107
108 /**
109 * Run the tests
110 *
111 * Runs the supplied tests
112 *
Derek Allard0ce73ef2010-01-18 15:48:25 +0000113 * @param array
114 * @return void
115 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200116 public function set_test_items($items = array())
Derek Allard0ce73ef2010-01-18 15:48:25 +0000117 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300118 if ( ! empty($items) && is_array($items))
Derek Allard0ce73ef2010-01-18 15:48:25 +0000119 {
120 $this->_test_items_visible = $items;
121 }
122 }
123
124 // --------------------------------------------------------------------
125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 /**
127 * Run the tests
128 *
129 * Runs the supplied tests
130 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300131 * @param mixed $test
Andrey Andreev597ea272012-11-01 22:56:26 +0200132 * @param mixed $expected
133 * @param string $test_name
134 * @param string $notes
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200136 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200137 public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100139 if ($this->active === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
141 return FALSE;
142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard82d6ec42009-12-22 13:53:53 +0000144 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 +0000145 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300146 $expected = str_replace('is_double', 'is_float', $expected);
147 $result = $expected($test);
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
149 }
150 else
151 {
Andrey Andreev597ea272012-11-01 22:56:26 +0200152 $result = ($this->strict === TRUE) ? ($test === $expected) : ($test == $expected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 $extype = gettype($expected);
154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 $back = $this->_backtrace();
Barry Mienydd671972010-10-04 16:33:58 +0200157
Daniel Paul Searlesd2ae1d12012-11-02 17:39:44 -0700158 $report = array (
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 'test_name' => $test_name,
160 'test_datatype' => gettype($test),
161 'res_datatype' => $extype,
162 'result' => ($result === TRUE) ? 'passed' : 'failed',
163 'file' => $back['file'],
Derek Allard0ce73ef2010-01-18 15:48:25 +0000164 'line' => $back['line'],
165 'notes' => $notes
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 );
167
Derek Allard0ce73ef2010-01-18 15:48:25 +0000168 $this->results[] = $report;
169
Daniel Paul Searlesd2ae1d12012-11-02 17:39:44 -0700170 return $this->report($this->result(array($report)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172
173 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 /**
176 * Generate a report
177 *
178 * Displays a table with the test data
179 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200180 * @param array $result
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 * @return string
182 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200183 public function report($result = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200185 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
187 $result = $this->result();
188 }
189
190 $CI =& get_instance();
191 $CI->load->language('unit_test');
192
193 $this->_parse_template();
194
195 $r = '';
196 foreach ($result as $res)
197 {
198 $table = '';
199
200 foreach ($res as $key => $val)
201 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100202 if ($key === $CI->lang->line('ut_result'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100204 if ($val === $CI->lang->line('ut_passed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 $val = '<span style="color: #0C0;">'.$val.'</span>';
207 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100208 elseif ($val === $CI->lang->line('ut_failed'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 $val = '<span style="color: #C00;">'.$val.'</span>';
211 }
212 }
213
Andrey Andreev2a27d312011-12-25 17:08:24 +0200214 $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216
217 $r .= str_replace('{rows}', $table, $this->_template);
218 }
219
220 return $r;
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 /**
226 * Use strict comparison
227 *
228 * Causes the evaluation to use === rather than ==
229 *
Andrey Andreeva6eae872014-01-03 18:25:20 +0200230 * @param bool $state
Andrey Andreev1b815532012-04-03 16:06:03 +0300231 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200233 public function use_strict($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200235 $this->strict = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 /**
241 * Make Unit testing active
242 *
243 * Enables/disables unit testing
244 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * @param bool
Andrey Andreev1b815532012-04-03 16:06:03 +0300246 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200248 public function active($state = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Andrey Andreev2a27d312011-12-25 17:08:24 +0200250 $this->active = (bool) $state;
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 /**
256 * Result Array
257 *
258 * Returns the raw result data
259 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200260 * @param array $results
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 * @return array
262 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200263 public function result($results = array())
Barry Mienydd671972010-10-04 16:33:58 +0200264 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $CI =& get_instance();
266 $CI->load->language('unit_test');
Barry Mienydd671972010-10-04 16:33:58 +0200267
Andrey Andreev2a27d312011-12-25 17:08:24 +0200268 if (count($results) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
270 $results = $this->results;
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 $retval = array();
274 foreach ($results as $result)
275 {
276 $temp = array();
277 foreach ($result as $key => $val)
278 {
Derek Allard0ce73ef2010-01-18 15:48:25 +0000279 if ( ! in_array($key, $this->_test_items_visible))
280 {
281 continue;
282 }
283
Andrey Andreevce0c9562012-11-22 17:26:29 +0200284 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val), FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Daniel Paul Searlesd2ae1d12012-11-02 17:39:44 -0700286 $val = $line;
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
Andrey Andreeva6eae872014-01-03 18:25:20 +0200288
Andrey Andreevce0c9562012-11-22 17:26:29 +0200289 $temp[$CI->lang->line('ut_'.$key, FALSE)] = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 $retval[] = $temp;
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 return $retval;
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 /**
301 * Set the template
302 *
303 * This lets us set the template to be used to display results
304 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @param string
306 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200307 */
Andrey Andreev2a27d312011-12-25 17:08:24 +0200308 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 $this->_template = $template;
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 /**
316 * Generate a backtrace
317 *
318 * This lets us show file names and line numbers
319 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @return array
321 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200322 protected function _backtrace()
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Andrey Andreevc839d282012-06-07 14:35:27 +0300324 $back = debug_backtrace();
325 return array(
326 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
327 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
328 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330
331 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 /**
334 * Get Default Template
335 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 * @return string
337 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200338 protected function _default_template()
Barry Mienydd671972010-10-04 16:33:58 +0200339 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300340 $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 +0200341
Andrey Andreev2a27d312011-12-25 17:08:24 +0200342 $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 +0300343 ."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
349 * Parse Template
350 *
351 * Harvests the data within the template {pseudo-variables}
352 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * @return void
354 */
Andrey Andreev114586f2011-12-26 16:27:17 +0200355 protected function _parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200356 {
vlakoff1228fe22013-01-14 01:30:09 +0100357 if ($this->_template_rows !== NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200358 {
359 return;
360 }
361
vlakoff1228fe22013-01-14 01:30:09 +0100362 if ($this->_template === NULL OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
Barry Mienydd671972010-10-04 16:33:58 +0200363 {
364 $this->_default_template();
365 return;
366 }
367
Andrey Andreev2a27d312011-12-25 17:08:24 +0200368 $this->_template_rows = $match[1];
369 $this->_template = str_replace($match[0], '{rows}', $this->_template);
Barry Mienydd671972010-10-04 16:33:58 +0200370 }
371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372}
Derek Allard2067d1a2008-11-13 22:59:24 +0000373
374/**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300375 * Helper function to test boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300377 * @param mixed $test
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * @return bool
379 */
380function is_true($test)
381{
Andrey Andreev56454792012-05-17 14:32:19 +0300382 return ($test === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000383}
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300384
385/**
386 * Helper function to test boolean FALSE
387 *
388 * @param mixed $test
389 * @return bool
390 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000391function is_false($test)
392{
Andrey Andreev56454792012-05-17 14:32:19 +0300393 return ($test === FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000394}
395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396/* End of file Unit_test.php */
Andrey Andreeva6eae872014-01-03 18:25:20 +0200397/* Location: ./system/libraries/Unit_test.php */