]> jfr.im git - z_archive/KronOS.git/blame - system/libraries/Unit_test.php
Fixing filestructure again
[z_archive/KronOS.git] / system / libraries / Unit_test.php
CommitLineData
59c06b17
CS
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://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 ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/uri.html
28 */
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 var $_test_items_visible = array();
37
38 public function __construct()
39 {
40 // These are the default items visible when a test is run.
41 $this->_test_items_visible = array (
42 'test_name',
43 'test_datatype',
44 'res_datatype',
45 'result',
46 'file',
47 'line',
48 'notes'
49 );
50
51 log_message('debug', "Unit Testing Class Initialized");
52 }
53
54 // --------------------------------------------------------------------
55
56 /**
57 * Run the tests
58 *
59 * Runs the supplied tests
60 *
61 * @access public
62 * @param array
63 * @return void
64 */
65 function set_test_items($items = array())
66 {
67 if ( ! empty($items) AND is_array($items))
68 {
69 $this->_test_items_visible = $items;
70 }
71 }
72
73 // --------------------------------------------------------------------
74
75 /**
76 * Run the tests
77 *
78 * Runs the supplied tests
79 *
80 * @access public
81 * @param mixed
82 * @param mixed
83 * @param string
84 * @return string
85 */
86 function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
87 {
88 if ($this->active == FALSE)
89 {
90 return FALSE;
91 }
92
93 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))
94 {
95 $expected = str_replace('is_float', 'is_double', $expected);
96 $result = ($expected($test)) ? TRUE : FALSE;
97 $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
98 }
99 else
100 {
101 if ($this->strict == TRUE)
102 $result = ($test === $expected) ? TRUE : FALSE;
103 else
104 $result = ($test == $expected) ? TRUE : FALSE;
105
106 $extype = gettype($expected);
107 }
108
109 $back = $this->_backtrace();
110
111 $report[] = array (
112 'test_name' => $test_name,
113 'test_datatype' => gettype($test),
114 'res_datatype' => $extype,
115 'result' => ($result === TRUE) ? 'passed' : 'failed',
116 'file' => $back['file'],
117 'line' => $back['line'],
118 'notes' => $notes
119 );
120
121 $this->results[] = $report;
122
123 return($this->report($this->result($report)));
124 }
125
126 // --------------------------------------------------------------------
127
128 /**
129 * Generate a report
130 *
131 * Displays a table with the test data
132 *
133 * @access public
134 * @return string
135 */
136 function report($result = array())
137 {
138 if (count($result) == 0)
139 {
140 $result = $this->result();
141 }
142
143 $CI =& get_instance();
144 $CI->load->language('unit_test');
145
146 $this->_parse_template();
147
148 $r = '';
149 foreach ($result as $res)
150 {
151 $table = '';
152
153 foreach ($res as $key => $val)
154 {
155 if ($key == $CI->lang->line('ut_result'))
156 {
157 if ($val == $CI->lang->line('ut_passed'))
158 {
159 $val = '<span style="color: #0C0;">'.$val.'</span>';
160 }
161 elseif ($val == $CI->lang->line('ut_failed'))
162 {
163 $val = '<span style="color: #C00;">'.$val.'</span>';
164 }
165 }
166
167 $temp = $this->_template_rows;
168 $temp = str_replace('{item}', $key, $temp);
169 $temp = str_replace('{result}', $val, $temp);
170 $table .= $temp;
171 }
172
173 $r .= str_replace('{rows}', $table, $this->_template);
174 }
175
176 return $r;
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Use strict comparison
183 *
184 * Causes the evaluation to use === rather than ==
185 *
186 * @access public
187 * @param bool
188 * @return null
189 */
190 function use_strict($state = TRUE)
191 {
192 $this->strict = ($state == FALSE) ? FALSE : TRUE;
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Make Unit testing active
199 *
200 * Enables/disables unit testing
201 *
202 * @access public
203 * @param bool
204 * @return null
205 */
206 function active($state = TRUE)
207 {
208 $this->active = ($state == FALSE) ? FALSE : TRUE;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Result Array
215 *
216 * Returns the raw result data
217 *
218 * @access public
219 * @return array
220 */
221 function result($results = array())
222 {
223 $CI =& get_instance();
224 $CI->load->language('unit_test');
225
226 if (count($results) == 0)
227 {
228 $results = $this->results;
229 }
230
231 $retval = array();
232 foreach ($results as $result)
233 {
234 $temp = array();
235 foreach ($result as $key => $val)
236 {
237 if ( ! in_array($key, $this->_test_items_visible))
238 {
239 continue;
240 }
241
242 if (is_array($val))
243 {
244 foreach ($val as $k => $v)
245 {
246 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
247 {
248 $v = $line;
249 }
250 $temp[$CI->lang->line('ut_'.$k)] = $v;
251 }
252 }
253 else
254 {
255 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
256 {
257 $val = $line;
258 }
259 $temp[$CI->lang->line('ut_'.$key)] = $val;
260 }
261 }
262
263 $retval[] = $temp;
264 }
265
266 return $retval;
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Set the template
273 *
274 * This lets us set the template to be used to display results
275 *
276 * @access public
277 * @param string
278 * @return void
279 */
280 function set_template($template)
281 {
282 $this->_template = $template;
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Generate a backtrace
289 *
290 * This lets us show file names and line numbers
291 *
292 * @access private
293 * @return array
294 */
295 function _backtrace()
296 {
297 if (function_exists('debug_backtrace'))
298 {
299 $back = debug_backtrace();
300
301 $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
302 $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
303
304 return array('file' => $file, 'line' => $line);
305 }
306 return array('file' => 'Unknown', 'line' => 'Unknown');
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Get Default Template
313 *
314 * @access private
315 * @return string
316 */
317 function _default_template()
318 {
319 $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';
320 $this->_template .= '{rows}';
321 $this->_template .= "\n".'</table>';
322
323 $this->_template_rows = "\n\t".'<tr>';
324 $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';
325 $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';
326 $this->_template_rows .= "\n\t".'</tr>';
327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Parse Template
333 *
334 * Harvests the data within the template {pseudo-variables}
335 *
336 * @access private
337 * @return void
338 */
339 function _parse_template()
340 {
341 if ( ! is_null($this->_template_rows))
342 {
343 return;
344 }
345
346 if (is_null($this->_template))
347 {
348 $this->_default_template();
349 return;
350 }
351
352 if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
353 {
354 $this->_default_template();
355 return;
356 }
357
358 $this->_template_rows = $match['1'];
359 $this->_template = str_replace($match['0'], '{rows}', $this->_template);
360 }
361
362}
363// END Unit_test Class
364
365/**
366 * Helper functions to test boolean true/false
367 *
368 *
369 * @access private
370 * @return bool
371 */
372function is_true($test)
373{
374 return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
375}
376function is_false($test)
377{
378 return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
379}
380
381
382/* End of file Unit_test.php */
383/* Location: ./system/libraries/Unit_test.php */