Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2005 Aurelien 1
<?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
8
 * @author		ExpressionEngine Dev Team
9
 * @copyright	Copyright (c) 2008, 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
 */
29
class 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
		{
59
			return FALSE;
60
		}
61
62
		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))
63
		{
64
			$expected = str_replace('is_float', 'is_double', $expected);
65
			$result = ($expected($test)) ? TRUE : FALSE;
66
			$extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
67
		}
68
		else
69
		{
70
			if ($this->strict == TRUE)
71
				$result = ($test === $expected) ? TRUE : FALSE;
72
			else
73
				$result = ($test == $expected) ? TRUE : FALSE;
74
75
			$extype = gettype($expected);
76
		}
77
78
		$back = $this->_backtrace();
79
80
		$report[] = array (
81
							'test_name'			=> $test_name,
82
							'test_datatype'		=> gettype($test),
83
							'res_datatype'		=> $extype,
84
							'result'			=> ($result === TRUE) ? 'passed' : 'failed',
85
							'file'				=> $back['file'],
86
							'line'				=> $back['line']
87
						);
88
89
		$this->results[] = $report;
90
91
		return($this->report($this->result($report)));
92
	}
93
94
	// --------------------------------------------------------------------
95
96
	/**
97
	 * Generate a report
98
	 *
99
	 * Displays a table with the test data
100
	 *
101
	 * @access	public
102
	 * @return	string
103
	 */
104
	function report($result = array())
105
	{
106
		if (count($result) == 0)
107
		{
108
			$result = $this->result();
109
		}
110
111
		$CI =& get_instance();
112
		$CI->load->language('unit_test');
113
114
		$this->_parse_template();
115
116
		$r = '';
117
		foreach ($result as $res)
118
		{
119
			$table = '';
120
121
			foreach ($res as $key => $val)
122
			{
123
124
				if ($key == $CI->lang->line('ut_result'))
125
				{
126
					if ($val == $CI->lang->line('ut_passed'))
127
					{
128
						$val = '<span style="color: #0C0;">'.$val.'</span>';
129
					}
130
					elseif ($val == $CI->lang->line('ut_failed'))
131
					{
132
						$val = '<span style="color: #C00;">'.$val.'</span>';
133
					}
134
				}
135
136
				$temp = $this->_template_rows;
137
				$temp = str_replace('{item}', $key, $temp);
138
				$temp = str_replace('{result}', $val, $temp);
139
				$table .= $temp;
140
			}
141
142
			$r .= str_replace('{rows}', $table, $this->_template);
143
		}
144
145
		return $r;
146
	}
147
148
	// --------------------------------------------------------------------
149
150
	/**
151
	 * Use strict comparison
152
	 *
153
	 * Causes the evaluation to use === rather than ==
154
	 *
155
	 * @access	public
156
	 * @param	bool
157
	 * @return	null
158
	 */
159
	function use_strict($state = TRUE)
160
	{
161
		$this->strict = ($state == FALSE) ? FALSE : TRUE;
162
	}
163
164
	// --------------------------------------------------------------------
165
166
	/**
167
	 * Make Unit testing active
168
	 *
169
	 * Enables/disables unit testing
170
	 *
171
	 * @access	public
172
	 * @param	bool
173
	 * @return	null
174
	 */
175
	function active($state = TRUE)
176
	{
177
		$this->active = ($state == FALSE) ? FALSE : TRUE;
178
	}
179
180
	// --------------------------------------------------------------------
181
182
	/**
183
	 * Result Array
184
	 *
185
	 * Returns the raw result data
186
	 *
187
	 * @access	public
188
	 * @return	array
189
	 */
190
	function result($results = array())
191
	{
192
		$CI =& get_instance();
193
		$CI->load->language('unit_test');
194
195
		if (count($results) == 0)
196
		{
197
			$results = $this->results;
198
		}
199
200
		$retval = array();
201
		foreach ($results as $result)
202
		{
203
			$temp = array();
204
			foreach ($result as $key => $val)
205
			{
206
				if (is_array($val))
207
				{
208
					foreach ($val as $k => $v)
209
					{
210
						if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))
211
						{
212
							$v = $line;
213
						}
214
						$temp[$CI->lang->line('ut_'.$k)] = $v;
215
					}
216
				}
217
				else
218
				{
219
					if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))
220
					{
221
						$val = $line;
222
					}
223
					$temp[$CI->lang->line('ut_'.$key)] = $val;
224
				}
225
			}
226
227
			$retval[] = $temp;
228
		}
229
230
		return $retval;
231
	}
232
233
	// --------------------------------------------------------------------
234
235
	/**
236
	 * Set the template
237
	 *
238
	 * This lets us set the template to be used to display results
239
	 *
240
	 * @access	public
241
	 * @param	string
242
	 * @return	void
243
	 */
244
	function set_template($template)
245
	{
246
		$this->_template = $template;
247
	}
248
249
	// --------------------------------------------------------------------
250
251
	/**
252
	 * Generate a backtrace
253
	 *
254
	 * This lets us show file names and line numbers
255
	 *
256
	 * @access	private
257
	 * @return	array
258
	 */
259
	function _backtrace()
260
	{
261
		if (function_exists('debug_backtrace'))
262
		{
263
			$back = debug_backtrace();
264
265
			$file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
266
			$line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
267
268
			return array('file' => $file, 'line' => $line);
269
		}
270
		return array('file' => 'Unknown', 'line' => 'Unknown');
271
	}
272
273
	// --------------------------------------------------------------------
274
275
	/**
276
	 * Get Default Template
277
	 *
278
	 * @access	private
279
	 * @return	string
280
	 */
281
	function _default_template()
282
	{
283
		$this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';
284
		$this->_template .= '{rows}';
285
		$this->_template .= "\n".'</table>';
286
287
		$this->_template_rows = "\n\t".'<tr>';
288
		$this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';
289
		$this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';
290
		$this->_template_rows .= "\n\t".'</tr>';
291
	}
292
293
	// --------------------------------------------------------------------
294
295
	/**
296
	 * Parse Template
297
	 *
298
	 * Harvests the data within the template {pseudo-variables}
299
	 *
300
	 * @access	private
301
	 * @return	void
302
	 */
303
 	function _parse_template()
304
 	{
305
 		if ( ! is_null($this->_template_rows))
306
 		{
307
 			return;
308
 		}
309
310
 		if (is_null($this->_template))
311
 		{
312
 			$this->_default_template();
313
 			return;
314
 		}
315
316
		if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
317
		{
318
 			$this->_default_template();
319
 			return;
320
		}
321
322
		$this->_template_rows = $match['1'];
323
		$this->_template = str_replace($match['0'], '{rows}', $this->_template);
324
 	}
325
326
}
327
// END Unit_test Class
328
329
/**
330
 * Helper functions to test boolean true/false
331
 *
332
 *
333
 * @access	private
334
 * @return	bool
335
 */
336
function is_true($test)
337
{
338
	return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
339
}
340
function is_false($test)
341
{
342
	return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
343
}
344
345
346
/* End of file Unit_test.php */
347
/* Location: ./system/libraries/Unit_test.php */