-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
仅测试了 TotalRanking 的 top 方法
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
// { | ||
// | ||
// } | ||
|
||
} |