1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 09:54:17 +02:00

allow filtering by time when listing demos in api

This commit is contained in:
Robin Appelman 2020-11-25 23:04:49 +01:00
commit 0d70d93e13
5 changed files with 82 additions and 3 deletions

View file

@ -198,4 +198,33 @@ class DemoControllerTest extends ControllerTest {
['user' => 'foo2', 'time' => 2, 'message' => 'bar2'],
]);
}
public function testListFilterTime() {
$controller = $this->getController(['before' => '500', 'after' => '100']);
$this->demoListProvider->expects($this->once())
->method('listDemos')
->with(1, [
'before' => \DateTime::createFromFormat('U', '500'),
'after' => \DateTime::createFromFormat('U', '100')
], 'DESC')
->willReturn(['dummy']);
$controller->listDemos();
$this->assertResponseData(['dummy']);
}
public function testListFilterTimeInvalid() {
$controller = $this->getController(['before' => '500', 'after' => 'foobar']);
$this->demoListProvider->expects($this->once())
->method('listDemos')
->with(1, [
'before' => \DateTime::createFromFormat('U', '500')
], 'DESC')
->willReturn(['dummy']);
$controller->listDemos();
$this->assertResponseData(['dummy']);
}
}

View file

@ -31,7 +31,9 @@ class DemoListProviderTest extends TestCase {
$this->playerProvider = new PlayerProvider($this->getDatabaseConnection());
}
private function getDemo(int $uploaderId, $map = 'map', $playerCount = 18) {
private function getDemo(int $uploaderId, $map = 'map', $playerCount = 18, int $time = null) {
$time = is_null($time) ? new \DateTime() : \DateTime::createFromFormat('U', (string) $time);
return new Demo(
0,
'http://example.com',
@ -40,7 +42,7 @@ class DemoListProviderTest extends TestCase {
12,
'nick',
$map,
new \DateTime(),
$time,
'RED',
'BLUE',
1,
@ -192,4 +194,30 @@ class DemoListProviderTest extends TestCase {
$this->assertCount(1, $list);
$this->assertEquals($id2, $list[0]->getId());
}
public function testFilterTime() {
$id1 = $this->demoProvider->storeDemo($this->getDemo(1, 'map1', 18, 1000), 'foo', 'bar');
$id2 = $this->demoProvider->storeDemo($this->getDemo(1, 'map2', 18, 1500), 'foo', 'bar');
$id3 = $this->demoProvider->storeDemo($this->getDemo(1, 'map1', 18, 2000), 'foo', 'bar');
$date1 = \DateTime::createFromFormat('U', '1200');
$date2 = \DateTime::createFromFormat('U', '1700');
$list = $this->demoListProvider->listDemos(1, ['before' => $date2]);
$this->assertCount(2, $list);
$this->assertEquals($id2, $list[0]->getId());
$this->assertEquals($id1, $list[1]->getId());
$list = $this->demoListProvider->listDemos(1, ['after' => $date1]);
$this->assertCount(2, $list);
$this->assertEquals($id3, $list[0]->getId());
$this->assertEquals($id2, $list[1]->getId());
$list = $this->demoListProvider->listDemos(1, ['before' => $date2, 'after' => $date1]);
$this->assertCount(1, $list);
$this->assertEquals($id2, $list[0]->getId());
}
}