Subversion-Projekte lars-tiefland.ci

Revision

Revision 1257 | Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
9
 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32
 * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 1.3.1
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * Unit Testing Class
42
 *
43
 * Simple testing class
44
 *
45
 * @package		CodeIgniter
46
 * @subpackage	Libraries
47
 * @category	UnitTesting
48
 * @author		EllisLab Dev Team
49
 * @link		https://codeigniter.com/user_guide/libraries/unit_testing.html
50
 */
51
class CI_Unit_test {
52
 
53
	/**
54
	 * Active flag
55
	 *
56
	 * @var	bool
57
	 */
58
	public $active = TRUE;
59
 
60
	/**
61
	 * Test results
62
	 *
63
	 * @var	array
64
	 */
65
	public $results = array();
66
 
67
	/**
68
	 * Strict comparison flag
69
	 *
70
	 * Whether to use === or == when comparing
71
	 *
72
	 * @var	bool
73
	 */
74
	public $strict = FALSE;
75
 
76
	/**
77
	 * Template
78
	 *
79
	 * @var	string
80
	 */
81
	protected $_template = NULL;
82
 
83
	/**
84
	 * Template rows
85
	 *
86
	 * @var	string
87
	 */
88
	protected $_template_rows = NULL;
89
 
90
	/**
91
	 * List of visible test items
92
	 *
93
	 * @var	array
94
	 */
95
	protected $_test_items_visible	= array(
96
		'test_name',
97
		'test_datatype',
98
		'res_datatype',
99
		'result',
100
		'file',
101
		'line',
102
		'notes'
103
	);
104
 
105
	// --------------------------------------------------------------------
106
 
107
	/**
108
	 * Constructor
109
	 *
110
	 * @return	void
111
	 */
112
	public function __construct()
113
	{
114
		log_message('info', 'Unit Testing Class Initialized');
115
	}
116
 
117
	// --------------------------------------------------------------------
118
 
119
	/**
120
	 * Run the tests
121
	 *
122
	 * Runs the supplied tests
123
	 *
124
	 * @param	array	$items
125
	 * @return	void
126
	 */
127
	public function set_test_items($items)
128
	{
129
		if ( ! empty($items) && is_array($items))
130
		{
131
			$this->_test_items_visible = $items;
132
		}
133
	}
134
 
135
	// --------------------------------------------------------------------
136
 
137
	/**
138
	 * Run the tests
139
	 *
140
	 * Runs the supplied tests
141
	 *
142
	 * @param	mixed	$test
143
	 * @param	mixed	$expected
144
	 * @param	string	$test_name
145
	 * @param	string	$notes
146
	 * @return	string
147
	 */
148
	public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
149
	{
150
		if ($this->active === FALSE)
151
		{
152
			return FALSE;
153
		}
154
 
155
		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', 'is_resource'), TRUE))
156
		{
157
			$expected = str_replace('is_double', 'is_float', $expected);
158
			$result = $expected($test);
159
			$extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
160
		}
161
		else
162
		{
163
			$result = ($this->strict === TRUE) ? ($test === $expected) : ($test == $expected);
164
			$extype = gettype($expected);
165
		}
166
 
167
		$back = $this->_backtrace();
168
 
169
		$report = array (
170
			'test_name'     => $test_name,
171
			'test_datatype' => gettype($test),
172
			'res_datatype'  => $extype,
173
			'result'        => ($result === TRUE) ? 'passed' : 'failed',
174
			'file'          => $back['file'],
175
			'line'          => $back['line'],
176
			'notes'         => $notes
177
		);
178
 
179
		$this->results[] = $report;
180
 
181
		return $this->report($this->result(array($report)));
182
	}
183
 
184
	// --------------------------------------------------------------------
185
 
186
	/**
187
	 * Generate a report
188
	 *
189
	 * Displays a table with the test data
190
	 *
191
	 * @param	array	 $result
192
	 * @return	string
193
	 */
194
	public function report($result = array())
195
	{
196
		if (count($result) === 0)
197
		{
198
			$result = $this->result();
199
		}
200
 
201
		$CI =& get_instance();
202
		$CI->load->language('unit_test');
203
 
204
		$this->_parse_template();
205
 
206
		$r = '';
207
		foreach ($result as $res)
208
		{
209
			$table = '';
210
 
211
			foreach ($res as $key => $val)
212
			{
213
				if ($key === $CI->lang->line('ut_result'))
214
				{
215
					if ($val === $CI->lang->line('ut_passed'))
216
					{
217
						$val = '<span style="color: #0C0;">'.$val.'</span>';
218
					}
219
					elseif ($val === $CI->lang->line('ut_failed'))
220
					{
221
						$val = '<span style="color: #C00;">'.$val.'</span>';
222
					}
223
				}
224
 
225
				$table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
226
			}
227
 
228
			$r .= str_replace('{rows}', $table, $this->_template);
229
		}
230
 
231
		return $r;
232
	}
233
 
234
	// --------------------------------------------------------------------
235
 
236
	/**
237
	 * Use strict comparison
238
	 *
239
	 * Causes the evaluation to use === rather than ==
240
	 *
241
	 * @param	bool	$state
242
	 * @return	void
243
	 */
244
	public function use_strict($state = TRUE)
245
	{
246
		$this->strict = (bool) $state;
247
	}
248
 
249
	// --------------------------------------------------------------------
250
 
251
	/**
252
	 * Make Unit testing active
253
	 *
254
	 * Enables/disables unit testing
255
	 *
256
	 * @param	bool
257
	 * @return	void
258
	 */
259
	public function active($state = TRUE)
260
	{
261
		$this->active = (bool) $state;
262
	}
263
 
264
	// --------------------------------------------------------------------
265
 
266
	/**
267
	 * Result Array
268
	 *
269
	 * Returns the raw result data
270
	 *
271
	 * @param	array	$results
272
	 * @return	array
273
	 */
274
	public function result($results = array())
275
	{
276
		$CI =& get_instance();
277
		$CI->load->language('unit_test');
278
 
279
		if (count($results) === 0)
280
		{
281
			$results = $this->results;
282
		}
283
 
284
		$retval = array();
285
		foreach ($results as $result)
286
		{
287
			$temp = array();
288
			foreach ($result as $key => $val)
289
			{
290
				if ( ! in_array($key, $this->_test_items_visible))
291
				{
292
					continue;
293
				}
294
				elseif (in_array($key, array('test_name', 'test_datatype', 'test_res_datatype', 'result'), TRUE))
295
				{
296
					if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val), FALSE)))
297
					{
298
						$val = $line;
299
					}
300
				}
