Skip to content

Commit 2a27d31

Browse files
committed
Improve the Unit test library
1 parent a96ade3 commit 2a27d31

File tree

1 file changed

+36
-54
lines changed

1 file changed

+36
-54
lines changed

system/libraries/Unit_test.php

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
22
/**
33
* CodeIgniter
44
*
55
* An open source application development framework for PHP 5.1.6 or newer
66
*
77
* NOTICE OF LICENSE
8-
*
8+
*
99
* Licensed under the Open Software License version 3.0
10-
*
10+
*
1111
* This source file is subject to the Open Software License (OSL 3.0) that is
1212
* bundled with this package in the files license.txt / license.rst. It is
1313
* also available through the world wide web at this URL:
@@ -40,12 +40,12 @@
4040
*/
4141
class CI_Unit_test {
4242

43-
var $active = TRUE;
44-
var $results = array();
45-
var $strict = FALSE;
46-
var $_template = NULL;
47-
var $_template_rows = NULL;
48-
var $_test_items_visible = array();
43+
public $active = TRUE;
44+
public $results = array();
45+
public $strict = FALSE;
46+
private $_template = NULL;
47+
private $_template_rows = NULL;
48+
private $_test_items_visible = array();
4949

5050
public function __construct()
5151
{
@@ -74,7 +74,7 @@ public function __construct()
7474
* @param array
7575
* @return void
7676
*/
77-
function set_test_items($items = array())
77+
public function set_test_items($items = array())
7878
{
7979
if ( ! empty($items) AND is_array($items))
8080
{
@@ -95,7 +95,7 @@ function set_test_items($items = array())
9595
* @param string
9696
* @return string
9797
*/
98-
function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
98+
public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
9999
{
100100
if ($this->active == FALSE)
101101
{
@@ -110,11 +110,7 @@ function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
110110
}
111111
else
112112
{
113-
if ($this->strict == TRUE)
114-
$result = ($test === $expected) ? TRUE : FALSE;
115-
else
116-
$result = ($test == $expected) ? TRUE : FALSE;
117-
113+
$result = ($this->strict == TRUE) ? ($test === $expected) : ($test == $expected);
118114
$extype = gettype($expected);
119115
}
120116

@@ -145,9 +141,9 @@ function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
145141
* @access public
146142
* @return string
147143
*/
148-
function report($result = array())
144+
public function report($result = array())
149145
{
150-
if (count($result) == 0)
146+
if (count($result) === 0)
151147
{
152148
$result = $this->result();
153149
}
@@ -176,10 +172,7 @@ function report($result = array())
176172
}
177173
}
178174

179-
$temp = $this->_template_rows;
180-
$temp = str_replace('{item}', $key, $temp);
181-
$temp = str_replace('{result}', $val, $temp);
182-
$table .= $temp;
175+
$table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
183176
}
184177

185178
$r .= str_replace('{rows}', $table, $this->_template);
@@ -199,9 +192,9 @@ function report($result = array())
199192
* @param bool
200193
* @return null
201194
*/
202-
function use_strict($state = TRUE)
195+
public function use_strict($state = TRUE)
203196
{
204-
$this->strict = ($state == FALSE) ? FALSE : TRUE;
197+
$this->strict = (bool) $state;
205198
}
206199

207200
// --------------------------------------------------------------------
@@ -215,9 +208,9 @@ function use_strict($state = TRUE)
215208
* @param bool
216209
* @return null
217210
*/
218-
function active($state = TRUE)
211+
public function active($state = TRUE)
219212
{
220-
$this->active = ($state == FALSE) ? FALSE : TRUE;
213+
$this->active = (bool) $state;
221214
}
222215

223216
// --------------------------------------------------------------------
@@ -230,12 +223,12 @@ function active($state = TRUE)
230223
* @access public
231224
* @return array
232225
*/
233-
function result($results = array())
226+
public function result($results = array())
234227
{
235228
$CI =& get_instance();
236229
$CI->load->language('unit_test');
237230

238-
if (count($results) == 0)
231+
if (count($results) === 0)
239232
{
240233
$results = $this->results;
241234
}
@@ -289,7 +282,7 @@ function result($results = array())
289282
* @param string
290283
* @return void
291284
*/
292-
function set_template($template)
285+
public function set_template($template)
293286
{
294287
$this->_template = $template;
295288
}
@@ -304,16 +297,15 @@ function set_template($template)
304297
* @access private
305298
* @return array
306299
*/
307-
function _backtrace()
300+
private function _backtrace()
308301
{
309302
if (function_exists('debug_backtrace'))
310303
{
311304
$back = debug_backtrace();
312-
313-
$file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
314-
$line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
315-
316-
return array('file' => $file, 'line' => $line);
305+
return array(
306+
'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
307+
'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
308+
);
317309
}
318310
return array('file' => 'Unknown', 'line' => 'Unknown');
319311
}
@@ -326,16 +318,12 @@ function _backtrace()
326318
* @access private
327319
* @return string
328320
*/
329-
function _default_template()
321+
private function _default_template()
330322
{
331-
$this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';
332-
$this->_template .= '{rows}';
333-
$this->_template .= "\n".'</table>';
334-
335-
$this->_template_rows = "\n\t".'<tr>';
336-
$this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';
337-
$this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';
338-
$this->_template_rows .= "\n\t".'</tr>';
323+
$this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">{rows}'."\n".'</table>';
324+
325+
$this->_template_rows = "\n\t<tr>\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'
326+
. "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
339327
}
340328

341329
// --------------------------------------------------------------------
@@ -348,27 +336,21 @@ function _default_template()
348336
* @access private
349337
* @return void
350338
*/
351-
function _parse_template()
339+
private function _parse_template()
352340
{
353341
if ( ! is_null($this->_template_rows))
354342
{
355343
return;
356344
}
357345

358-
if (is_null($this->_template))
359-
{
360-
$this->_default_template();
361-
return;
362-
}
363-
364-
if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
346+
if (is_null($this->_template) OR ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
365347
{
366348
$this->_default_template();
367349
return;
368350
}
369351

370-
$this->_template_rows = $match['1'];
371-
$this->_template = str_replace($match['0'], '{rows}', $this->_template);
352+
$this->_template_rows = $match[1];
353+
$this->_template = str_replace($match[0], '{rows}', $this->_template);
372354
}
373355

374356
}

0 commit comments

Comments
 (0)