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 | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 42 | /** |
| 43 | * Active flag |
| 44 | * |
| 45 | * @var bool |
| 46 | */ |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 47 | public $active = TRUE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 48 | |
| 49 | /** |
| 50 | * Test results |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 54 | public $results = array(); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 55 | |
| 56 | /** |
| 57 | * Strict comparison flag |
| 58 | * |
| 59 | * Whether to use === or == when comparing |
| 60 | * |
| 61 | * @var bool |
| 62 | */ |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 63 | public $strict = FALSE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 64 | |
| 65 | /** |
| 66 | * Template |
| 67 | * |
| 68 | * @var string |
| 69 | */ |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 70 | protected $_template = NULL; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 71 | |
| 72 | /** |
| 73 | * Template rows |
| 74 | * |
| 75 | * @var string |
| 76 | */ |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 77 | protected $_template_rows = NULL; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 78 | |
| 79 | /** |
| 80 | * List of visible test items |
| 81 | * |
| 82 | * @var array |
| 83 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 84 | protected $_test_items_visible = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 86 | // -------------------------------------------------------------------- |
| 87 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 88 | /** |
| 89 | * Constructor |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 93 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 95 | // These are the default items visible when a test is run. |
| 96 | $this->_test_items_visible = array ( |
| 97 | 'test_name', |
| 98 | 'test_datatype', |
| 99 | 'res_datatype', |
| 100 | 'result', |
| 101 | 'file', |
| 102 | 'line', |
| 103 | 'notes' |
| 104 | ); |
| 105 | |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 106 | log_message('debug', 'Unit Testing Class Initialized'); |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 107 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | |
| 109 | // -------------------------------------------------------------------- |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Run the tests |
| 113 | * |
| 114 | * Runs the supplied tests |
| 115 | * |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 116 | * @param array |
| 117 | * @return void |
| 118 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 119 | public function set_test_items($items = array()) |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 120 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 121 | if ( ! empty($items) && is_array($items)) |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 122 | { |
| 123 | $this->_test_items_visible = $items; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // -------------------------------------------------------------------- |
| 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | /** |
| 130 | * Run the tests |
| 131 | * |
| 132 | * Runs the supplied tests |
| 133 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 134 | * @param mixed $test |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 135 | * @param mixed $expected |
| 136 | * @param string $test_name |
| 137 | * @param string $notes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 140 | public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 142 | if ($this->active === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | { |
| 144 | return FALSE; |
| 145 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Derek Allard | 82d6ec4 | 2009-12-22 13:53:53 +0000 | [diff] [blame] | 147 | 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] | 148 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 149 | $expected = str_replace('is_double', 'is_float', $expected); |
| 150 | $result = $expected($test); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected)); |
| 152 | } |
| 153 | else |
| 154 | { |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 155 | $result = ($this->strict === TRUE) ? ($test === $expected) : ($test == $expected); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | $extype = gettype($expected); |
| 157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | $back = $this->_backtrace(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | $report[] = array ( |
| 162 | 'test_name' => $test_name, |
| 163 | 'test_datatype' => gettype($test), |
| 164 | 'res_datatype' => $extype, |
| 165 | 'result' => ($result === TRUE) ? 'passed' : 'failed', |
| 166 | 'file' => $back['file'], |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 167 | 'line' => $back['line'], |
| 168 | 'notes' => $notes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | ); |
| 170 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 171 | $this->results[] = $report; |
| 172 | |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 173 | return $this->report($this->result($report)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | /** |
| 179 | * Generate a report |
| 180 | * |
| 181 | * Displays a table with the test data |
| 182 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 183 | * @param array $result |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | * @return string |
| 185 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 186 | public function report($result = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 188 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | { |
| 190 | $result = $this->result(); |
| 191 | } |
| 192 | |
| 193 | $CI =& get_instance(); |
| 194 | $CI->load->language('unit_test'); |
| 195 | |
| 196 | $this->_parse_template(); |
| 197 | |
| 198 | $r = ''; |
| 199 | foreach ($result as $res) |
| 200 | { |
| 201 | $table = ''; |
| 202 | |
| 203 | foreach ($res as $key => $val) |
| 204 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 205 | if ($key === $CI->lang->line('ut_result')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 207 | if ($val === $CI->lang->line('ut_passed')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | { |
| 209 | $val = '<span style="color: #0C0;">'.$val.'</span>'; |
| 210 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 211 | elseif ($val === $CI->lang->line('ut_failed')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | { |
| 213 | $val = '<span style="color: #C00;">'.$val.'</span>'; |
| 214 | } |
| 215 | } |
| 216 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 217 | $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | $r .= str_replace('{rows}', $table, $this->_template); |
| 221 | } |
| 222 | |
| 223 | return $r; |
| 224 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | /** |
| 229 | * Use strict comparison |
| 230 | * |
| 231 | * Causes the evaluation to use === rather than == |
| 232 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | * @param bool |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 234 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 236 | public function use_strict($state = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 238 | $this->strict = (bool) $state; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | /** |
| 244 | * Make Unit testing active |
| 245 | * |
| 246 | * Enables/disables unit testing |
| 247 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | * @param bool |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 249 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 251 | public function active($state = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 253 | $this->active = (bool) $state; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | /** |
| 259 | * Result Array |
| 260 | * |
| 261 | * Returns the raw result data |
| 262 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame^] | 263 | * @param array $results |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | * @return array |
| 265 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 266 | public function result($results = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | $CI =& get_instance(); |
| 269 | $CI->load->language('unit_test'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 271 | if (count($results) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | { |
| 273 | $results = $this->results; |
| 274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | $retval = array(); |
| 277 | foreach ($results as $result) |
| 278 | { |
| 279 | $temp = array(); |
| 280 | foreach ($result as $key => $val) |
| 281 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 282 | if ( ! in_array($key, $this->_test_items_visible)) |
| 283 | { |
| 284 | continue; |
| 285 | } |
| 286 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | if (is_array($val)) |
| 288 | { |
| 289 | foreach ($val as $k => $v) |
| 290 | { |
Kyle Johnson | c4a3c3c | 2012-10-03 14:34:37 -0700 | [diff] [blame] | 291 | if ( ! in_array($k, $this->_test_items_visible)) |
| 292 | { |
| 293 | continue; |
| 294 | } |
Andrey Andreev | 9438e26 | 2012-10-05 13:16:27 +0300 | [diff] [blame] | 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v)))) |
| 297 | { |
| 298 | $v = $line; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | } |
| 300 | $temp[$CI->lang->line('ut_'.$k)] = $v; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val)))) |
| 306 | { |
| 307 | $val = $line; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | $temp[$CI->lang->line('ut_'.$key)] = $val; |
| 310 | } |
| 311 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | $retval[] = $temp; |
| 314 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 315 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | return $retval; |
| 317 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | /** |
| 322 | * Set the template |
| 323 | * |
| 324 | * This lets us set the template to be used to display results |
| 325 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | * @param string |
| 327 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 328 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 329 | public function set_template($template) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | { |
| 331 | $this->_template = $template; |
| 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | /** |
| 337 | * Generate a backtrace |
| 338 | * |
| 339 | * This lets us show file names and line numbers |
| 340 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | * @return array |
| 342 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 343 | protected function _backtrace() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | { |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 345 | $back = debug_backtrace(); |
| 346 | return array( |
| 347 | 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''), |
| 348 | 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '') |
| 349 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | /** |
| 355 | * Get Default Template |
| 356 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | * @return string |
| 358 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 359 | protected function _default_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 361 | $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] | 362 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 363 | $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] | 364 | ."\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] | 365 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 366 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
| 370 | * Parse Template |
| 371 | * |
| 372 | * Harvests the data within the template {pseudo-variables} |
| 373 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | * @return void |
| 375 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 376 | protected function _parse_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 377 | { |
| 378 | if ( ! is_null($this->_template_rows)) |
| 379 | { |
| 380 | return; |
| 381 | } |
| 382 | |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 383 | 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] | 384 | { |
| 385 | $this->_default_template(); |
| 386 | return; |
| 387 | } |
| 388 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 389 | $this->_template_rows = $match[1]; |
| 390 | $this->_template = str_replace($match[0], '{rows}', $this->_template); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | |
| 395 | /** |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 396 | * Helper function to test boolean TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 398 | * @param mixed $test |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | * @return bool |
| 400 | */ |
| 401 | function is_true($test) |
| 402 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 403 | return ($test === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | } |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 405 | |
| 406 | /** |
| 407 | * Helper function to test boolean FALSE |
| 408 | * |
| 409 | * @param mixed $test |
| 410 | * @return bool |
| 411 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | function is_false($test) |
| 413 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 414 | return ($test === FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | /* End of file Unit_test.php */ |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 418 | /* Location: ./system/libraries/Unit_test.php */ |