301
 
302
				$temp[$CI->lang->line('ut_'.$key, FALSE)] = $val;
303
			}
304
 
305
			$retval[] = $temp;
306
		}
307
 
308
		return $retval;
309
	}
310
 
311
	// --------------------------------------------------------------------
312
 
313
	/**
314
	 * Set the template
315
	 *
316
	 * This lets us set the template to be used to display results
317
	 *
318
	 * @param	string
319
	 * @return	void
320
	 */
321
	public function set_template($template)
322
	{
323
		$this->_template = $template;
324
	}
325
 
326
	// --------------------------------------------------------------------
327
 
328
	/**
329
	 * Generate a backtrace
330
	 *
331
	 * This lets us show file names and line numbers
332
	 *
333
	 * @return	array
334
	 */
335
	protected function _backtrace()
336
	{
337
		$back = debug_backtrace();
338
		return array(
339
			'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
340
			'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
341
		);
342
	}
343
 
344
	// --------------------------------------------------------------------
345
 
346
	/**
347
	 * Get Default Template
348
	 *
349
	 * @return	string
350
	 */
351
	protected function _default_template()
352
	{
353
		$this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">{rows}'."\n</table>";
354
 
355
		$this->_template_rows = "\n\t<tr>\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'
356
					."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
357
	}
358
 
359
	// --------------------------------------------------------------------
360
 
361
	/**
362
	 * Parse Template
363
	 *
364
	 * Harvests the data within the template {pseudo-variables}
365
	 *
366
	 * @return	void
367
	 */
368
	protected function _parse_template()
369
	{
370
		if ($this->_template_rows !== NULL)
371
		{
372
			return;
373
		}
374
 
375
		if ($this->_template === NULL OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
376
		{
377
			$this->_default_template();
378
			return;
379
		}
380
 
381
		$this->_template_rows = $match[1];
382
		$this->_template = str_replace($match[0], '{rows}', $this->_template);
383
	}
384
 
385
}
386
 
387
/**
388
 * Helper function to test boolean TRUE
389
 *
390
 * @param	mixed	$test
391
 * @return	bool
392
 */
393
function is_true($test)
394
{
395
	return ($test === TRUE);
396
}
397
 
398
/**
399
 * Helper function to test boolean FALSE
400
 *
401
 * @param	mixed	$test
402
 * @return	bool
403
 */
404
function is_false($test)
405
{
406
	return ($test === FALSE);
407
}