TestDriver.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * A driver for the PHP OpenID unit tests.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @package OpenID
  10. * @author JanRain, Inc. <openid@janrain.com>
  11. * @copyright 2005-2008 Janrain, Inc.
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  13. */
  14. require_once 'PHPUnit/Framework.php';
  15. //require_once 'PHPUnit/GUI/HTML.php';
  16. error_reporting(E_ALL | E_STRICT);
  17. global $__test_errors;
  18. $__test_errors = array();
  19. function __handler($code, $message)
  20. {
  21. global $__test_errors;
  22. if ($code == E_USER_WARNING) {
  23. $__test_errors[] = $message;
  24. }
  25. }
  26. function __raiseError($message)
  27. {
  28. set_error_handler('__handler');
  29. trigger_error($message, E_USER_WARNING);
  30. restore_error_handler();
  31. }
  32. function __getError()
  33. {
  34. global $__test_errors;
  35. if ($__test_errors) {
  36. return array_pop($__test_errors);
  37. }
  38. return null;
  39. }
  40. /**
  41. * Load the tests that are defined in the named modules.
  42. *
  43. * If you have Tests/Foo.php which defines a test class called
  44. * Tests_Foo, the call would look like:
  45. *
  46. * loadTests('Tests/', array('Foo'))
  47. *
  48. * @param string $test_dir The root of the test hierarchy. Must end
  49. * with a /
  50. *
  51. * @param array $test_names The names of the modules in which the
  52. * tests are defined. This should not include the root of the test
  53. * hierarchy.
  54. */
  55. function loadTests($test_dir, $test_names)
  56. {
  57. global $_tests;
  58. $suites = array();
  59. foreach ($test_names as $filename) {
  60. $filename = $test_dir . $filename . '.php';
  61. if (!global_require_once($filename)) {
  62. continue;
  63. }
  64. $class_name = str_replace('/', '_', $filename);
  65. $class_name = basename($class_name, '.php');
  66. $suites[] = makeSuite($class_name);
  67. }
  68. return $suites;
  69. }
  70. function makeSuite($class_name) {
  71. $test = new $class_name($class_name);
  72. if (is_a($test, 'PHPUnit_Framework_TestCase')) {
  73. $s = new PHPUnit_Framework_TestSuite();
  74. $s->setName($class_name);
  75. $s->addTestSuite($class_name);
  76. $test = $s;
  77. }
  78. $tc_array_name = $class_name . '_other';
  79. if (array_key_exists($tc_array_name, $GLOBALS) &&
  80. is_array($GLOBALS[$tc_array_name])) {
  81. foreach ($GLOBALS[$tc_array_name] as $tc) {
  82. $test->addTestSuite(get_class($tc));
  83. }
  84. }
  85. return $test;
  86. }
  87. function global_require_once($name)
  88. {
  89. $f = include_once $name;
  90. if (!$f) {
  91. print("global require once skipping $name\n");
  92. return false;
  93. }
  94. foreach (get_defined_vars() as $k => $v) {
  95. if (!in_array($k, array('name', 'GLOBALS'))) {
  96. $GLOBALS[$k] = $v;
  97. }
  98. }
  99. return true;
  100. }
  101. $_tests = array(
  102. array(
  103. 'dir' => 'Tests/Auth/OpenID/',
  104. 'files' => array(
  105. 'Association',
  106. 'AssociationResponse',
  107. 'AuthRequest',
  108. 'AX',
  109. 'BigMath',
  110. 'Consumer',
  111. 'CryptUtil',
  112. 'DiffieHellman',
  113. 'Discover_OpenID',
  114. 'Extension',
  115. 'HMAC',
  116. 'KVForm',
  117. 'Message',
  118. 'Negotiation',
  119. 'Nonce',
  120. 'OpenID_Yadis',
  121. 'PAPE',
  122. 'Parse',
  123. 'RPVerify',
  124. 'Server',
  125. 'SReg',
  126. 'StoreTest',
  127. 'TrustRoot',
  128. 'URINorm',
  129. 'Util',
  130. 'VerifyDisco'),
  131. ),
  132. array(
  133. 'dir' => 'Tests/Auth/Yadis/',
  134. 'files' => array(
  135. 'ParseHTML',
  136. 'XRDS',
  137. 'Yadis',
  138. 'Discover_Yadis',
  139. 'XRI'
  140. )
  141. )
  142. );
  143. function selectTests($package, $names)
  144. {
  145. global $_tests;
  146. $lnames = array_map('strtolower', $names);
  147. $include = array();
  148. $exclude = array();
  149. foreach ($package['files'] as $t) {
  150. $l = strtolower($t);
  151. if (in_array($l, $lnames)) {
  152. $include[] = $t;
  153. }
  154. if (in_array("/$l", $lnames)) {
  155. $exclude[] = $t;
  156. }
  157. }
  158. return array_diff($include, $exclude);
  159. }
  160. // Load OpenID library tests
  161. function loadSuite($names=null)
  162. {
  163. global $_tests;
  164. $result = array();
  165. foreach ($_tests as $package) {
  166. if (!$names) {
  167. $selected = $package['files'];
  168. } else {
  169. $selected = selectTests($package, $names);
  170. }
  171. $result = array_merge($result, loadTests($package['dir'], $selected));
  172. }
  173. return $result;
  174. }