mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 09:54:17 +02:00
filter by backend
This commit is contained in:
parent
fa7a6f9b0a
commit
df83a46e20
17 changed files with 265 additions and 38 deletions
53
test/Controllers/ControllerTest.php
Normal file
53
test/Controllers/ControllerTest.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Controllers;
|
||||
|
||||
use Demostf\API\Test\TestCase;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
use flight\util\Collection;
|
||||
|
||||
abstract class ControllerTest extends TestCase {
|
||||
/** @var string $responseData */
|
||||
private $responseData;
|
||||
|
||||
protected function getRequest(array $get = [], array $post = [], array $files = []): Request {
|
||||
/** @var Request $mock */
|
||||
$mock = $this->getMockBuilder(Request::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods([])
|
||||
->getMock();
|
||||
|
||||
$mock->query = new Collection($get);
|
||||
$mock->data = new Collection($post);
|
||||
$mock->files = new Collection($files);
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
protected function getResponse() {
|
||||
/** @var Response|\PHPUnit_Framework_MockObject_MockObject $mock */
|
||||
$mock = $this->getMockBuilder(Response::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['send'])
|
||||
->getMock();
|
||||
|
||||
$mock->expects($this->any())
|
||||
->method('send')
|
||||
->willReturnCallback(function () use ($mock) {
|
||||
$reflection = new \ReflectionClass($mock);
|
||||
$bodyProperty = $reflection->getProperty('body');
|
||||
$bodyProperty->setAccessible(true);
|
||||
|
||||
$this->responseData = $bodyProperty->getValue($mock);
|
||||
});
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
protected function getResponseData() {
|
||||
return $this->responseData;
|
||||
}
|
||||
}
|
||||
84
test/Controllers/DemoControllerTest.php
Normal file
84
test/Controllers/DemoControllerTest.php
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Controllers;
|
||||
|
||||
use Demostf\API\Controllers\DemoController;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoListProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
|
||||
class DemoControllerTest extends ControllerTest {
|
||||
/** @var DemoProvider|\PHPUnit_Framework_MockObject_MockObject $demoProvider */
|
||||
private $demoProvider;
|
||||
/** @var ChatProvider|\PHPUnit_Framework_MockObject_MockObject $chatProvider */
|
||||
private $chatProvider;
|
||||
/** @var DemoListProvider|\PHPUnit_Framework_MockObject_MockObject $demoListProvider */
|
||||
private $demoListProvider;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->demoProvider = $this->createMock(DemoProvider::class);
|
||||
$this->chatProvider = $this->createMock(ChatProvider::class);
|
||||
$this->demoListProvider = $this->createMock(DemoListProvider::class);
|
||||
}
|
||||
|
||||
public function testGetBasicList() {
|
||||
$controller = new DemoController(
|
||||
$this->getRequest(),
|
||||
$this->getResponse(),
|
||||
$this->demoProvider,
|
||||
$this->chatProvider,
|
||||
$this->demoListProvider,
|
||||
''
|
||||
);
|
||||
|
||||
$this->demoListProvider->expects($this->once())
|
||||
->method('listDemos')
|
||||
->with(1, [], 'DESC')
|
||||
->willReturn(['dummy']);
|
||||
|
||||
$controller->listDemos();
|
||||
$this->assertEquals('["dummy"]', $this->getResponseData());
|
||||
}
|
||||
|
||||
public function testGetListPageASC() {
|
||||
$controller = new DemoController(
|
||||
$this->getRequest(['page' => '3', 'order' => 'ASC']),
|
||||
$this->getResponse(),
|
||||
$this->demoProvider,
|
||||
$this->chatProvider,
|
||||
$this->demoListProvider,
|
||||
''
|
||||
);
|
||||
|
||||
$this->demoListProvider->expects($this->once())
|
||||
->method('listDemos')
|
||||
->with(3, [], 'ASC')
|
||||
->willReturn(['dummy']);
|
||||
|
||||
$controller->listDemos();
|
||||
$this->assertEquals('["dummy"]', $this->getResponseData());
|
||||
}
|
||||
|
||||
public function testListFilterBackend() {
|
||||
$controller = new DemoController(
|
||||
$this->getRequest(['backend' => 'foo']),
|
||||
$this->getResponse(),
|
||||
$this->demoProvider,
|
||||
$this->chatProvider,
|
||||
$this->demoListProvider,
|
||||
''
|
||||
);
|
||||
|
||||
$this->demoListProvider->expects($this->once())
|
||||
->method('listDemos')
|
||||
->with(1, ['backend' => 'foo'], 'DESC')
|
||||
->willReturn(['dummy']);
|
||||
|
||||
$controller->listDemos();
|
||||
$this->assertEquals('["dummy"]', $this->getResponseData());
|
||||
}
|
||||
}
|
||||
|
|
@ -153,13 +153,28 @@ class DemoListProviderTest extends TestCase {
|
|||
$this->assertEquals($id2, $list[0]->getId());
|
||||
$this->assertEquals($id1, $list[1]->getId());
|
||||
|
||||
$list = $this->demoListProvider->listDemos(1, ['players' => [$steamId1->getSteamId64(), $steamId3->getSteamId64()]]);
|
||||
$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()]]);
|
||||
$list = $this->demoListProvider->listDemos(1,
|
||||
['players' => [$steamId2->getSteamId64(), $steamId3->getSteamId64()]]);
|
||||
|
||||
$this->assertCount(0, $list);
|
||||
}
|
||||
|
||||
public function testByUploaderFilterBackend() {
|
||||
$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), 'foo1', 'bar');
|
||||
$id2 = $this->demoProvider->storeDemo($this->getDemo($userId, 'map2', 18), 'foo1', 'bar');
|
||||
$id3 = $this->demoProvider->storeDemo($this->getDemo($userId + 1, 'map1', 12), 'foo2', 'bar');
|
||||
|
||||
$list = $this->demoListProvider->listUploads($steamId->getSteamId64(), 1, ['backend' => 'foo2']);
|
||||
|
||||
$this->assertEquals($id3, $list[0]->getId());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue