Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.3.1 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Unit Testing Class |
| 31 | * |
| 32 | * Simple testing class |
| 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category UnitTesting |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Gerry | 33c9c3f | 2011-09-25 00:32:38 +0800 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/libraries/unit_testing.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | */ |
| 40 | class CI_Unit_test { |
| 41 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 42 | public $active = TRUE; |
| 43 | public $results = array(); |
| 44 | public $strict = FALSE; |
| 45 | protected $_template = NULL; |
| 46 | protected $_template_rows = NULL; |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 47 | protected $_test_items_visible = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 49 | /** |
| 50 | * Constructor |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 54 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 56 | // These are the default items visible when a test is run. |
| 57 | $this->_test_items_visible = array ( |
| 58 | 'test_name', |
| 59 | 'test_datatype', |
| 60 | 'res_datatype', |
| 61 | 'result', |
| 62 | 'file', |
| 63 | 'line', |
| 64 | 'notes' |
| 65 | ); |
| 66 | |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 67 | log_message('debug', 'Unit Testing Class Initialized'); |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 68 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | |
| 70 | // -------------------------------------------------------------------- |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Run the tests |
| 74 | * |
| 75 | * Runs the supplied tests |
| 76 | * |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 77 | * @param array |
| 78 | * @return void |
| 79 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 80 | public function set_test_items($items = array()) |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 81 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 82 | if ( ! empty($items) && is_array($items)) |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 83 | { |
| 84 | $this->_test_items_visible = $items; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // -------------------------------------------------------------------- |
| 89 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Run the tests |
| 92 | * |
| 93 | * Runs the supplied tests |
| 94 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 95 | * @param mixed $test |
| 96 | * @param mixed $expected = TRUE |
| 97 | * @param string $test_name = 'undefined' |
| 98 | * @param string $notes = '' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 100 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 101 | public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 103 | if ($this->active === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | { |
| 105 | return FALSE; |
| 106 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | |
Derek Allard | 82d6ec4 | 2009-12-22 13:53:53 +0000 | [diff] [blame] | 108 | 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'), TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 110 | $expected = str_replace('is_double', 'is_float', $expected); |
| 111 | $result = $expected($test); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected)); |
| 113 | } |
| 114 | else |
| 115 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 116 | $result = ($this->strict === TRUE) ? ($test === $expected) : ($test === $expected); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | $extype = gettype($expected); |
| 118 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | $back = $this->_backtrace(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | $report[] = array ( |
| 123 | 'test_name' => $test_name, |
| 124 | 'test_datatype' => gettype($test), |
| 125 | 'res_datatype' => $extype, |
| 126 | 'result' => ($result === TRUE) ? 'passed' : 'failed', |
| 127 | 'file' => $back['file'], |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 128 | 'line' => $back['line'], |
| 129 | 'notes' => $notes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | ); |
| 131 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 132 | $this->results[] = $report; |
| 133 | |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 134 | return $this->report($this->result($report)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Generate a report |
| 141 | * |
| 142 | * Displays a table with the test data |
| 143 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 144 | * @param array $result = array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | * @return string |
| 146 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 147 | public function report($result = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 149 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | { |
| 151 | $result = $this->result(); |
| 152 | } |
| 153 | |
| 154 | $CI =& get_instance(); |
| 155 | $CI->load->language('unit_test'); |
| 156 | |
| 157 | $this->_parse_template(); |
| 158 | |
| 159 | $r = ''; |
| 160 | foreach ($result as $res) |
| 161 | { |
| 162 | $table = ''; |
| 163 | |
| 164 | foreach ($res as $key => $val) |
| 165 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 166 | if ($key === $CI->lang->line('ut_result')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 168 | if ($val === $CI->lang->line('ut_passed')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | { |
| 170 | $val = '<span style="color: #0C0;">'.$val.'</span>'; |
| 171 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 172 | elseif ($val === $CI->lang->line('ut_failed')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | { |
| 174 | $val = '<span style="color: #C00;">'.$val.'</span>'; |
| 175 | } |
| 176 | } |
| 177 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 178 | $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | $r .= str_replace('{rows}', $table, $this->_template); |
| 182 | } |
| 183 | |
| 184 | return $r; |
| 185 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | /** |
| 190 | * Use strict comparison |
| 191 | * |
| 192 | * Causes the evaluation to use === rather than == |
| 193 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | * @param bool |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 195 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 197 | public function use_strict($state = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 199 | $this->strict = (bool) $state; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | /** |
| 205 | * Make Unit testing active |
| 206 | * |
| 207 | * Enables/disables unit testing |
| 208 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | * @param bool |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 210 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 212 | public function active($state = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 214 | $this->active = (bool) $state; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | /** |
| 220 | * Result Array |
| 221 | * |
| 222 | * Returns the raw result data |
| 223 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 224 | * @param array $results = array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | * @return array |
| 226 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 227 | public function result($results = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 228 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | $CI =& get_instance(); |
| 230 | $CI->load->language('unit_test'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 232 | if (count($results) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | { |
| 234 | $results = $this->results; |
| 235 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | $retval = array(); |
| 238 | foreach ($results as $result) |
| 239 | { |
| 240 | $temp = array(); |
| 241 | foreach ($result as $key => $val) |
| 242 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 243 | if ( ! in_array($key, $this->_test_items_visible)) |
| 244 | { |
| 245 | continue; |
| 246 | } |
| 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | if (is_array($val)) |
| 249 | { |
| 250 | foreach ($val as $k => $v) |
| 251 | { |
Kyle Johnson | c4a3c3c | 2012-10-03 14:34:37 -0700 | [diff] [blame] | 252 | if ( ! in_array($k, $this->_test_items_visible)) |
| 253 | { |
| 254 | continue; |
| 255 | } |
Andrey Andreev | 9438e26 | 2012-10-05 13:16:27 +0300 | [diff] [blame] | 256 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v)))) |
| 258 | { |
| 259 | $v = $line; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 260 | } |
| 261 | $temp[$CI->lang->line('ut_'.$k)] = $v; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val)))) |
| 267 | { |
| 268 | $val = $line; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | $temp[$CI->lang->line('ut_'.$key)] = $val; |
| 271 | } |
| 272 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 273 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | $retval[] = $temp; |
| 275 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 276 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | return $retval; |
| 278 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 281 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | /** |
| 283 | * Set the template |
| 284 | * |
| 285 | * This lets us set the template to be used to display results |
| 286 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | * @param string |
| 288 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 290 | public function set_template($template) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | { |
| 292 | $this->_template = $template; |
| 293 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | /** |
| 298 | * Generate a backtrace |
| 299 | * |
| 300 | * This lets us show file names and line numbers |
| 301 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | * @return array |
| 303 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 304 | protected function _backtrace() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | { |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 306 | $back = debug_backtrace(); |
| 307 | return array( |
| 308 | 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''), |
| 309 | 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '') |
| 310 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 314 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | /** |
| 316 | * Get Default Template |
| 317 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | * @return string |
| 319 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 320 | protected function _default_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 321 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 322 | $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">{rows}'."\n</table>"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 324 | $this->_template_rows = "\n\t<tr>\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>' |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 325 | ."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 327 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | // -------------------------------------------------------------------- |
| 329 | |
| 330 | /** |
| 331 | * Parse Template |
| 332 | * |
| 333 | * Harvests the data within the template {pseudo-variables} |
| 334 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | * @return void |
| 336 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 337 | protected function _parse_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | { |
| 339 | if ( ! is_null($this->_template_rows)) |
| 340 | { |
| 341 | return; |
| 342 | } |
| 343 | |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 344 | if (is_null($this->_template) OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 345 | { |
| 346 | $this->_default_template(); |
| 347 | return; |
| 348 | } |
| 349 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 350 | $this->_template_rows = $match[1]; |
| 351 | $this->_template = str_replace($match[0], '{rows}', $this->_template); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | |
| 356 | /** |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 357 | * Helper function to test boolean TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 359 | * @param mixed $test |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | * @return bool |
| 361 | */ |
| 362 | function is_true($test) |
| 363 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 364 | return ($test === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | } |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 366 | |
| 367 | /** |
| 368 | * Helper function to test boolean FALSE |
| 369 | * |
| 370 | * @param mixed $test |
| 371 | * @return bool |
| 372 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | function is_false($test) |
| 374 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 375 | return ($test === FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | /* End of file Unit_test.php */ |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 379 | /* Location: ./system/libraries/Unit_test.php */ |