summaryrefslogtreecommitdiffstats
path: root/system/libraries/Unit_test.php
diff options
context:
space:
mode:
authorGreg Aker <greg@gregaker.net>2011-12-27 08:38:36 +0100
committerGreg Aker <greg@gregaker.net>2011-12-27 08:38:36 +0100
commit51ba4387d7ca788e4bbfd6c2ac8208d8c2167f8c (patch)
tree01e1b38642522c8089ba4b8194e2281a4f290722 /system/libraries/Unit_test.php
parentc99d9fbf17f5ee5ddd1765d7fe7a19524ca177c8 (diff)
parent9c0e2346e06e71d4f2c467f986dc39ff30137643 (diff)
Merge pull request #830 from narfbg/develop-unit-test
Improve the Unit test library
Diffstat (limited to 'system/libraries/Unit_test.php')
-rw-r--r--system/libraries/Unit_test.php102
1 files changed, 36 insertions, 66 deletions
diff --git a/system/libraries/Unit_test.php b/system/libraries/Unit_test.php
index 7afe40b09..fd3880d29 100644
--- a/system/libraries/Unit_test.php
+++ b/system/libraries/Unit_test.php
@@ -1,13 +1,13 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* NOTICE OF LICENSE
- *
+ *
* Licensed under the Open Software License version 3.0
- *
+ *
* This source file is subject to the Open Software License (OSL 3.0) that is
* bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
@@ -40,12 +40,12 @@
*/
class CI_Unit_test {
- var $active = TRUE;
- var $results = array();
- var $strict = FALSE;
- var $_template = NULL;
- var $_template_rows = NULL;
- var $_test_items_visible = array();
+ public $active = TRUE;
+ public $results = array();
+ public $strict = FALSE;
+ protected $_template = NULL;
+ protected $_template_rows = NULL;
+ protected $_test_items_visible = array();
public function __construct()
{
@@ -70,11 +70,10 @@ class CI_Unit_test {
*
* Runs the supplied tests
*
- * @access public
* @param array
* @return void
*/
- function set_test_items($items = array())
+ public function set_test_items($items = array())
{
if ( ! empty($items) AND is_array($items))
{
@@ -89,13 +88,12 @@ class CI_Unit_test {
*
* Runs the supplied tests
*
- * @access public
* @param mixed
* @param mixed
* @param string
* @return string
*/
- function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
+ public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
{
if ($this->active == FALSE)
{
@@ -110,11 +108,7 @@ class CI_Unit_test {
}
else
{
- if ($this->strict == TRUE)
- $result = ($test === $expected) ? TRUE : FALSE;
- else
- $result = ($test == $expected) ? TRUE : FALSE;
-
+ $result = ($this->strict == TRUE) ? ($test === $expected) : ($test == $expected);
$extype = gettype($expected);
}
@@ -142,12 +136,11 @@ class CI_Unit_test {
*
* Displays a table with the test data
*
- * @access public
* @return string
*/
- function report($result = array())
+ public function report($result = array())
{
- if (count($result) == 0)
+ if (count($result) === 0)
{
$result = $this->result();
}
@@ -176,10 +169,7 @@ class CI_Unit_test {
}
}
- $temp = $this->_template_rows;
- $temp = str_replace('{item}', $key, $temp);
- $temp = str_replace('{result}', $val, $temp);
- $table .= $temp;
+ $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
}
$r .= str_replace('{rows}', $table, $this->_template);
@@ -195,13 +185,12 @@ class CI_Unit_test {
*
* Causes the evaluation to use === rather than ==
*
- * @access public
* @param bool
* @return null
*/
- function use_strict($state = TRUE)
+ public function use_strict($state = TRUE)
{
- $this->strict = ($state == FALSE) ? FALSE : TRUE;
+ $this->strict = (bool) $state;
}
// --------------------------------------------------------------------
@@ -211,13 +200,12 @@ class CI_Unit_test {
*
* Enables/disables unit testing
*
- * @access public
* @param bool
* @return null
*/
- function active($state = TRUE)
+ public function active($state = TRUE)
{
- $this->active = ($state == FALSE) ? FALSE : TRUE;
+ $this->active = (bool) $state;
}
// --------------------------------------------------------------------
@@ -227,15 +215,14 @@ class CI_Unit_test {
*
* Returns the raw result data
*
- * @access public
* @return array
*/
- function result($results = array())
+ public function result($results = array())
{
$CI =& get_instance();
$CI->load->language('unit_test');
- if (count($results) == 0)
+ if (count($results) === 0)
{
$results = $this->results;
}
@@ -285,11 +272,10 @@ class CI_Unit_test {
*
* This lets us set the template to be used to display results
*
- * @access public
* @param string
* @return void
*/
- function set_template($template)
+ public function set_template($template)
{
$this->_template = $template;
}
@@ -301,19 +287,17 @@ class CI_Unit_test {
*
* This lets us show file names and line numbers
*
- * @access private
* @return array
*/
- function _backtrace()
+ protected function _backtrace()
{
if (function_exists('debug_backtrace'))
{
$back = debug_backtrace();
-
- $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
- $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
-
- return array('file' => $file, 'line' => $line);
+ return array(
+ 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
+ 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
+ );
}
return array('file' => 'Unknown', 'line' => 'Unknown');
}
@@ -323,19 +307,14 @@ class CI_Unit_test {
/**
* Get Default Template
*
- * @access private
* @return string
*/
- function _default_template()
+ protected function _default_template()
{
- $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';
- $this->_template .= '{rows}';
- $this->_template .= "\n".'</table>';
-
- $this->_template_rows = "\n\t".'<tr>';
- $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';
- $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';
- $this->_template_rows .= "\n\t".'</tr>';
+ $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">{rows}'."\n".'</table>';
+
+ $this->_template_rows = "\n\t<tr>\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'
+ . "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
}
// --------------------------------------------------------------------
@@ -345,30 +324,23 @@ class CI_Unit_test {
*
* Harvests the data within the template {pseudo-variables}
*
- * @access private
* @return void
*/
- function _parse_template()
+ protected function _parse_template()
{
if ( ! is_null($this->_template_rows))
{
return;
}
- if (is_null($this->_template))
+ if (is_null($this->_template) OR ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
{
$this->_default_template();
return;
}
- if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
- {
- $this->_default_template();
- return;
- }
-
- $this->_template_rows = $match['1'];
- $this->_template = str_replace($match['0'], '{rows}', $this->_template);
+ $this->_template_rows = $match[1];
+ $this->_template = str_replace($match[0], '{rows}', $this->_template);
}
}
@@ -377,8 +349,6 @@ class CI_Unit_test {
/**
* Helper functions to test boolean true/false
*
- *
- * @access private
* @return bool
*/
function is_true($test)