source: trunk/server/www/vendors/simpletest/autorun.php @ 6

Last change on this file since 6 was 6, checked in by sander, 11 years ago

Added SimpleTest? test framework

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