PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/simpletest/autorun.php

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