Subversion-Projekte lars-tiefland.ci

Revision

Revision 2242 | Revision 2257 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | 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
 *
2254 lars 9
 * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
68 lars 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/)
2254 lars 32
 * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
68 lars 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
			$result = $expected($test);
158
			$extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
159
		}
160
		else
161
		{
162
			$result = ($this->strict === TRUE) ? ($test === $expected) : ($test == $expected);
163
			$extype = gettype($expected);
164
		}
165
 
166
		$back = $this->_backtrace();
167
 
168
		$report = array (
169
			'test_name'     => $test_name,
170
			'test_datatype' => gettype($test),
171
			'res_datatype'  => $extype,
172
			'result'        => ($result === TRUE) ? 'passed' : 'failed',
173
			'file'          => $back['file'],
174
			'line'          => $back['line'],
175
			'notes'         => $notes
176
		);
177
 
178
		$this->results[] = $report;
179
 
180
		return $this->report($this->result(array($report)));
181
	}
182
 
183
	// --------------------------------------------------------------------
184
 
185
	/**
186
	 * Generate a report
187
	 *
188
	 * Displays a table with the test data
189
	 *
190
	 * @param	array	 $result
191
	 * @return	string
192
	 */
193
	public function report($result = array())
194
	{
195
		if (count($result) === 0)
196
		{
197
			$result = $this->result();
198
		}
199
 
200
		$CI =& get_instance();
201
		$CI->load->language('unit_test');
202
 
203
		$this->_parse_template();
204
 
205
		$r = '';
206
		foreach ($result as $res)
207
		{
208
			$table = '';
209
 
210
			foreach ($res as $key => $val)
211
			{
212
				if ($key === $CI->lang->line('ut_result'))
213
				{
214
					if ($val === $CI->lang->line('ut_passed'))
215
					{
216
						$val = '<span style="color: #0C0;">'.$val.'</span>';
217
					}
218
					elseif ($val === $CI->lang->line('ut_failed'))
219
					{
220
						$val = '<span style="color: #C00;">'.$val.'</span>';
221
					}
222
				}
223
 
224
				$table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
225
			}
226
 
227
			$r .= str_replace('{rows}', $table, $this->_template);
228
		}
229
 
230
		return $r;
231
	}
232
 
233
	// --------------------------------------------------------------------
234
 
235
	/**
236
	 * Use strict comparison
237
	 *
238
	 * Causes the evaluation to use === rather than ==
239
	 *
240
	 * @param	bool	$state
241
	 * @return	void
242
	 */
243
	public function use_strict($state = TRUE)
244
	{
245
		$this->strict = (bool) $state;
246
	}
247
 
248
	// --------------------------------------------------------------------
249
 
250
	/**
251
	 * Make Unit testing active
252
	 *
253
	 * Enables/disables unit testing
254
	 *
255
	 * @param	bool
256
	 * @return	void
257
	 */
258
	public function active($state = TRUE)
259
	{
260
		$this->active = (bool) $state;
261
	}
262
 
263
	// --------------------------------------------------------------------
264
 
265
	/**
266
	 * Result Array
267
	 *
268
	 * Returns the raw result data
269
	 *
270
	 * @param	array	$results
271
	 * @return	array
272
	 */
273
	public function result($results = array())
274
	{
275
		$CI =& get_instance();
276
		$CI->load->language('unit_test');
277
 
278
		if (count($results) === 0)
279
		{
280
			$results = $this->results;
281
		}
282
 
283
		$retval = array();
284
		foreach ($results as $result)
285
		{
286
			$temp = array();
287
			foreach ($result as $key => $val)
288
			{
289
				if ( ! in_array($key, $this->_test_items_visible))
290
				{
291
					continue;
292
				}
1257 lars 293
				elseif (in_array($key, array('test_name', 'test_datatype', 'res_datatype', 'result'), TRUE))
68 lars 294
				{
295
					if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val), FALSE)))
296
					{
297
						$val = $line;
298
					}
299
				}
300
 
301
				$temp[$CI->lang->line('ut_'.$key, FALSE)] = $val;
302
			}
303
 
304
			$retval[] = $temp;
305
		}
306
 
307
		return $retval;
308
	}
309
 
310
	// --------------------------------------------------------------------
311
 
312
	/**
313
	 * Set the template
314
	 *
315
	 * This lets us set the template to be used to display results
316
	 *
317
	 * @param	string
318
	 * @return	void
319
	 */
320
	public function set_template($template)
321
	{
322
		$this->_template = $template;
323
	}
324
 
325
	// --------------------------------------------------------------------
326
 
327
	/**
328
	 * Generate a backtrace
329
	 *
330
	 * This lets us show file names and line numbers
331
	 *
332
	 * @return	array
333
	 */
334
	protected function _backtrace()
335
	{
336
		$back = debug_backtrace();
337
		return array(
338
			'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
339
			'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
340
		);
341
	}
342
 
343
	// --------------------------------------------------------------------
344
 
345
	/**
346
	 * Get Default Template
347
	 *
348
	 * @return	string
349
	 */
350
	protected function _default_template()
351
	{
352
		$this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">{rows}'."\n</table>";
353
 
354
		$this->_template_rows = "\n\t<tr>\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'
355
					."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
356
	}
357
 
358
	// --------------------------------------------------------------------
359
 
360
	/**
361
	 * Parse Template
362
	 *
363
	 * Harvests the data within the template {pseudo-variables}
364
	 *
365
	 * @return	void
366
	 */
367
	protected function _parse_template()
368
	{
369
		if ($this->_template_rows !== NULL)
370
		{
371
			return;
372
		}
373
 
374
		if ($this->_template === NULL OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
375
		{
376
			$this->_default_template();
377
			return;
378
		}
379
 
380
		$this->_template_rows = $match[1];
381
		$this->_template = str_replace($match[0], '{rows}', $this->_template);
382
	}
383
 
384
}
385
 
386
/**
387
 * Helper function to test boolean TRUE
388
 *
389
 * @param	mixed	$test
390
 * @return	bool
391
 */
392
function is_true($test)
393
{
394
	return ($test === TRUE);
395
}
396
 
397
/**
398
 * Helper function to test boolean FALSE
399
 *
400
 * @param	mixed	$test
401
 * @return	bool
402
 */
403
function is_false($test)
404
{
405
	return ($test === FALSE);
406
}