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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 8 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2019, British Columbia Institute of Technology |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | * @license https://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.3.1 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Unit Testing Class |
| 42 | * |
| 43 | * Simple testing class |
| 44 | * |
| 45 | * @package CodeIgniter |
| 46 | * @subpackage Libraries |
| 47 | * @category UnitTesting |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/libraries/unit_testing.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
| 51 | class CI_Unit_test { |
| 52 | |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 53 | /** |
| 54 | * Active flag |
| 55 | * |
| 56 | * @var bool |
| 57 | */ |
Andrey Andreev | a45e37e | 2015-07-08 17:58:21 +0300 | [diff] [blame] | 58 | public $active = TRUE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Test results |
| 62 | * |
| 63 | * @var array |
| 64 | */ |
Andrey Andreev | a45e37e | 2015-07-08 17:58:21 +0300 | [diff] [blame] | 65 | public $results = array(); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Strict comparison flag |
| 69 | * |
| 70 | * Whether to use === or == when comparing |
| 71 | * |
| 72 | * @var bool |
| 73 | */ |
Andrey Andreev | a45e37e | 2015-07-08 17:58:21 +0300 | [diff] [blame] | 74 | public $strict = FALSE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Template |
| 78 | * |
| 79 | * @var string |
| 80 | */ |
Andrey Andreev | a45e37e | 2015-07-08 17:58:21 +0300 | [diff] [blame] | 81 | protected $_template = NULL; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Template rows |
| 85 | * |
| 86 | * @var string |
| 87 | */ |
Andrey Andreev | a45e37e | 2015-07-08 17:58:21 +0300 | [diff] [blame] | 88 | protected $_template_rows = NULL; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * List of visible test items |
| 92 | * |
| 93 | * @var array |
| 94 | */ |
Andrey Andreev | a6eae87 | 2014-01-03 18:25:20 +0200 | [diff] [blame] | 95 | protected $_test_items_visible = array( |
Andrey Andreev | a45e37e | 2015-07-08 17:58:21 +0300 | [diff] [blame] | 96 | 'test_name', |
| 97 | 'test_datatype', |
| 98 | 'res_datatype', |
| 99 | 'result', |
| 100 | 'file', |
| 101 | 'line', |
| 102 | 'notes' |
Andrey Andreev | a6eae87 | 2014-01-03 18:25:20 +0200 | [diff] [blame] | 103 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 105 | // -------------------------------------------------------------------- |
| 106 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 107 | /** |
| 108 | * Constructor |
| 109 | * |
| 110 | * @return void |
| 111 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 112 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | { |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 114 | log_message('info', 'Unit Testing Class Initialized'); |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 115 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | |
| 117 | // -------------------------------------------------------------------- |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Run the tests |
| 121 | * |
| 122 | * Runs the supplied tests |
| 123 | * |
Andrey Andreev | cb66367 | 2014-01-03 18:45:37 +0200 | [diff] [blame] | 124 | * @param array $items |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 125 | * @return void |
| 126 | */ |
Andrey Andreev | cb66367 | 2014-01-03 18:45:37 +0200 | [diff] [blame] | 127 | public function set_test_items($items) |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 128 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 129 | if ( ! empty($items) && is_array($items)) |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 130 | { |
| 131 | $this->_test_items_visible = $items; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // -------------------------------------------------------------------- |
| 136 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | /** |
| 138 | * Run the tests |
| 139 | * |
| 140 | * Runs the supplied tests |
| 141 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 142 | * @param mixed $test |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 143 | * @param mixed $expected |
| 144 | * @param string $test_name |
| 145 | * @param string $notes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 148 | public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 150 | if ($this->active === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | { |
| 152 | return FALSE; |
| 153 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | |
ftwbzhao | a5ea506 | 2015-07-06 13:18:11 +0800 | [diff] [blame] | 155 | 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', 'is_resource'), TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 157 | $result = $expected($test); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected)); |
| 159 | } |
| 160 | else |
| 161 | { |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 162 | $result = ($this->strict === TRUE) ? ($test === $expected) : ($test == $expected); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | $extype = gettype($expected); |
| 164 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | $back = $this->_backtrace(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Daniel Paul Searles | d2ae1d1 | 2012-11-02 17:39:44 -0700 | [diff] [blame] | 168 | $report = array ( |
Andrey Andreev | a45e37e | 2015-07-08 17:58:21 +0300 | [diff] [blame] | 169 | 'test_name' => $test_name, |
| 170 | 'test_datatype' => gettype($test), |
| 171 | 'res_datatype' => $extype, |
| 172 | 'result' => ($result === TRUE) ? 'passed' : 'failed', |
| 173 | 'file' => $back['file'], |
| 174 | 'line' => $back['line'], |
| 175 | 'notes' => $notes |
| 176 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 178 | $this->results[] = $report; |
| 179 | |
Daniel Paul Searles | d2ae1d1 | 2012-11-02 17:39:44 -0700 | [diff] [blame] | 180 | return $this->report($this->result(array($report))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | /** |
| 186 | * Generate a report |
| 187 | * |
| 188 | * Displays a table with the test data |
| 189 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 190 | * @param array $result |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | * @return string |
| 192 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 193 | public function report($result = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 195 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | { |
| 197 | $result = $this->result(); |
| 198 | } |
| 199 | |
| 200 | $CI =& get_instance(); |
| 201 | $CI->load->language('unit_test'); |
| 202 | |
| 203 | $this->_parse_template(); |
| 204 | |
| 205 | $r = ''; |
| 206 | foreach ($result as $res) |
| 207 | { |
| 208 | $table = ''; |
| 209 | |
| 210 | foreach ($res as $key => $val) |
| 211 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 212 | if ($key === $CI->lang->line('ut_result')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 214 | if ($val === $CI->lang->line('ut_passed')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | { |
| 216 | $val = '<span style="color: #0C0;">'.$val.'</span>'; |
| 217 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 218 | elseif ($val === $CI->lang->line('ut_failed')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | { |
| 220 | $val = '<span style="color: #C00;">'.$val.'</span>'; |
| 221 | } |
| 222 | } |
| 223 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 224 | $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | $r .= str_replace('{rows}', $table, $this->_template); |
| 228 | } |
| 229 | |
| 230 | return $r; |
| 231 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | /** |
| 236 | * Use strict comparison |
| 237 | * |
| 238 | * Causes the evaluation to use === rather than == |
| 239 | * |
Andrey Andreev | a6eae87 | 2014-01-03 18:25:20 +0200 | [diff] [blame] | 240 | * @param bool $state |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 241 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 243 | public function use_strict($state = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 245 | $this->strict = (bool) $state; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 249 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | /** |
| 251 | * Make Unit testing active |
| 252 | * |
| 253 | * Enables/disables unit testing |
| 254 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | * @param bool |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 256 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 258 | public function active($state = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | { |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 260 | $this->active = (bool) $state; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 262 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | // -------------------------------------------------------------------- |
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 | /** |
| 266 | * Result Array |
| 267 | * |
| 268 | * Returns the raw result data |
| 269 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 270 | * @param array $results |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | * @return array |
| 272 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 273 | public function result($results = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 274 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | $CI =& get_instance(); |
| 276 | $CI->load->language('unit_test'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 278 | if (count($results) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | { |
| 280 | $results = $this->results; |
| 281 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | $retval = array(); |
| 284 | foreach ($results as $result) |
| 285 | { |
| 286 | $temp = array(); |
| 287 | foreach ($result as $key => $val) |
| 288 | { |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 289 | if ( ! in_array($key, $this->_test_items_visible)) |
| 290 | { |
| 291 | continue; |
| 292 | } |
Andrey Andreev | b5e2410 | 2016-08-10 13:21:15 +0300 | [diff] [blame] | 293 | elseif (in_array($key, array('test_name', 'test_datatype', 'res_datatype', 'result'), TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | { |
Andrey Andreev | 7018d89 | 2015-07-08 17:57:52 +0300 | [diff] [blame] | 295 | if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val), FALSE))) |
| 296 | { |
| 297 | $val = $line; |
| 298 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | } |
Andrey Andreev | a6eae87 | 2014-01-03 18:25:20 +0200 | [diff] [blame] | 300 | |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 301 | $temp[$CI->lang->line('ut_'.$key, FALSE)] = $val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | $retval[] = $temp; |
| 305 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 306 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | return $retval; |
| 308 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 311 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | /** |
| 313 | * Set the template |
| 314 | * |
| 315 | * This lets us set the template to be used to display results |
| 316 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | * @param string |
| 318 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 319 | */ |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 320 | public function set_template($template) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | { |
| 322 | $this->_template = $template; |
| 323 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 324 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | /** |
| 328 | * Generate a backtrace |
| 329 | * |
| 330 | * This lets us show file names and line numbers |
| 331 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | * @return array |
| 333 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 334 | protected function _backtrace() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | { |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 336 | $back = debug_backtrace(); |
| 337 | return array( |
Andrey Andreev | a45e37e | 2015-07-08 17:58:21 +0300 | [diff] [blame] | 338 | 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''), |
| 339 | 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '') |
| 340 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | /** |
| 346 | * Get Default Template |
| 347 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | * @return string |
| 349 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 350 | protected function _default_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 351 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 352 | $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] | 353 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 354 | $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] | 355 | ."\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] | 356 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 357 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * Parse Template |
| 362 | * |
| 363 | * Harvests the data within the template {pseudo-variables} |
| 364 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | * @return void |
| 366 | */ |
Andrey Andreev | 114586f | 2011-12-26 16:27:17 +0200 | [diff] [blame] | 367 | protected function _parse_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 368 | { |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 369 | if ($this->_template_rows !== NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 370 | { |
| 371 | return; |
| 372 | } |
| 373 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 374 | 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] | 375 | { |
| 376 | $this->_default_template(); |
| 377 | return; |
| 378 | } |
| 379 | |
Andrey Andreev | 2a27d31 | 2011-12-25 17:08:24 +0200 | [diff] [blame] | 380 | $this->_template_rows = $match[1]; |
| 381 | $this->_template = str_replace($match[0], '{rows}', $this->_template); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | |
| 386 | /** |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 387 | * Helper function to test boolean TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 389 | * @param mixed $test |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | * @return bool |
| 391 | */ |
| 392 | function is_true($test) |
| 393 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 394 | return ($test === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | } |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 396 | |
| 397 | /** |
| 398 | * Helper function to test boolean FALSE |
| 399 | * |
| 400 | * @param mixed $test |
| 401 | * @return bool |
| 402 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | function is_false($test) |
| 404 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 405 | return ($test === FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | } |