autorun.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 2037 2011-11-30 17:58:21Z pp11 $
  7. */
  8. /**#@+
  9. * include simpletest files
  10. */
  11. require_once dirname(__FILE__) . '/unit_tester.php';
  12. require_once dirname(__FILE__) . '/mock_objects.php';
  13. require_once dirname(__FILE__) . '/collector.php';
  14. require_once dirname(__FILE__) . '/default_reporter.php';
  15. /**#@-*/
  16. $GLOBALS['SIMPLETEST_AUTORUNNER_INITIAL_CLASSES'] = get_declared_classes();
  17. $GLOBALS['SIMPLETEST_AUTORUNNER_INITIAL_PATH'] = getcwd();
  18. register_shutdown_function('simpletest_autorun');
  19. /**
  20. * Exit handler to run all recent test cases and exit system if in CLI
  21. */
  22. function simpletest_autorun() {
  23. chdir($GLOBALS['SIMPLETEST_AUTORUNNER_INITIAL_PATH']);
  24. if (tests_have_run()) {
  25. return;
  26. }
  27. $result = run_local_tests();
  28. if (SimpleReporter::inCli()) {
  29. exit($result ? 0 : 1);
  30. }
  31. }
  32. /**
  33. * run all recent test cases if no test has
  34. * so far been run. Uses the DefaultReporter which can have
  35. * it's output controlled with SimpleTest::prefer().
  36. * @return boolean/null false if there were test failures, true if
  37. * there were no failures, null if tests are
  38. * already running
  39. */
  40. function run_local_tests() {
  41. try {
  42. if (tests_have_run()) {
  43. return;
  44. }
  45. $candidates = capture_new_classes();
  46. $loader = new SimpleFileLoader();
  47. $suite = $loader->createSuiteFromClasses(
  48. basename(initial_file()),
  49. $loader->selectRunnableTests($candidates));
  50. return $suite->run(new DefaultReporter());
  51. } catch (Exception $stack_frame_fix) {
  52. print $stack_frame_fix->getMessage();
  53. return false;
  54. }
  55. }
  56. /**
  57. * Checks the current test context to see if a test has
  58. * ever been run.
  59. * @return boolean True if tests have run.
  60. */
  61. function tests_have_run() {
  62. if ($context = SimpleTest::getContext()) {
  63. return (boolean)$context->getTest();
  64. }
  65. return false;
  66. }
  67. /**
  68. * The first autorun file.
  69. * @return string Filename of first autorun script.
  70. */
  71. function initial_file() {
  72. static $file = false;
  73. if (! $file) {
  74. if (isset($_SERVER, $_SERVER['SCRIPT_FILENAME'])) {
  75. $file = $_SERVER['SCRIPT_FILENAME'];
  76. } else {
  77. $included_files = get_included_files();
  78. $file = reset($included_files);
  79. }
  80. }
  81. return $file;
  82. }
  83. /**
  84. * Every class since the first autorun include. This
  85. * is safe enough if require_once() is always used.
  86. * @return array Class names.
  87. */
  88. function capture_new_classes() {
  89. global $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES;
  90. return array_map('strtolower', array_diff(get_declared_classes(),
  91. $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES ?
  92. $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES : array()));
  93. }
  94. ?>