[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/tests/lib/ -> testmanager.php (source)

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


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5