1<?php 2 3/*******************************************************************\ 4* PROJECT INFORMATION * 5* * 6* Project: Apache-Test * 7* URL: http://perl.apache.org/Apache-Test/ * 8* Notice: Copyright (c) 2006 The Apache Software Foundation * 9* * 10********************************************************************* 11* LICENSE INFORMATION * 12* * 13* Licensed under the Apache License, Version 2.0 (the "License"); * 14* you may not use this file except in compliance with the * 15* License. You may obtain a copy of the License at: * 16* * 17* http://www.apache.org/licenses/LICENSE-2.0 * 18* * 19* Unless required by applicable law or agreed to in writing, * 20* software distributed under the License is distributed on an "AS * 21* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * 22* express or implied. See the License for the specific language * 23* governing permissions and limitations under the License. * 24* * 25********************************************************************* 26* MODULE INFORMATION * 27* * 28* This is a PHP implementation of Test::More: * 29* * 30* http://search.cpan.org/dist/Test-Simple/lib/Test/More.pm * 31* * 32* It is customized by Baron Schwartz for the Cacti Template Plugin * 33* * 34********************************************************************* 35* CREDITS * 36* * 37* Originally inspired by work from Andy Lester. Written and * 38* maintained by Chris Shiflett. For contact information, see: * 39* * 40* http://shiflett.org/contact * 41* * 42\*******************************************************************/ 43 44header('Content-Type: text/plain'); 45register_shutdown_function('_test_end'); 46 47$_no_plan = FALSE; 48$_num_failures = 0; 49$_num_skips = 0; 50$_test_num = 0; 51 52function plan($plan) 53{ 54 /* 55 plan('no_plan'); 56 plan('skip_all'); 57 plan(array('skip_all' => 'My reason is...')); 58 plan(23); 59 */ 60 61 global $_no_plan; 62 global $_skip_all; 63 64 switch ($plan) { 65 case 'no_plan': 66 $_no_plan = TRUE; 67 break; 68 69 case 'skip_all': 70 echo "1..0\n"; 71 break; 72 73 default: 74 if (is_array($plan)) { 75 echo "1..0 # Skip {$plan['skip_all']}\n"; 76 exit; 77 } 78 79 echo "1..$plan\n"; 80 break; 81 } 82} 83 84function ok($pass, $test_name = '') 85{ 86 global $_test_num; 87 global $_num_failures; 88 global $_num_skips; 89 90 $_test_num++; 91 92 if ($_num_skips) { 93 $_num_skips--; 94 return TRUE; 95 } 96 97 if (!empty($test_name) && $test_name[0] != '#') { 98 $test_name = "- $test_name"; 99 } 100 101 if ($pass) { 102 echo "ok $_test_num $test_name\n"; 103 } else { 104 echo "not ok $_test_num $test_name\n"; 105 106 $_num_failures++; 107 $caller = debug_backtrace(); 108 109 if (strstr($caller['0']['file'], $_SERVER['PHP_SELF'])) { 110 $file = $caller['0']['file']; 111 $line = $caller['0']['line']; 112 } else { 113 $file = $caller['1']['file']; 114 $line = $caller['1']['line']; 115 } 116 117 if ( isset($_SERVER['SERVER_ROOT']) ) { 118 $file = str_replace($_SERVER['SERVER_ROOT'], 't', $file); 119 } 120 121 diag(" Failed test ($file at line $line)"); 122 } 123 124 return $pass; 125} 126 127function is($this, $that, $test_name = '') 128{ 129 $pass = ($this == $that); 130 131 ok($pass, $test_name); 132 133 if (!$pass) { 134 diag(" got: '$this'"); 135 diag(" expected: '$that'"); 136 } 137 138 return $pass; 139} 140 141function is_deeply($this, $that, $test_name = '') 142{ 143 $diff1 = array_diff_assoc($this, $that); 144 $diff2 = array_diff_assoc($that, $this); 145 $pass = count($diff1) == 0; 146 ok($pass, $test_name); 147 if ( !$pass ) { 148 diag(" got: " . var_export($diff1, true)); 149 diag(" expected: " . var_export($diff2, true)); 150 } 151 return $pass; 152} 153 154function isnt($this, $that, $test_name = '') 155{ 156 $pass = ($this != $that); 157 158 ok($pass, $test_name); 159 160 if (!$pass) { 161 diag(" '$this'"); 162 diag(' !='); 163 diag(" '$that'"); 164 } 165 166 return $pass; 167} 168 169function like($string, $pattern, $test_name = '') 170{ 171 $pass = preg_match($pattern, $string); 172 173 ok($pass, $test_name); 174 175 if (!$pass) { 176 diag(" '$string'"); 177 diag(" doesn't match '$pattern'"); 178 } 179 180 return $pass; 181} 182 183function unlike($string, $pattern, $test_name = '') 184{ 185 $pass = !preg_match($pattern, $string); 186 187 ok($pass, $test_name); 188 189 if (!$pass) { 190 diag(" '$string'"); 191 diag(" matches '$pattern'"); 192 } 193 194 return $pass; 195} 196 197function cmp_ok($this, $operator, $that, $test_name = '') 198{ 199 eval("\$pass = (\$this $operator \$that);"); 200 201 ob_start(); 202 var_dump($this); 203 $_this = trim(ob_get_clean()); 204 205 ob_start(); 206 var_dump($that); 207 $_that = trim(ob_get_clean()); 208 209 ok($pass, $test_name); 210 211 if (!$pass) { 212 diag(" got: $_this"); 213 diag(" expected: $_that"); 214 } 215 216 return $pass; 217} 218 219function can_ok($object, $methods) 220{ 221 $pass = TRUE; 222 $errors = array(); 223 224 foreach ($methods as $method) { 225 if (!method_exists($object, $method)) { 226 $pass = FALSE; 227 $errors[] = " method_exists(\$object, $method) failed"; 228 } 229 } 230 231 if ($pass) { 232 ok(TRUE, "method_exists(\$object, ...)"); 233 } else { 234 ok(FALSE, "method_exists(\$object, ...)"); 235 diag($errors); 236 } 237 238 return $pass; 239} 240 241function isa_ok($object, $expected_class, $object_name = 'The object') 242{ 243 $got_class = get_class($object); 244 245 if (version_compare(phpversion(), '5', '>=')) { 246 $pass = ($got_class == $expected_class); 247 } else { 248 $pass = ($got_class == strtolower($expected_class)); 249 } 250 251 if ($pass) { 252 ok(TRUE, "$object_name isa $expected_class"); 253 } else { 254 ok(FALSE, "$object_name isn't a '$expected_class' it's a '$got_class'"); 255 } 256 257 return $pass; 258} 259 260function pass($test_name = '') 261{ 262 return ok(TRUE, $test_name); 263} 264 265function fail($test_name = '') 266{ 267 return ok(FALSE, $test_name); 268} 269 270function diag($message) 271{ 272 if (is_array($message)) { 273 foreach($message as $current) { 274 echo "# $current\n"; 275 } 276 } else { 277 echo "# $message\n"; 278 } 279} 280 281function include_ok($module) 282{ 283 $pass = ((include $module) == 1); 284 return ok($pass); 285} 286 287function require_ok($module) 288{ 289 $pass = ((require $module) == 1); 290 return ok($pass); 291} 292 293function skip($message, $num) 294{ 295 global $_num_skips; 296 297 if ($num < 0) { 298 $num = 0; 299 } 300 301 for ($i = 0; $i < $num; $i++) { 302 pass("# SKIP $message"); 303 } 304 305 $_num_skips = $num; 306} 307 308/* 309 310TODO: 311 312function todo() 313{ 314} 315 316function todo_skip() 317{ 318} 319 320function is_deeply() 321{ 322} 323 324function eq_array() 325{ 326} 327 328function eq_hash() 329{ 330} 331 332function eq_set() 333{ 334} 335 336*/ 337 338function _test_end() 339{ 340 global $_no_plan; 341 global $_num_failures; 342 global $_test_num; 343 344 if ($_no_plan) { 345 echo "1..$_test_num\n"; 346 } 347 348 if ($_num_failures) { 349 diag("Looks like you failed $_num_failures tests of $_test_num."); 350 } 351} 352 353?> 354 355