aboutsummaryrefslogtreecommitdiff
path: root/vendors/simpletest/autorun.php
diff options
context:
space:
mode:
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2009-10-02 18:40:04 +0000
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2009-10-02 18:40:04 +0000
commit322bb9cd2be9e51422cb2b82684692e825c2bfb7 (patch)
tree1e8e75c1ff0c47d6eda9db114a13dfa2b83adf72 /vendors/simpletest/autorun.php
parent4ffb02ad9c1b95013a0fbf97cefde827600ecc3f (diff)
downloadelgg-322bb9cd2be9e51422cb2b82684692e825c2bfb7.tar.gz
elgg-322bb9cd2be9e51422cb2b82684692e825c2bfb7.tar.bz2
Added simpletest and start of unit tests.
git-svn-id: http://code.elgg.org/elgg/trunk@3503 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'vendors/simpletest/autorun.php')
-rwxr-xr-xvendors/simpletest/autorun.php87
1 files changed, 87 insertions, 0 deletions
diff --git a/vendors/simpletest/autorun.php b/vendors/simpletest/autorun.php
new file mode 100755
index 000000000..7d97d2d7f
--- /dev/null
+++ b/vendors/simpletest/autorun.php
@@ -0,0 +1,87 @@
+<?php
+/**
+ * Autorunner which runs all tests cases found in a file
+ * that includes this module.
+ * @package SimpleTest
+ * @version $Id: autorun.php 1721 2008-04-07 19:27:10Z lastcraft $
+ */
+require_once dirname(__FILE__) . '/unit_tester.php';
+require_once dirname(__FILE__) . '/mock_objects.php';
+require_once dirname(__FILE__) . '/collector.php';
+require_once dirname(__FILE__) . '/default_reporter.php';
+
+$GLOBALS['SIMPLETEST_AUTORUNNER_INITIAL_CLASSES'] = get_declared_classes();
+register_shutdown_function('simpletest_autorun');
+
+/**
+ * Exit handler to run all recent test cases if no test has
+ * so far been run. Uses the DefaultReporter which can have
+ * it's output controlled with SimpleTest::prefer().
+ */
+function simpletest_autorun() {
+ if (tests_have_run()) {
+ return;
+ }
+ $candidates = array_intersect(
+ capture_new_classes(),
+ classes_defined_in_initial_file());
+ $loader = new SimpleFileLoader();
+ $suite = $loader->createSuiteFromClasses(
+ basename(initial_file()),
+ $loader->selectRunnableTests($candidates));
+ $result = $suite->run(new DefaultReporter());
+ if (SimpleReporter::inCli()) {
+ exit($result ? 0 : 1);
+ }
+}
+
+/**
+ * Checks the current test context to see if a test has
+ * ever been run.
+ * @return boolean True if tests have run.
+ */
+function tests_have_run() {
+ if ($context = SimpleTest::getContext()) {
+ return (boolean)$context->getTest();
+ }
+ return false;
+}
+
+/**
+ * The first autorun file.
+ * @return string Filename of first autorun script.
+ */
+function initial_file() {
+ static $file = false;
+ if (! $file) {
+ $file = reset(get_included_files());
+ }
+ return $file;
+}
+
+/**
+ * Just the classes from the first autorun script. May
+ * get a few false positives, as it just does a regex based
+ * on following the word "class".
+ * @return array List of all possible classes in first
+ * autorun script.
+ */
+function classes_defined_in_initial_file() {
+ if (preg_match_all('/\bclass\s+(\w+)/i', file_get_contents(initial_file()), $matches)) {
+ return array_map('strtolower', $matches[1]);
+ }
+ return array();
+}
+
+/**
+ * Every class since the first autorun include. This
+ * is safe enough if require_once() is alwyas used.
+ * @return array Class names.
+ */
+function capture_new_classes() {
+ global $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES;
+ return array_map('strtolower', array_diff(get_declared_classes(),
+ $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES ?
+ $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES : array()));
+}
+?> \ No newline at end of file