Skip to content

Commit

Permalink
添加 dsIncrby 单元测试,支持 --filter 命令行参数
Browse files Browse the repository at this point in the history
  • Loading branch information
Ye Wenbin committed Dec 17, 2013
1 parent ab99215 commit e618d76
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
29 changes: 29 additions & 0 deletions tests/TestRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public function setUp()
{
$this->redis = $this->newInstance();
$info = $this->redis->info();
if ( isset($info['redis_version']) ) {
if ( strpos($info['redis_version'], ', ') !== false ) {
foreach ( explode(', ', $info['redis_version']) as $part ) {
$pair = explode(':', $part);
if ( count($pair) == 2 ) {
$info[$pair[0].'_version'] = $pair[1];
}
}
}
$this->leveldb_version = isset($info['leveldb_version']) ? $info['leveldb_version'] : '0.0.0';
}
$this->version = (isset($info['redis_version'])?$info['redis_version']:'0.0.0');
}

Expand Down Expand Up @@ -4356,6 +4367,24 @@ public function testIntrospection() {
$this->assertTrue($this->redis->getAuth() === self::AUTH);
}

private function isLeveldbEnabled()
{
return isset($this->leveldb_version) && version_compare($this->leveldb_version, '1', 'ge');
}

public function testDsIncrby()
{
if ( !$this->isLeveldbEnabled() ) {
$this->markTestSkipped();
}
$this->redis->dsDel('key');
$this->redis->dsIncrby('key', 1);
$this->assertEquals(1, (int)$this->redis->dsGet('key'));
$this->redis->dsIncrby('key', 2);
$this->assertEquals(3, (int)$this->redis->dsGet('key'));
$this->redis->dsIncrby('key', -1);
$this->assertEquals(2, (int)$this->redis->dsGet('key'));
}
}

exit(TestSuite::run("Redis_Test"));
Expand Down
39 changes: 37 additions & 2 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ protected function markTestSkipped($msg='') {
}

public static function run($className) {

$runner = new TestRunner;
$rc = new ReflectionClass($className);
$methods = $rc->GetMethods(ReflectionMethod::IS_PUBLIC);

foreach($methods as $m) {

$name = $m->name;
if(substr($name, 0, 4) !== 'test')
if(!$runner->isValidTest($name))
continue;

$count = count($className::$errors);
Expand Down Expand Up @@ -76,4 +76,39 @@ public static function run($className) {
}
}

class TestRunner
{
private $filter;

public function __construct()
{
$argv = $_SERVER['argv'];
foreach ( $argv as $i => $opt ) {
if ( $opt == '--filter' && isset($argv[$i+1]) ) {
$this->setFilter($argv[$i+1]);
}
}
}

public function getFilter()
{
return $this->filter;
}

public function setFilter($filter)
{
$this->filter = $filter;
}

public function isValidTest($name)
{
if ( substr($name, 0, 4) !== 'test' ) {
return false;
}
if ( isset($this->filter) ) {
return preg_match($this->filter, $name);
}
return true;
}
}
?>

0 comments on commit e618d76

Please sign in to comment.