mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
demo list tests and fixes
This commit is contained in:
parent
f0065d8e86
commit
835bad1901
5 changed files with 175 additions and 28 deletions
|
|
@ -31,9 +31,14 @@ class DemoListProvider extends BaseProvider {
|
|||
$demos = $this->db->demo()->where('id', $demoIds)
|
||||
->where($where)
|
||||
->orderBy('id', 'DESC');
|
||||
return $this->formatList($demos);
|
||||
return $this->formatList($demos->fetchAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @param array $where
|
||||
* @return Demo[]
|
||||
*/
|
||||
public function listDemos(int $page, array $where = []) {
|
||||
if (isset($where['players']) and is_array($where['players']) and count($where['players']) > 0) {
|
||||
return $this->listProfile($page, $where);
|
||||
|
|
@ -50,18 +55,21 @@ class DemoListProvider extends BaseProvider {
|
|||
$query->where($query->expr()->eq('map', $query->createNamedParameter($where['map'])));
|
||||
}
|
||||
if (isset($where['playerCount'])) {
|
||||
$query->where($query->expr()->in('playerCount', $query->createNamedParameter($where['playerCount'], Connection::PARAM_INT_ARRAY)));
|
||||
$query->where($query->expr()->in('"playerCount"', $query->createNamedParameter($where['playerCount'], Connection::PARAM_INT_ARRAY)));
|
||||
}
|
||||
if (isset($where['uploader'])) {
|
||||
$query->where($query->expr()->in('uploader', $query->createNamedParameter($where['uploader'], \PDO::PARAM_INT)));
|
||||
}
|
||||
$query->orderBy('d.id', 'DESC')
|
||||
->setMaxResults(50)
|
||||
->setFirstResult($offset);
|
||||
|
||||
$demos = $query->execute()->fetchAll();
|
||||
$demos = $query->execute()->fetchAll(\PDO::FETCH_ASSOC);
|
||||
return $this->formatList($demos);
|
||||
}
|
||||
|
||||
protected function formatList(array $rows) {
|
||||
return array_map(function (array $row) {
|
||||
return array_map(function ($row) {
|
||||
return Demo::fromRow($row);
|
||||
}, $rows);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,13 +11,9 @@ class InfoProvider extends BaseProvider {
|
|||
$demoCount = $this->db->demo()->count();
|
||||
$playerCount = $this->db->user()->count();
|
||||
|
||||
$sql = 'SELECT count(user_id) FROM players GROUP BY user_id';
|
||||
$result = $this->query($sql);
|
||||
|
||||
return [
|
||||
'demos' => $demoCount,
|
||||
'players' => $playerCount,
|
||||
'uploaders' => $result->fetchColumn()
|
||||
'players' => $playerCount
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Demo\ChatMessage;
|
||||
|
|
|
|||
161
tests/Providers/DemoListProviderTest.php
Normal file
161
tests/Providers/DemoListProviderTest.php
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Data\Player;
|
||||
use Demostf\API\Demo\Demo;
|
||||
use Demostf\API\Providers\DemoListProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
use Demostf\API\Providers\PlayerProvider;
|
||||
use Demostf\API\Providers\UserProvider;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class DemoListProviderTest extends TestCase {
|
||||
/** @var DemoListProvider */
|
||||
private $demoListProvider;
|
||||
/** @var DemoProvider */
|
||||
private $demoProvider;
|
||||
/** @var PlayerProvider */
|
||||
private $playerProvider;
|
||||
/** @var UserProvider */
|
||||
private $userProvider;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->demoListProvider = new DemoListProvider($this->getDatabaseConnection());
|
||||
$this->demoProvider = new DemoProvider($this->getDatabaseConnection());
|
||||
$this->playerProvider = new PlayerProvider($this->getDatabaseConnection());
|
||||
$this->userProvider = new UserProvider($this->getDatabaseConnection(), $this->getRandomGenerator());
|
||||
}
|
||||
|
||||
private function getDemo(int $uploaderId, $map = 'map', $playerCount = 18) {
|
||||
return new Demo(
|
||||
0,
|
||||
'http://example.com',
|
||||
'name',
|
||||
'server',
|
||||
12,
|
||||
'nick',
|
||||
$map,
|
||||
new \DateTime(),
|
||||
'RED',
|
||||
'BLUE',
|
||||
1,
|
||||
2,
|
||||
$playerCount,
|
||||
$uploaderId,
|
||||
'hash'
|
||||
);
|
||||
}
|
||||
|
||||
public function testListEmpty() {
|
||||
$this->assertEquals([], $this->demoListProvider->listDemos(1));
|
||||
}
|
||||
|
||||
public function testListSimple() {
|
||||
$id1 = $this->demoProvider->storeDemo($this->getDemo(1), 'foo', 'bar');
|
||||
$id2 = $this->demoProvider->storeDemo($this->getDemo(1), 'foo', 'bar');
|
||||
$id3 = $this->demoProvider->storeDemo($this->getDemo(1), 'foo', 'bar');
|
||||
|
||||
$list = $this->demoListProvider->listDemos(1);
|
||||
$this->assertCount(3, $list);
|
||||
|
||||
$this->assertEquals($id3, $list[0]->getId());
|
||||
$this->assertEquals($id2, $list[1]->getId());
|
||||
$this->assertEquals($id1, $list[2]->getId());
|
||||
}
|
||||
|
||||
public function testFilterMap() {
|
||||
$id1 = $this->demoProvider->storeDemo($this->getDemo(1, 'map1'), 'foo', 'bar');
|
||||
$id2 = $this->demoProvider->storeDemo($this->getDemo(1, 'map2'), 'foo', 'bar');
|
||||
$id3 = $this->demoProvider->storeDemo($this->getDemo(1, 'map1'), 'foo', 'bar');
|
||||
|
||||
$list = $this->demoListProvider->listDemos(1, ['map' => 'map1']);
|
||||
$this->assertCount(2, $list);
|
||||
|
||||
$this->assertEquals($id3, $list[0]->getId());
|
||||
$this->assertEquals($id1, $list[1]->getId());
|
||||
}
|
||||
|
||||
public function testFilterPlayerCount() {
|
||||
$id1 = $this->demoProvider->storeDemo($this->getDemo(1, 'map1', 17), 'foo', 'bar');
|
||||
$id2 = $this->demoProvider->storeDemo($this->getDemo(1, 'map2', 18), 'foo', 'bar');
|
||||
$id3 = $this->demoProvider->storeDemo($this->getDemo(1, 'map1', 12), 'foo', 'bar');
|
||||
|
||||
$list = $this->demoListProvider->listDemos(1, ['playerCount' => [17, 18, 19]]);
|
||||
$this->assertCount(2, $list);
|
||||
|
||||
$this->assertEquals($id2, $list[0]->getId());
|
||||
$this->assertEquals($id1, $list[1]->getId());
|
||||
}
|
||||
|
||||
public function testByUploader() {
|
||||
$steamId = $this->getSteamId('12345', 'bar');
|
||||
$this->userProvider->store($steamId);
|
||||
$userId = $this->userProvider->get($steamId->getSteamId64())->getId();
|
||||
$id1 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map1', 17), 'foo', 'bar');
|
||||
$id2 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map2', 18), 'foo', 'bar');
|
||||
$id3 = $this->demoProvider->storeDemo($this->getDemo($userId + 1, 'map1', 12), 'foo', 'bar');
|
||||
|
||||
$list = $this->demoListProvider->listUploads($steamId->getSteamId64(), 1);
|
||||
|
||||
$this->assertEquals($id2, $list[0]->getId());
|
||||
$this->assertEquals($id1, $list[1]->getId());
|
||||
}
|
||||
|
||||
public function testByUploaderFilter() {
|
||||
$steamId = $this->getSteamId('12345', 'bar');
|
||||
$this->userProvider->store($steamId);
|
||||
$userId = $this->userProvider->get($steamId->getSteamId64())->getId();
|
||||
$id1 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map1', 12), 'foo', 'bar');
|
||||
$id2 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map2', 18), 'foo', 'bar');
|
||||
$id3 = $this->demoProvider->storeDemo($this->getDemo($userId + 1, 'map1', 12), 'foo', 'bar');
|
||||
|
||||
$list = $this->demoListProvider->listUploads($steamId->getSteamId64(), 1, ['playerCount' => [17, 18, 19]]);
|
||||
|
||||
$this->assertEquals($id2, $list[0]->getId());
|
||||
}
|
||||
|
||||
private function addPlayer($demoId, $userId) {
|
||||
$player = new Player(0, $demoId, 0, $userId, 'foo', 'red', 'scout');
|
||||
$this->playerProvider->store($player);
|
||||
}
|
||||
|
||||
public function testFilterPlayer() {
|
||||
$steamId1 = $this->getSteamId('12345', 'bar1');
|
||||
$steamId2 = $this->getSteamId('22345', 'bar2');
|
||||
$steamId3 = $this->getSteamId('32345', 'bar3');
|
||||
$this->userProvider->store($steamId1);
|
||||
$this->userProvider->store($steamId2);
|
||||
$this->userProvider->store($steamId3);
|
||||
$userId1 = $this->userProvider->get($steamId1->getSteamId64())->getId();
|
||||
$userId2 = $this->userProvider->get($steamId2->getSteamId64())->getId();
|
||||
$userId3 = $this->userProvider->get($steamId3->getSteamId64())->getId();
|
||||
|
||||
$id1 = $this->demoProvider->storeDemo($this->getDemo(1, 'map1', 17), 'foo', 'bar');
|
||||
$id2 = $this->demoProvider->storeDemo($this->getDemo(1, 'map2', 18), 'foo', 'bar');
|
||||
$id3 = $this->demoProvider->storeDemo($this->getDemo(1, 'map1', 12), 'foo', 'bar');
|
||||
|
||||
$this->addPlayer($id1, $userId1);
|
||||
$this->addPlayer($id1, $userId2);
|
||||
$this->addPlayer($id2, $userId1);
|
||||
$this->addPlayer($id2, $userId3);
|
||||
$this->addPlayer($id3, $userId3);
|
||||
|
||||
$list = $this->demoListProvider->listDemos(1, ['players' => [$steamId1->getSteamId64()]]);
|
||||
|
||||
$this->assertCount(2, $list);
|
||||
$this->assertEquals($id2, $list[0]->getId());
|
||||
$this->assertEquals($id1, $list[1]->getId());
|
||||
|
||||
$list = $this->demoListProvider->listDemos(1, ['players' => [$steamId1->getSteamId64(), $steamId3->getSteamId64()]]);
|
||||
|
||||
$this->assertCount(1, $list);
|
||||
$this->assertEquals($id2, $list[0]->getId());
|
||||
|
||||
$list = $this->demoListProvider->listDemos(1, ['players' => [$steamId2->getSteamId64(), $steamId3->getSteamId64()]]);
|
||||
|
||||
$this->assertCount(0, $list);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
<?php declare(strict_types = 1);
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Providers\InfoProvider;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class InfoProviderTest extends TestCase {
|
||||
/** @var InfoProvider */
|
||||
private $provider;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->provider = new InfoProvider($this->getDatabaseConnection());
|
||||
}
|
||||
|
||||
public function testGetStats() {
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue