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