blob: 2fb2bbd320e8590af3bd9dd37a25ec41acb79550 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.3.1
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Unit Testing Class
20 *
21 * Simple testing class
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category UnitTesting
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/libraries/uri.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000028 */
29class CI_Unit_test {
30
31 var $active = TRUE;
32 var $results = array();
33 var $strict = FALSE;
34 var $_template = NULL;
35 var $_template_rows = NULL;
36
37 function CI_Unit_test()
38 {
39 log_message('debug', "Unit Testing Class Initialized");
40 }
41
42 // --------------------------------------------------------------------
43
44 /**
45 * Run the tests
46 *
47 * Runs the supplied tests
48 *
49 * @access public
50 * @param mixed
51 * @param mixed
52 * @param string
53 * @return string
54 */
55 function run($test, $expected = TRUE, $test_name = 'undefined')
56 {
57 if ($this->active == FALSE)
58 return FALSE;
59
60 if (in_array($expected, array('is_string', 'is_bool', 'is_true', 'is_false', 'is_int', 'is_numeric', 'is_float', 'is_double', 'is_array', 'is_null'), TRUE))
61 {
62 $expected = str_replace('is_float', 'is_double', $expected);
63 $result = ($expected($test)) ? TRUE : FALSE;
64 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
65 }
66 else
67 {
68 if ($this->strict == TRUE)
69 $result = ($test === $expected) ? TRUE : FALSE;
70 else
71 $result = ($test == $expected) ? TRUE : FALSE;
72
73 $extype = gettype($expected);
74 }
75
76 $back = $this->_backtrace();
77
78 $report[] = array (
79 'test_name' => $test_name,
80 'test_datatype' => gettype($test),
81 'res_datatype' => $extype,
82 'result' => ($result === TRUE) ? 'passed' : 'failed',
83 'file' => $back['file'],
84 'line' => $back['line']
85 );
86
87 $this->results[] = $report;
88
89 return($this->report($this->result($report)));
90 }
91
92 // --------------------------------------------------------------------
93
94 /**
95 * Generate a report
96 *
97 * Displays a table with the test data
98 *
99 * @access public
100 * @return string
101 */
102 function report($result = array())
Derek Allardf9d53482008-04-26 19:19:25 +0000103 {
Derek Allardd2df9bc2007-04-15 17:41:17 +0000104 if (count($result) == 0)
105 {
106 $result = $this->result();
107 }
Derek Allardf9d53482008-04-26 19:19:25 +0000108
109 $CI =& get_instance();
110 $CI->load->language('unit_test');
111
Derek Allardd2df9bc2007-04-15 17:41:17 +0000112 $this->_parse_template();
Derek Allardf9d53482008-04-26 19:19:25 +0000113
Derek Allardd2df9bc2007-04-15 17:41:17 +0000114 $r = '';
115 foreach ($result as $res)
116 {
117 $table = '';
Derek Allardf9d53482008-04-26 19:19:25 +0000118
Derek Allardd2df9bc2007-04-15 17:41:17 +0000119 foreach ($res as $key => $val)
120 {
Derek Allardf9d53482008-04-26 19:19:25 +0000121
122 if ($key == $CI->lang->line('ut_result'))
123 {
124 if ($val == $CI->lang->line('ut_passed'))
125 {
126 $val = '<span style="color: #0C0;">'.$val.'</span>';
127 }
128 elseif ($val == $CI->lang->line('ut_failed'))
129 {
130 $val = '<span style="color: #C00;">'.$val.'</span>';
131 }
132 }
133
134 $temp = $this->_template_rows;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000135 $temp = str_replace('{item}', $key, $temp);
136 $temp = str_replace('{result}', $val, $temp);
137 $table .= $temp;
138 }
Derek Allardf9d53482008-04-26 19:19:25 +0000139
Derek Allardd2df9bc2007-04-15 17:41:17 +0000140 $r .= str_replace('{rows}', $table, $this->_template);
141 }
Derek Allardf9d53482008-04-26 19:19:25 +0000142
143 return $r;
144 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000145
146 // --------------------------------------------------------------------
147
148 /**
149 * Use strict comparison
150 *
151 * Causes the evaluation to use === rather then ==
152 *
153 * @access public
154 * @param bool
155 * @return null
156 */
157 function use_strict($state = TRUE)
158 {
159 $this->strict = ($state == FALSE) ? FALSE : TRUE;
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Make Unit testing active
166 *
167 * Enables/disables unit testing
168 *
169 * @access public
170 * @param bool
171 * @return null
172 */
173 function active($state = TRUE)
174 {
175 $this->active = ($state == FALSE) ? FALSE : TRUE;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Result Array
182 *
183 * Returns the raw result data
184 *
185 * @access public
186 * @return array
187 */
188 function result($results = array())
189 {
190 $CI =& get_instance();
191 $CI->load->language('unit_test');
192
193 if (count($results) == 0)
194 {
195 $results = $this->results;
196 }
197
198 $retval = array();
199 foreach ($results as $result)
200 {
201 $temp = array();
202 foreach ($result as $key => $val)
203 {
204 if (is_array($val))
205 {
206 foreach ($val as $k => $v)
207 {
208 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
209 {
210 $v = $line;
211 }
212 $temp[$CI->lang->line('ut_'.$k)] = $v;
213 }
214 }
215 else
216 {
217 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
218 {
219 $val = $line;
220 }
221 $temp[$CI->lang->line('ut_'.$key)] = $val;
222 }
223 }
224
225 $retval[] = $temp;
226 }
227
228 return $retval;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Set the template
235 *
236 * This lets us set the template to be used to display results
237 *
238 * @access public
239 * @param string
240 * @return void
241 */
242 function set_template($template)
243 {
244 $this->_template = $template;
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * Generate a backtrace
251 *
252 * This lets us show file names and line numbers
253 *
254 * @access private
255 * @return array
256 */
257 function _backtrace()
258 {
259 if (function_exists('debug_backtrace'))
260 {
261 $back = debug_backtrace();
262
Derek Allard73274992008-05-05 16:39:18 +0000263 $file = (! isset($back['1']['file'])) ? '' : $back['1']['file'];
264 $line = (! isset($back['1']['line'])) ? '' : $back['1']['line'];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000265
266 return array('file' => $file, 'line' => $line);
267 }
268 return array('file' => 'Unknown', 'line' => 'Unknown');
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Get Default Template
275 *
276 * @access private
277 * @return string
278 */
279 function _default_template()
280 {
Derek Allardf9d53482008-04-26 19:19:25 +0000281 $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';
282 $this->_template .= '{rows}';
283 $this->_template .= "\n".'</table>';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000284
Derek Allardf9d53482008-04-26 19:19:25 +0000285 $this->_template_rows = "\n\t".'<tr>';
286 $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';
287 $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';
288 $this->_template_rows .= "\n\t".'</tr>';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Parse Template
295 *
296 * Harvests the data within the template {pseudo-variables}
297 *
298 * @access private
299 * @return void
300 */
301 function _parse_template()
302 {
Derek Allard73274992008-05-05 16:39:18 +0000303 if (! is_null($this->_template_rows))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000304 {
305 return;
306 }
307
308 if (is_null($this->_template))
309 {
310 $this->_default_template();
311 return;
312 }
313
Derek Allard73274992008-05-05 16:39:18 +0000314 if (! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000315 {
316 $this->_default_template();
317 return;
318 }
319
320 $this->_template_rows = $match['1'];
321 $this->_template = str_replace($match['0'], '{rows}', $this->_template);
322 }
323
324}
325// END Unit_test Class
326
327/**
328 * Helper functions to test boolean true/false
329 *
330 *
331 * @access private
332 * @return bool
333 */
334function is_true($test)
335{
336 return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
337}
338function is_false($test)
339{
340 return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
341}
342
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000343
344/* End of file Unit_test.php */
345/* Location: ./system/libraries/Unit_test.php */