| [ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
1 <?php // -*- fill-column: 80; tab-width: 4; c-basic-offset: 4 -*- 2 /** 3 * Lots TODO here... 4 */ 5 6 define('TEST_GROUPS',realpath(dirname(__FILE__).'/../cases')); 7 define('TEST_CASES',realpath(dirname(__FILE__).'/../cases')); 8 9 // try to load runkit extension 10 if (!extension_loaded('runkit')) { 11 if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) { 12 @dl('php_runkit.dll'); 13 } else { 14 @dl('runkit.so'); 15 } 16 } 17 18 19 class TestManager { 20 var $_testcase_extension = '.test.php'; 21 var $_grouptest_extension = '.group.php'; 22 23 function setup() { 24 $ini_file = realpath(dirname(__FILE__).'/../tests.ini'); 25 26 if (! file_exists($ini_file)) { 27 trigger_error("Missing configuration file {$ini_file}", 28 E_USER_ERROR); 29 } 30 $config = parse_ini_file($ini_file); 31 foreach ($config as $key => $value) { 32 define($key, $value); 33 } 34 TestManager::_installSimpleTest(); 35 36 list($version) = file(SIMPLE_TEST.'VERSION'); 37 $version = trim($version); 38 if(!version_compare('1.0.1alpha',$version,'<')){ 39 echo "At least SimpleTest Version 1.0.1alpha is required."; 40 echo " Yours is $version\n"; 41 exit; 42 } 43 } 44 45 function _installSimpleTest() { 46 require_once SIMPLE_TEST . 'unit_tester.php'; 47 require_once SIMPLE_TEST . 'web_tester.php'; 48 require_once SIMPLE_TEST . 'mock_objects.php'; 49 require_once 'web.inc.php'; 50 require_once 'mock_functions.php'; 51 } 52 53 function runAllTests(&$reporter) { 54 $manager =& new TestManager(); 55 $test_cases =& $manager->_getTestFileList(); 56 $test =& new GroupTest('All Tests'); 57 foreach ($test_cases as $test_case) { 58 $test->addTestFile($test_case); 59 } 60 $test->run($reporter); 61 } 62 63 function runTestCase($testcase_name, $test_case_directory, &$reporter) { 64 $manager =& new TestManager(); 65 66 $testcase_name = preg_replace('/[^a-zA-Z0-9_:]/','',$testcase_name); 67 $testcase_name = str_replace(':',DIRECTORY_SEPARATOR,$testcase_name); 68 69 $testcase_file = $test_case_directory . DIRECTORY_SEPARATOR . 70 strtolower($testcase_name) . $manager->_testcase_extension; 71 72 if (! file_exists($testcase_file)) { 73 trigger_error("Test case {$testcase_file} cannot be found", 74 E_USER_ERROR); 75 } 76 77 $test =& new GroupTest("Individual test case: " . $testcase_name); 78 $test->addTestFile($testcase_file); 79 $test->run($reporter); 80 } 81 82 function runTestFile($testcase_file, &$reporter) { 83 $manager =& new TestManager(); 84 85 if (! file_exists($testcase_file)) { 86 trigger_error("Test case {$testcase_file} cannot be found", 87 E_USER_ERROR); 88 } 89 90 $test =& new GroupTest("Individual test case: " . $testcase_file); 91 $test->addTestFile($testcase_file); 92 $test->run($reporter); 93 } 94 95 function runGroupTest($group_test_name, $group_test_directory, &$reporter) { 96 $manager =& new TestManager(); 97 $group_test_name = preg_replace('/[^a-zA-Z0-9_:]/','',$group_test_name); 98 $group_test_name = str_replace(':',DIRECTORY_SEPARATOR,$group_test_name); 99 $file_path = $group_test_directory . DIRECTORY_SEPARATOR . 100 strtolower($group_test_name) . $manager->_grouptest_extension; 101 102 if (! file_exists($file_path)) { 103 trigger_error("Group test {$group_test_name} cannot be found at {$file_path}", 104 E_USER_ERROR); 105 } 106 107 require_once $file_path; 108 $test =& new GroupTest($group_test_name . ' group test'); 109 foreach ($manager->_getGroupTestClassNames($file_path) as $group_test) { 110 $test->addTestCase(new $group_test()); 111 } 112 $test->run($reporter); 113 } 114 115 function addTestCasesFromDirectory(&$group_test, $directory = '.') { 116 $manager =& new TestManager(); 117 $test_cases =& $manager->_getTestFileList($directory); 118 foreach ($test_cases as $test_case) { 119 $group_test->addTestFile($test_case); 120 } 121 } 122 123 function &getTestCaseList($directory = '.') { 124 $manager =& new TestManager(); 125 return $manager->_getTestCaseList($directory); 126 } 127 128 function &_getTestCaseList($directory = '.') { 129 $base = TEST_GROUPS . DIRECTORY_SEPARATOR; 130 $file_list =& $this->_getTestFileList($directory); 131 $testcases = array(); 132 foreach ($file_list as $testcase_file) { 133 $case = str_replace($this->_testcase_extension, '',$testcase_file); 134 $case = str_replace($base, '', $case); 135 $case = str_replace(DIRECTORY_SEPARATOR, ':', $case); 136 $testcases[$testcase_file] = $case; 137 } 138 return $testcases; 139 } 140 141 function &_getTestFileList($directory = '.') { 142 return $this->_getRecursiveFileList($directory, 143 array(&$this, '_isTestCaseFile')); 144 } 145 146 function &getGroupTestList($directory = '.') { 147 $manager =& new TestManager(); 148 return $manager->_getTestGroupList($directory); 149 } 150 151 function &_getTestGroupFileList($directory = '.') { 152 return $this->_getRecursiveFileList($directory, 153 array(&$this, '_isTestGroupFile')); 154 } 155 156 function &_getTestGroupList($directory = '.') { 157 $base = TEST_GROUPS . DIRECTORY_SEPARATOR; 158 $file_list =& $this->_getTestGroupFileList($directory); 159 $grouptests = array(); 160 foreach ($file_list as $grouptest_file) { 161 $group = str_replace($this->_grouptest_extension, '',$grouptest_file); 162 $group = str_replace($base, '', $group); 163 $group = str_replace(DIRECTORY_SEPARATOR, ':', $group); 164 $grouptests[$grouptest_file] = $group; 165 } 166 sort($grouptests); 167 return $grouptests; 168 } 169 170 function &_getGroupTestClassNames($grouptest_file) { 171 $file = implode("\n", file($grouptest_file)); 172 preg_match("~lass\s+?(.*)\s+?extends GroupTest~", $file, $matches); 173 if (! empty($matches)) { 174 unset($matches[0]); 175 return $matches; 176 } else { 177 return array(); 178 } 179 } 180 181 function &_getRecursiveFileList($directory = '.', $file_test_function) { 182 $dh = opendir($directory); 183 if (! is_resource($dh)) { 184 trigger_error("Couldn't open {$directory}", E_USER_ERROR); 185 } 186 187 $file_list = array(); 188 while ($file = readdir($dh)) { 189 $file_path = $directory . DIRECTORY_SEPARATOR . $file; 190 191 if (0 === strpos($file, '.')) continue; 192 193 if (is_dir($file_path)) { 194 $file_list = 195 array_merge($file_list, 196 $this->_getRecursiveFileList($file_path, 197 $file_test_function)); 198 } 199 if ($file_test_function[0]->$file_test_function[1]($file)) { 200 $file_list[] = $file_path; 201 } 202 } 203 closedir($dh); 204 return $file_list; 205 } 206 207 function _isTestCaseFile($file) { 208 return $this->_hasExpectedExtension($file, $this->_testcase_extension); 209 } 210 211 function _isTestGroupFile($file) { 212 return $this->_hasExpectedExtension($file, $this->_grouptest_extension); 213 } 214 215 function _hasExpectedExtension($file, $extension) { 216 return $extension == 217 strtolower(substr($file, (0 - strlen($extension)))); 218 } 219 } 220 221 /** 222 * @package WACT_TESTS 223 */ 224 class CLITestManager extends TestManager { 225 function &getGroupTestList($directory = '.') { 226 $manager =& new CLITestManager(); 227 $group_tests =& $manager->_getTestGroupList($directory); 228 229 $buffer = "Available grouptests:\n"; 230 foreach ($group_tests as $group_test) { 231 $buffer .= " " . $group_test . "\n"; 232 } 233 return $buffer . "\n"; 234 } 235 236 function &getTestCaseList($directory = '.') { 237 $manager =& new CLITestManager(); 238 $test_cases =& $manager->_getTestCaseList($directory); 239 240 $buffer = "Available test cases:\n"; 241 foreach ($test_cases as $test_case) { 242 $buffer .= " " . $test_case . "\n"; 243 } 244 return $buffer . "\n"; 245 } 246 } 247 248 class HTMLTestManager extends TestManager { 249 var $_url; 250 251 function HTMLTestManager() { 252 $this->_url = $_SERVER['PHP_SELF']; 253 } 254 255 function getBaseURL() { 256 return $this->_url; 257 } 258 259 function &getGroupTestList($directory = '.') { 260 $manager =& new HTMLTestManager(); 261 $group_tests =& $manager->_getTestGroupList($directory); 262 if (1 > count($group_tests)) { 263 return "<p>No test groups set up!</p>"; 264 } 265 $buffer = "<p>Available test groups:</p>\n<ul>"; 266 $buffer .= "<li><a href='" . $manager->getBaseURL() . "?group=all'>All tests</a></li>\n"; 267 foreach ($group_tests as $group_test) { 268 $buffer .= "<li><a href='" . $manager->getBaseURL() . "?group={$group_test}'>" . 269 $group_test . "</a></li>\n"; 270 } 271 272 $buffer .= "</ul>\n"; 273 return $buffer; 274 } 275 276 function &getTestCaseList($directory = '.') { 277 $manager =& new HTMLTestManager(); 278 $testcases =& $manager->_getTestCaseList($directory); 279 280 if (1 > count($testcases)) { 281 return "<p>No test cases set up!</p>"; 282 } 283 $buffer = "<p>Available test cases:</p>\n<ul>"; 284 foreach ($testcases as $testcase) { 285 $buffer .= "<li><a href='" . $manager->getBaseURL() . 286 "?case=" . urlencode($testcase) . "'>" . 287 $testcase . "</a></li>\n"; 288 } 289 290 $buffer .= "</ul>\n"; 291 return $buffer; 292 } 293 } 294 295 /** 296 * @package WACT_TESTS 297 */ 298 class XMLTestManager extends HTMLTestManager { 299 300 function XMLTestManager() { 301 parent::HTMLTestManager(); 302 } 303 304 function &getGroupTestList($directory = '.') { 305 306 $manager =& new XMLTestManager(); 307 $group_tests =& $manager->_getTestGroupList($directory); 308 309 $rss = & $manager->_getRssWriter(); 310 311 if (1 > count($group_tests)) { 312 $rss->writeRss($output); 313 return $output; 314 } 315 316 $properties["title"]="All Tests"; 317 $properties["description"]="All Tests"; 318 $properties["link"]='http://'.$_SERVER['SERVER_NAME']. 319 $manager->getBaseURL()."?group=all&output=xml"; 320 321 $rss->additem($properties); 322 323 foreach ($group_tests as $group_test) { 324 $properties["title"]=$group_test; 325 $properties["description"]=$group_test; 326 $properties["link"]='http://'.$_SERVER['SERVER_NAME']. 327 $manager->getBaseURL(). 328 "?group={$group_test}&output=xml"; 329 330 $rss->additem($properties); 331 } 332 if ( !$rss->writeRss($output) ) { 333 die ( $rss->error ); 334 } 335 return $output; 336 337 } 338 339 function &getTestCaseList($directory = '.') { 340 341 $manager =& new XMLTestManager(); 342 $testcases =& $manager->_getTestCaseList($directory); 343 344 $rss = & $manager->_getRssWriter(); 345 346 if (1 > count($testcases)) { 347 $rss->writeRss($output); 348 return $output; 349 } 350 351 foreach ($testcases as $testfile => $testcase) { 352 $properties["title"]=$testcase; 353 $properties["description"]=$testcase; 354 $properties["link"]='http://'.$_SERVER['SERVER_NAME']. 355 $manager->getBaseURL()."?case=" . 356 urlencode($testcase) . "&output=xml"; 357 358 // Comment this out for performance? 359 $properties["dc:date"]=gmdate("Y-m-d\TH:i:sO",filemtime($testfile)); 360 361 $rss->additem($properties); 362 } 363 364 $rss->writeRss($output); 365 return $output; 366 } 367 368 function &_getRssWriter() { 369 370 $url = 'http://'.$_SERVER['SERVER_NAME'].str_replace('index.php','',$_SERVER['PHP_SELF']); 371 372 require_once TEST_ROOT . '/lib/xml_writer_class.php'; 373 require_once TEST_ROOT . '/lib/rss_writer_class.php'; 374 375 $rss_writer_object=& new rss_writer_class(); 376 $rss_writer_object->specification="1.0"; 377 $rss_writer_object->about=$url."index.php?output=xml"; 378 $rss_writer_object->stylesheet=$url."rss2html.xsl"; 379 $rss_writer_object->rssnamespaces["dc"]="http://purl.org/dc/elements/1.1/"; 380 381 // Channel Properties 382 $properties=array(); 383 $properties["title"]="Dokuwiki Unit Test Cases"; 384 $properties["description"]="Dokuwiki Unit Test Cases"; 385 $properties["link"]="http://wiki.splitbrain.org/"; 386 $properties["dc:date"]=gmdate("Y-m-d\TH:i:sO"); 387 $rss_writer_object->addchannel($properties); 388 389 // Logo like this (if we had one) 390 /* 391 $properties=array(); 392 393 $properties["link"]="http://www.phpclasses.org/"; 394 $properties["title"]="PHP Classes repository logo"; 395 $properties["description"]="Repository of components and other resources for PHP developers"; 396 $rss_writer_object->addimage($properties); 397 */ 398 399 return $rss_writer_object; 400 } 401 402 } 403 404 /** 405 * @package WACT_TESTS 406 */ 407 class RemoteTestManager extends TestManager { 408 409 function RemoteTestManager() { 410 RemoteTestManager::_installSimpleTest(); 411 } 412 413 function _installSimpleTest() { 414 require_once SIMPLE_TEST . 'remote.php'; 415 } 416 417 function runAllTests(&$reporter, $url = FALSE) { 418 $groups = RemoteTestManager::getGroupTestList($url); 419 $T = &new RemoteTestCase($groups['All Tests']); 420 $T->run($reporter); 421 } 422 423 function runTestUrl($case_url,& $reporter, $url = FALSE) { 424 RemoteTestManager::_installSimpleTest(); 425 $T = &new RemoteTestCase($case_url); 426 $T->run($reporter); 427 } 428 429 function runTestCase($case_id,& $reporter, $url = FALSE) { 430 $cases = RemoteTestManager::getTestCaseList($url); 431 if ( !array_key_exists($case_id, $cases) ) { 432 trigger_error("Unknown test id $case_id\n",E_USER_ERROR); 433 } 434 $T = &new RemoteTestCase($cases[$case_id]); 435 $T->run($reporter); 436 } 437 438 function runGroupTest($group_name, &$reporter, $url = FALSE) { 439 $groups = RemoteTestManager::getGroupTestList($url); 440 if ( !array_key_exists($group_name, $groups) ) { 441 trigger_error("Unknown group $group_name\n",E_USER_ERROR); 442 } 443 $T = &new RemoteTestCase($groups[$group_name]); 444 $T->run($reporter); 445 } 446 447 function & getGroupTestList($url = FALSE) { 448 449 if ( !$url<