hxp
2025-06-09 6c3f6335c70859ded94a1ad8d218acb0ac34239c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
 
// phpunit is such a pain to install, we're going with pure-PHP here.
class TestSuite {
    public static $errors = array();
    public static $warnings = array();
 
    protected function assertFalse($bool) {
        $this->assertTrue(!$bool);
    }
 
    protected function assertTrue($bool) {
        if($bool)
            return;
 
        $bt = debug_backtrace(false);
        self::$errors []= sprintf("Assertion failed: %s:%d (%s)\n",
            $bt[0]["file"], $bt[0]["line"], $bt[1]["function"]);
    }
 
    protected function assertLess($a, $b) {
        if($a < $b) 
            return;
 
        $bt = debug_backtrace(false);
        self::$errors[] = sprintf("Assertion failed (%s >= %s): %s: %d (%s\n",
            print_r($a, true), print_r($b, true),
            $bt[0]["file"], $bt[0]["line"], $bt[1]["function"]);
    }
 
    protected function assertEquals($a, $b) {
        if($a === $b)
            return;
 
        $bt = debug_backtrace(false);
        self::$errors []= sprintf("Assertion failed (%s !== %s): %s:%d (%s)\n",
            print_r($a, true), print_r($b, true),
            $bt[0]["file"], $bt[0]["line"], $bt[1]["function"]);
    }
 
    protected function markTestSkipped($msg='') {
        $bt = debug_backtrace(false);
        self::$warnings []= sprintf("Skipped test: %s:%d (%s) %s\n",
            $bt[0]["file"], $bt[0]["line"], $bt[1]["function"], $msg);
 
        throw new Exception($msg);
    }
 
    public static function run($className, $str_limit) {
        $rc = new ReflectionClass($className);
        $methods = $rc->GetMethods(ReflectionMethod::IS_PUBLIC);
 
        if ($str_limit) {
            echo "Limiting to tests with the substring: '$str_limit'\n";
        }
 
        foreach($methods as $m) {
            $name = $m->name;
            if(substr($name, 0, 4) !== 'test')
                continue;
 
            /* If TestRedis.php was envoked with an argument, do a simple
             * match against the routine.  Useful to limit to one test */
            if ($str_limit && strpos(strtolower($name),strtolower($str_limit))===false)
                continue;
 
            $count = count($className::$errors);
            $rt = new $className;
            try {
                $rt->setUp();
                $rt->$name();
                echo ($count === count($className::$errors)) ? "." : "F";
            } catch (Exception $e) {
                if ($e instanceof RedisException) {
                    $className::$errors[] = "Uncaught exception '".$e->getMessage()."' ($name)\n";
                    echo 'F';
                } else {
                    echo 'S';
                }
            }
        }
        echo "\n";
        echo implode('', $className::$warnings);
 
        if(empty($className::$errors)) {
            echo "All tests passed.\n";
            return 0;
        }
 
        echo implode('', $className::$errors);
        return 1;
    }
}
 
?>