Skip to content

Commit

Permalink
updated:添加简单的测试
Browse files Browse the repository at this point in the history
仅测试了 TotalRanking 的 top 方法
  • Loading branch information
zqhong committed Jun 27, 2017
1 parent da8c1b8 commit 560d8a2
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@
<directory>./src/</directory>
</whitelist>
</filter>

<php>
<env name="REDIS_SCHEME" value="tcp"/>
<env name="REDIS_HOST" value="127.0.0.1"/>
<env name="REDIS_PORT" value="6379"/>
<env name="REDIS_PASSWORD" value="secret"/>
<env name="REDIS_DATABASE" value="1"/>
</php>
</phpunit>
59 changes: 59 additions & 0 deletions src/Data/DummyDataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Zqhong\RedisRanking\Data;

use Zqhong\RedisRanking\DataSourceInterface;

class DummyDataSource implements DataSourceInterface
{
/**
* 获取数据源,每次返回 $fetchNum 条
*
* 返回格式:
* [
* {
* "id": 1,
* "member": "akira",
* "score": 100,
* "created_at": 1498469135,
* },
* {
* "id": 2,
* "member": "jian",
* "score": 87,
* "created_at": 1498469136,
* },
* ]
*
* @param integer $lastId
* @param integer $fetchNum
* @return array 无数据时,返回空数组
*/
public function get($lastId, $fetchNum)
{
if (is_null($lastId)) {
return [
[
'id' => 1,
'member' => 'akira',
'score' => 100,
'created_at' => 1498469135,
],
[
'id' => 2,
'member' => 'jian',
'score' => 87,
'created_at' => 1498469135,
],
[
'id' => 3,
'member' => 'mike',
'score' => 59,
'created_at' => 1498469135,
],
];
} else {
return [];
}
}
}
64 changes: 64 additions & 0 deletions test/TotalRankingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Zqhong\RedisRanking\Test;

use PHPUnit\Framework\TestCase;
use Predis\Client;
use Zqhong\RedisRanking\Data\DummyDataSource;
use Zqhong\RedisRanking\Ranking\TotalRanking;
use Zqhong\RedisRanking\RankingManger;

class TotalRankingTest extends TestCase
{
protected $redisClient;

/**
* @var RankingManger
*/
protected $rankingManager;

protected function setUp()
{
$redisClient = new Client([
'scheme' => getenv('REDIS_SCHEME'),
'host' => getenv('REDIS_HOST'),
'port' => getenv('REDIS_PORT'),
'password' => getenv('REDIS_PASSWORD'),
'database ' => getenv('REDIS_DATABASE'),
]);

$this->rankingManager = (new RankingManger())
->setDataSource(new DummyDataSource())
->setRankingClasses([
TotalRanking::class,
])
->setRankingName('test')
->setRedisClient($redisClient)
->init();
}

public function testTotalRankingTop()
{
$this->assertEquals([
'akira',
'jian',
'mike',
], $this->rankingManager->totalRanking->top(3, false));
}

// public function testTotalRankingRank()
// {
//
// }
//
// public function testRankingAdd()
// {
//
// }
//
// public function testRankingScore()
// {
//
// }

}

0 comments on commit 560d8a2

Please sign in to comment.