blob: 6573f42691c582670c6b160782fd957860c6d1fa [file] [log] [blame]
admin5dc80422006-10-01 18:53:35 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @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
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/libraries/uri.html
28 */
29class CI_Unit {
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()
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;
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())
103 {
104 if (count($result) == 0)
105 {
106 $result = $this->result();
107 }
108
109 $this->_parse_template();
110
111 $r = '';
112 foreach ($result as $res)
113 {
114 $table = '';
115
116 foreach ($res as $key => $val)
117 {
118 $temp = $this->_template_rows;
119 $temp = str_replace('{item}', $key, $temp);
120 $temp = str_replace('{result}', $val, $temp);
121 $table .= $temp;
122 }
123
124 $r .= str_replace('{rows}', $table, $this->_template);
125 }
126
127 return $r;
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Use strict comparison
134 *
135 * Causes the evaluation to use === rather then ==
136 *
137 * @access public
138 * @param bool
139 * @return null
140 */
141 function use_strict($state = TRUE)
142 {
143 $this->strict = ($state == FALSE) ? FALSE : TRUE;
144 }
145
146 // --------------------------------------------------------------------
147
148 /**
149 * Make Unit testing active
150 *
151 * Enables/disables unit testing
152 *
153 * @access public
154 * @param bool
155 * @return null
156 */
157 function active($state = TRUE)
158 {
159 $this->active = ($state == FALSE) ? FALSE : TRUE;
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Result Array
166 *
167 * Returns the raw result data
168 *
169 * @access public
170 * @return array
171 */
172 function result($results = array())
173 {
admin88a8ad12006-10-07 03:16:32 +0000174 $CI =& get_instance();
175 $CI->load->language('unit_test');
admin5dc80422006-10-01 18:53:35 +0000176
177 if (count($results) == 0)
178 {
179 $results = $this->results;
180 }
181
182 $retval = array();
183 foreach ($results as $result)
184 {
185 $temp = array();
186 foreach ($result as $key => $val)
187 {
188 if (is_array($val))
189 {
190 foreach ($val as $k => $v)
191 {
admin88a8ad12006-10-07 03:16:32 +0000192 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
admin5dc80422006-10-01 18:53:35 +0000193 {
194 $v = $line;
195 }
admin88a8ad12006-10-07 03:16:32 +0000196 $temp[$CI->lang->line('ut_'.$k)] = $v;
admin5dc80422006-10-01 18:53:35 +0000197 }
198 }
199 else
200 {
admin88a8ad12006-10-07 03:16:32 +0000201 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
admin5dc80422006-10-01 18:53:35 +0000202 {
203 $val = $line;
204 }
admin88a8ad12006-10-07 03:16:32 +0000205 $temp[$CI->lang->line('ut_'.$key)] = $val;
admin5dc80422006-10-01 18:53:35 +0000206 }
207 }
208
209 $retval[] = $temp;
210 }
211
212 return $retval;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Set the template
219 *
220 * This lets us set the template to be used to display results
221 *
222 * @access public
223 * @params string
224 * @return void
225 */
226 function set_template($template)
227 {
228 $this->_template = $template;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Generate a backtrace
235 *
236 * This lets us show file names and line numbers
237 *
238 * @access private
239 * @return array
240 */
241 function _backtrace()
242 {
243 if (function_exists('debug_backtrace'))
244 {
245 $back = debug_backtrace();
246
247 $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
248 $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
249
250 return array('file' => $file, 'line' => $line);
251 }
252 return array('file' => 'Unknown', 'line' => 'Unknown');
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Get Default Template
259 *
260 * @access private
261 * @return string
262 */
263 function _default_template()
264 {
265 $this->_template = '
266 <div style="margin:15px;background-color:#ccc;">
267 <table border="0" cellpadding="4" cellspacing="1" style="width:100%;">
268 {rows}
269 </table></div>';
270
271 $this->_template_rows = '
272 <tr>
273 <td style="background-color:#fff;width:140px;font-size:12px;font-weight:bold;">{item}</td>
274 <td style="background-color:#fff;font-size:12px;">{result}</td>
275 </tr>
276 ';
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Parse Template
283 *
284 * Harvests the data within the template {pseudo-variables}
285 *
286 * @access private
287 * @return void
288 */
289 function _parse_template()
290 {
291 if ( ! is_null($this->_template_rows))
292 {
293 return;
294 }
295
296 if (is_null($this->_template))
297 {
298 $this->_default_template();
299 return;
300 }
301
302 if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
303 {
304 $this->_default_template();
305 return;
306 }
307
308 $this->_template_rows = $match['1'];
309 $this->_template = str_replace($match['0'], '{rows}', $this->_template);
310 }
311
312}
313// END Unit_test Class
314
315/**
316 * Helper functions to test boolean true/false
317 *
318 *
319 * @access private
320 * @return bool
321 */
322function is_true($test)
323{
324 return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
325}
326function is_false($test)
327{
328 return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
329}
330
331?>