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