autorun.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 1721 2008-04-07 19:27:10Z 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. if (tests_have_run()) {
  21. return;
  22. }
  23. $candidates = array_intersect(
  24. capture_new_classes(),
  25. classes_defined_in_initial_file());
  26. $loader = new SimpleFileLoader();
  27. $suite = $loader->createSuiteFromClasses(
  28. basename(initial_file()),
  29. $loader->selectRunnableTests($candidates));
  30. $result = $suite->run(new DefaultReporter());
  31. if (SimpleReporter::inCli()) {
  32. exit($result ? 0 : 1);
  33. }
  34. }
  35. /**
  36. * Checks the current test context to see if a test has
  37. * ever been run.
  38. * @return boolean True if tests have run.
  39. */
  40. function tests_have_run() {
  41. if ($context = SimpleTest::getContext()) {
  42. return (boolean)$context->getTest();
  43. }
  44. return false;
  45. }
  46. /**
  47. * The first autorun file.
  48. * @return string Filename of first autorun script.
  49. */
  50. function initial_file() {
  51. static $file = false;
  52. if (! $file) {
  53. $file = reset(get_included_files());
  54. }
  55. return $file;
  56. }
  57. /**
  58. * Just the classes from the first autorun script. May
  59. * get a few false positives, as it just does a regex based
  60. * on following the word "class".
  61. * @return array List of all possible classes in first
  62. * autorun script.
  63. */
  64. function classes_defined_in_initial_file() {
  65. if (preg_match_all('/\bclass\s+(\w+)/i', file_get_contents(initial_file()), $matches)) {
  66. return array_map('strtolower', $matches[1]);
  67. }
  68. return array();
  69. }
  70. /**
  71. * Every class since the first autorun include. This
  72. * is safe enough if require_once() is alwyas used.
  73. * @return array Class names.
  74. */
  75. function capture_new_classes() {
  76. global $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES;
  77. return array_map('strtolower', array_diff(get_declared_classes(),
  78. $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES ?
  79. $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES : array()));
  80. }
  81. ?>