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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
<?php defined('PHPREDIS_TESTRUN') or die("Use TestRedis.php to run tests!\n");
// phpunit is such a pain to install, we're going with pure-PHP here.
class TestSuite {
/* Host the tests will use */
private $str_host;
private static $_boo_colorize = false;
private static $BOLD_ON = "\033[1m";
private static $BOLD_OFF = "\033[0m";
private static $BLACK = "\033[0;30m";
private static $DARKGRAY = "\033[1;30m";
private static $BLUE = "\033[0;34m";
private static $PURPLE = "\033[0;35m";
private static $GREEN = "\033[0;32m";
private static $YELLOW = "\033[0;33m";
private static $RED = "\033[0;31m";
public static $errors = array();
public static $warnings = array();
public function __construct($str_host) {
$this->str_host = $str_host;
}
public function getHost() { return $this->str_host; }
/**
* Returns the fully qualified host path,
* which may be used directly for php.ini parameters like session.save_path
*
* @return null|string
*/
protected function getFullHostPath()
{
return $this->str_host
? 'tcp://' . $this->str_host . ':6379'
: null;
}
public static function make_bold($str_msg) {
return self::$_boo_colorize
? self::$BOLD_ON . $str_msg . self::$BOLD_OFF
: $str_msg;
}
public static function make_success($str_msg) {
return self::$_boo_colorize
? self::$GREEN . $str_msg . self::$BOLD_OFF
: $str_msg;
}
public static function make_fail($str_msg) {
return self::$_boo_colorize
? self::$RED . $str_msg . self::$BOLD_OFF
: $str_msg;
}
public static function make_warning($str_msg) {
return self::$_boo_colorize
? self::$YELLOW . $str_msg . self::$BOLD_OFF
: $str_msg;
}
protected function assertFalse($bool) {
if(!$bool)
return true;
$bt = debug_backtrace(false);
self::$errors []= sprintf("Assertion failed: %s:%d (%s)\n",
$bt[0]["file"], $bt[0]["line"], $bt[1]["function"]);
return false;
}
protected function assertTrue($bool) {
if($bool)
return true;
$bt = debug_backtrace(false);
self::$errors []= sprintf("Assertion failed: %s:%d (%s)\n",
$bt[0]["file"], $bt[0]["line"], $bt[1]["function"]);
return false;
}
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 assertPatternMatch($str_test, $str_regex) {
if (preg_match($str_regex, $str_test))
return;
$bt = debug_backtrace(false);
self::$errors []= sprintf("Assertion failed ('%s' doesnt match '%s'): %s:%d (%s)\n",
$str_test, $str_regex, $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);
}
private static function getMaxTestLen($arr_methods, $str_limit) {
$i_result = 0;
$str_limit = strtolower($str_limit);
foreach ($arr_methods as $obj_method) {
$str_name = strtolower($obj_method->name);
if (substr($str_name, 0, 4) != 'test')
continue;
if ($str_limit && !strstr($str_name, $str_limit))
continue;
if (strlen($str_name) > $i_result) {
$i_result = strlen($str_name);
}
}
return $i_result;
}
/* Flag colorization */
public static function flagColorization($boo_override) {
self::$_boo_colorize = $boo_override && function_exists('posix_isatty') &&
posix_isatty(STDOUT);
}
public static function run($className, $str_limit = NULL, $str_host = NULL) {
/* Lowercase our limit arg if we're passed one */
$str_limit = $str_limit ? strtolower($str_limit) : $str_limit;
$rc = new ReflectionClass($className);
$methods = $rc->GetMethods(ReflectionMethod::IS_PUBLIC);
$i_max_len = self::getMaxTestLen($methods, $str_limit);
foreach($methods as $m) {
$name = $m->name;
if(substr($name, 0, 4) !== 'test')
continue;
/* If we're trying to limit to a specific test and can't match the
* substring, skip */
if ($str_limit && strstr(strtolower($name), $str_limit)===FALSE) {
continue;
}
$str_out_name = str_pad($name, $i_max_len + 1);
echo self::make_bold($str_out_name);
$count = count($className::$errors);
$rt = new $className($str_host);
try {
$rt->setUp();
$rt->$name();
if ($count === count($className::$errors)) {
$str_msg = self::make_success('PASSED');
} else {
$str_msg = self::make_fail('FAILED');
}
//echo ($count === count($className::$errors)) ? "." : "F";
} catch (Exception $e) {
if ($e instanceof RedisException) {
$className::$errors[] = "Uncaught exception '".$e->getMessage()."' ($name)\n";
$str_msg = self::make_fail('FAILED');
} else {
$str_msg = self::make_warning('SKIPPED');
}
}
echo "[" . $str_msg . "]\n";
}
echo "\n";
echo implode('', $className::$warnings) . "\n";
if(empty($className::$errors)) {
echo "All tests passed. \o/\n";
return 0;
}
echo implode('', $className::$errors);
return 1;
}
}
?>
|