autorun.php 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Autorunner which runs all tests cases found in a file
  4. * that includes this module.
  5. * @package SimpleTest
  6. * @version $Id: autorun.php 1809 2008-09-12 00:46:55Z lastcraft $
  7. */
  8. require_once dirname(__FILE__) . '/unit_tester.php';
  9. require_once dirname(__FILE__) . '/mock_objects.php';
  10. require_once dirname(__FILE__) . '/collector.php';
  11. require_once dirname(__FILE__) . '/default_reporter.php';
  12. $GLOBALS['SIMPLETEST_AUTORUNNER_INITIAL_CLASSES'] = get_declared_classes();
  13. register_shutdown_function('simpletest_autorun');
  14. /**
  15. * Exit handler to run all recent test cases if no test has
  16. * so far been run. Uses the DefaultReporter which can have
  17. * it's output controlled with SimpleTest::prefer().
  18. */
  19. function simpletest_autorun() {
  20. try {
  21. if (tests_have_run()) {
  22. return;
  23. }
  24. $candidates = array_intersect(
  25. capture_new_classes(),
  26. classes_defined_in_initial_file());
  27. $loader = new SimpleFileLoader();
  28. $suite = $loader->createSuiteFromClasses(
  29. basename(initial_file()),
  30. $loader->selectRunnableTests($candidates));
  31. $result = $suite->run(new DefaultReporter());
  32. } catch (Exception $stack_frame_fix) {
  33. print $stack_frame_fix->getMessage();
  34. $result = false;
  35. }
  36. if (SimpleReporter::inCli()) {
  37. exit($result ? 0 : 1);
  38. }
  39. }
  40. /**
  41. * Checks the current test context to see if a test has
  42. * ever been run.
  43. * @return boolean True if tests have run.
  44. */
  45. function tests_have_run() {
  46. if ($context = SimpleTest::getContext()) {
  47. return (boolean)$context->getTest();
  48. }
  49. return false;
  50. }
  51. /**
  52. * The first autorun file.
  53. * @return string Filename of first autorun script.
  54. */
  55. function initial_file() {
  56. static $file = false;
  57. if (! $file) {
  58. if (isset($_SERVER, $_SERVER['SCRIPT_FILENAME'])) {
  59. $file = $_SERVER['SCRIPT_FILENAME'];
  60. } else {
  61. $included_files = get_included_files();
  62. $file = reset($included_files);
  63. }
  64. }
  65. return $file;
  66. }
  67. /**
  68. * Just the classes from the first autorun script. May
  69. * get a few false positives, as it just does a regex based
  70. * on following the word "class".
  71. * @return array List of all possible classes in first
  72. * autorun script.
  73. */
  74. function classes_defined_in_initial_file() {
  75. if (preg_match_all('/\bclass\s+(\w+)/i', file_get_contents(initial_file()), $matches)) {
  76. return array_map('strtolower', $matches[1]);
  77. }
  78. return array();
  79. }
  80. /**
  81. * Every class since the first autorun include. This
  82. * is safe enough if require_once() is alwyas used.
  83. * @return array Class names.
  84. */
  85. function capture_new_classes() {
  86. global $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES;
  87. return array_map('strtolower', array_diff(get_declared_classes(),
  88. $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES ?
  89. $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES : array()));
  90. }
  91. ?>