mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 09:54:17 +02:00
add basic api tests
This commit is contained in:
parent
64b0aff075
commit
e00e6ece5f
30 changed files with 350 additions and 17 deletions
27
test/Data/DemoPlayerTest.php
Normal file
27
test/Data/DemoPlayerTest.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Data;
|
||||
|
||||
use Demostf\API\Data\DemoPlayer;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class DemoPlayerTest extends TestCase {
|
||||
public function testParseSerialize() {
|
||||
$data = [
|
||||
'id' => 1,
|
||||
'user_id' => 2,
|
||||
'name' => 'foo',
|
||||
'team' => 'red',
|
||||
'class' => 'sniper',
|
||||
'steamid' => 'asd',
|
||||
'avatar' => 'asd.png',
|
||||
'kills' => 5,
|
||||
'assists' => 3,
|
||||
'deaths' => 7
|
||||
];
|
||||
|
||||
$demoPlayer = DemoPlayer::fromRow($data);
|
||||
|
||||
$this->assertEquals($data, $demoPlayer->jsonSerialize());
|
||||
}
|
||||
}
|
||||
112
test/Demo/DemoSaverTest.php
Normal file
112
test/Demo/DemoSaverTest.php
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Demo;
|
||||
|
||||
use Demostf\API\Data\ParsedDemo;
|
||||
use Demostf\API\Data\ParsedKill;
|
||||
use Demostf\API\Data\ParsedPlayer;
|
||||
use Demostf\API\Data\StoredDemo;
|
||||
use Demostf\API\Data\Upload;
|
||||
use Demostf\API\Demo\ChatMessage;
|
||||
use Demostf\API\Demo\DemoSaver;
|
||||
use Demostf\API\Demo\Header;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
use Demostf\API\Providers\KillProvider;
|
||||
use Demostf\API\Providers\PlayerProvider;
|
||||
use Demostf\API\Providers\UserProvider;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class DemoSaverTest extends TestCase {
|
||||
public function testSave() {
|
||||
|
||||
$steamId1 = $this->getSteamId('1234567', 'user1');
|
||||
$steamId2 = $this->getSteamId('2345678', 'user2');
|
||||
|
||||
$userProvider = new UserProvider($this->getDatabaseConnection(), $this->getRandomGenerator());
|
||||
$demoProvider = new DemoProvider($this->getDatabaseConnection());
|
||||
$chatProvider = new ChatProvider($this->getDatabaseConnection());
|
||||
|
||||
$userProvider->store($steamId1);
|
||||
$userProvider->store($steamId2);
|
||||
|
||||
$upload = new Upload(
|
||||
'foodemo',
|
||||
'DER',
|
||||
'ULB',
|
||||
$userProvider->getUserId('2345678'),
|
||||
'securehash'
|
||||
);
|
||||
|
||||
$header = new Header(
|
||||
'HL2DEMO',
|
||||
12,
|
||||
13,
|
||||
'My Server',
|
||||
'STV',
|
||||
'pl_badwater',
|
||||
'tf',
|
||||
60,
|
||||
60 * 60,
|
||||
2,
|
||||
1
|
||||
);
|
||||
|
||||
$parsed = new ParsedDemo(2, 3,
|
||||
[
|
||||
new ChatMessage('user1', 12, 'msg1'),
|
||||
new ChatMessage('user2', 13, 'msg2')
|
||||
],
|
||||
[
|
||||
new ParsedPlayer('user1', 1, '1234567', 'red', 'scout'),
|
||||
new ParsedPlayer('user2', 2, '2345678', 'blue', 'soldier'),
|
||||
],
|
||||
[
|
||||
new ParsedKill(1, 0, 2, 'pan'),
|
||||
new ParsedKill(1, 2, 2, 'pan'),
|
||||
new ParsedKill(2, 0, 1, 'pan'),
|
||||
]
|
||||
);
|
||||
|
||||
$saver = new DemoSaver(
|
||||
new KillProvider($this->getDatabaseConnection()),
|
||||
new PlayerProvider($this->getDatabaseConnection()),
|
||||
$chatProvider,
|
||||
$userProvider,
|
||||
$demoProvider
|
||||
);
|
||||
|
||||
$storedDemo = new StoredDemo('http://example.com/foo', 'foo', 'example');
|
||||
|
||||
$demoId = $saver->saveDemo($parsed, $header, $storedDemo, $upload);
|
||||
|
||||
$retrievedDemo = $demoProvider->get($demoId, true);
|
||||
|
||||
$this->assertEquals(2, $retrievedDemo->getPlayerCount());
|
||||
$this->assertEquals(2, $retrievedDemo->getRedScore());
|
||||
$this->assertEquals(3, $retrievedDemo->getBlueScore());
|
||||
$this->assertEquals('DER', $retrievedDemo->getRed());
|
||||
$this->assertEquals('ULB', $retrievedDemo->getBlue());
|
||||
|
||||
$this->assertEquals('user2', $retrievedDemo->getUploaderUser()->getName());
|
||||
|
||||
$this->assertEquals('user2', $retrievedDemo->getPlayers()[0]->getName());
|
||||
$this->assertEquals(1, $retrievedDemo->getPlayers()[0]->getKills());
|
||||
$this->assertEquals(1, $retrievedDemo->getPlayers()[0]->getAssists());
|
||||
$this->assertEquals(2, $retrievedDemo->getPlayers()[0]->getDeaths());
|
||||
$this->assertEquals('blue', $retrievedDemo->getPlayers()[0]->getTeam());
|
||||
$this->assertEquals('soldier', $retrievedDemo->getPlayers()[0]->getClass());
|
||||
|
||||
$this->assertEquals('user1', $retrievedDemo->getPlayers()[1]->getName());
|
||||
$this->assertEquals(2, $retrievedDemo->getPlayers()[1]->getKills());
|
||||
$this->assertEquals(0, $retrievedDemo->getPlayers()[1]->getAssists());
|
||||
$this->assertEquals(1, $retrievedDemo->getPlayers()[1]->getDeaths());
|
||||
$this->assertEquals('red', $retrievedDemo->getPlayers()[1]->getTeam());
|
||||
$this->assertEquals('scout', $retrievedDemo->getPlayers()[1]->getClass());
|
||||
|
||||
$this->assertEquals([
|
||||
new ChatMessage('user1', 12, 'msg1'),
|
||||
new ChatMessage('user2', 13, 'msg2')
|
||||
], $chatProvider->getChat($demoId));
|
||||
}
|
||||
}
|
||||
31
test/Demo/DemoStoreTest.php
Normal file
31
test/Demo/DemoStoreTest.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Demo;
|
||||
|
||||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class DemoStoreTest extends TestCase {
|
||||
public function testStore() {
|
||||
$targetDir = tempnam(sys_get_temp_dir(), 'dummy_target_');
|
||||
unlink($targetDir);
|
||||
mkdir($targetDir);
|
||||
|
||||
$demoStore = new DemoStore($targetDir, 'static.example.com');
|
||||
|
||||
$file = tempnam(sys_get_temp_dir(), 'dummy_');
|
||||
file_put_contents($file, 'foobar');
|
||||
|
||||
$storedDemo = $demoStore->store($file, 'foodemo.dem');
|
||||
|
||||
$this->assertStringEndsWith('/foodemo.dem', $storedDemo->getUrl());
|
||||
$this->assertStringStartsWith('https://static.example.com/', $storedDemo->getUrl());
|
||||
$this->assertEquals('static', $storedDemo->getBackend());
|
||||
|
||||
$this->assertStringEqualsFile($storedDemo->getPath(), 'foobar');
|
||||
unlink($storedDemo->getPath());
|
||||
rmdir(dirname($storedDemo->getPath()));
|
||||
rmdir(dirname($storedDemo->getPath(), 2));
|
||||
rmdir($targetDir);
|
||||
}
|
||||
}
|
||||
66
test/Demo/HeaderParserTest.php
Normal file
66
test/Demo/HeaderParserTest.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Demo;
|
||||
|
||||
use Demostf\API\Demo\Header;
|
||||
use Demostf\API\Demo\HeaderParser;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class HeaderParserTest extends TestCase {
|
||||
public function testParseFile() {
|
||||
$parser = new HeaderParser();
|
||||
|
||||
$expected = new Header(
|
||||
'HL2DEMO',
|
||||
3,
|
||||
24,
|
||||
'UGC Highlander Match',
|
||||
'SourceTV Demo',
|
||||
'koth_product_rc8',
|
||||
'tf',
|
||||
778.4849853515625,
|
||||
51899,
|
||||
25703,
|
||||
818263
|
||||
);
|
||||
$parsed = $parser->parseHeader(__DIR__ . '/../data/product.dem');
|
||||
|
||||
$this->assertEquals($expected->getServer(), $parsed->getServer());
|
||||
$this->assertEquals($expected->getDuration(), $parsed->getDuration());
|
||||
$this->assertEquals($expected->getTicks(), $parsed->getTicks());
|
||||
$this->assertEquals($expected->getFrames(), $parsed->getFrames());
|
||||
$this->assertEquals($expected->getGame(), $parsed->getGame());
|
||||
$this->assertEquals($expected->getMap(), $parsed->getMap());
|
||||
$this->assertEquals($expected->getNick(), $parsed->getNick());
|
||||
$this->assertEquals($expected->getProtocol(), $parsed->getProtocol());
|
||||
$this->assertEquals($expected->getSigon(), $parsed->getSigon());
|
||||
$this->assertEquals($expected->getType(), $parsed->getType());
|
||||
$this->assertEquals($expected->getVersion(), $parsed->getVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Not an HL2 demo
|
||||
*/
|
||||
public function testNonDemoShort() {
|
||||
$parser = new HeaderParser();
|
||||
$parser->parseString("short");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Not an HL2 demo
|
||||
*/
|
||||
public function testNonDemoLong() {
|
||||
$parser = new HeaderParser();
|
||||
$parser->parseHeader(__FILE__);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testNonExisting() {
|
||||
$parser = new HeaderParser();
|
||||
$parser->parseHeader('/non/existing');
|
||||
}
|
||||
}
|
||||
93
test/Demo/ParserTest.php
Normal file
93
test/Demo/ParserTest.php
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Demo;
|
||||
|
||||
use Demostf\API\Demo\Parser;
|
||||
use Demostf\API\Demo\RawParser;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class ParserTest extends TestCase {
|
||||
/** @var RawParser|\PHPUnit_Framework_MockObject_MockObject */
|
||||
private $rawParser;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->rawParser = $this->getMockBuilder(RawParser::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->rawParser->expects($this->any())
|
||||
->method('parse')
|
||||
->will($this->returnCallback(function ($path) {
|
||||
$jsonPath = str_replace('.dem', '-raw.json', $path);
|
||||
return json_decode(file_get_contents($jsonPath), true);
|
||||
}));
|
||||
}
|
||||
|
||||
public function testAnalyse() {
|
||||
$parser = new Parser($this->rawParser);
|
||||
|
||||
$result = $parser->analyse(__DIR__ . '/../data/product.dem');
|
||||
|
||||
$expectedRaw = json_decode(file_get_contents(__DIR__ . '/../data/product-analyse.json'), true);
|
||||
|
||||
$expectedChat = $expectedRaw['chat'];
|
||||
$this->assertCount(count($expectedChat), $result->getChat());
|
||||
$this->assertEquals($expectedChat[0]['text'], $result->getChat()[0]->getMessage());
|
||||
$this->assertEquals($expectedChat[0]['time'], $result->getChat()[0]->getTime());
|
||||
$this->assertEquals($expectedChat[0]['from'], $result->getChat()[0]->getUser());
|
||||
|
||||
$this->assertEquals($expectedRaw['score']['red'], $result->getRedScore());
|
||||
$this->assertEquals($expectedRaw['score']['blue'], $result->getBlueScore());
|
||||
|
||||
$expectedPlayers = $expectedRaw['players'];
|
||||
$this->assertCount(count($expectedPlayers), $result->getPlayers());
|
||||
$this->assertEquals($expectedPlayers[0]['name'], $result->getPlayers()[0]->getName());
|
||||
$this->assertEquals($expectedPlayers[0]['demo_user_id'], $result->getPlayers()[0]->getDemoUserId());
|
||||
$this->assertEquals($expectedPlayers[0]['team'], $result->getPlayers()[0]->getTeam());
|
||||
$this->assertEquals($expectedPlayers[0]['class'], $result->getPlayers()[0]->getClass());
|
||||
$this->assertEquals($parser->convertSteamIdToCommunityId($expectedPlayers[0]['steam_id']), $result->getPlayers()[0]->getSteamId());
|
||||
|
||||
$expectedKills = $expectedRaw['kills'];
|
||||
$this->assertCount(count($expectedKills), $result->getKills());
|
||||
$this->assertEquals((int)$expectedKills[0]['killer'], $result->getKills()[0]->getAttackerDemoId());
|
||||
$this->assertEquals((int)$expectedKills[0]['assister'], $result->getKills()[0]->getAssisterDemoId());
|
||||
$this->assertEquals((int)$expectedKills[0]['victim'], $result->getKills()[0]->getVictimDemoId());
|
||||
$this->assertEquals($expectedKills[0]['weapon'], $result->getKills()[0]->getWeapon());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testFailedParse() {
|
||||
/** @var RawParser|\PHPUnit_Framework_MockObject_MockObject $rawParser */
|
||||
$rawParser = $this->getMockBuilder(RawParser::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$parser = new Parser($rawParser);
|
||||
|
||||
$rawParser->expects($this->any())
|
||||
->method('parse')
|
||||
->willReturn(null);
|
||||
|
||||
$parser->analyse('foo');
|
||||
}
|
||||
|
||||
public function testConvertSteamIdToCommunityId() {
|
||||
$parser = new Parser($this->rawParser);
|
||||
|
||||
$steamId64 = $parser->convertSteamIdToCommunityId('STEAM_0:0:12345');
|
||||
$this->assertEquals('76561197960290418', $steamId64);
|
||||
}
|
||||
|
||||
public function testConvertUIdToCommunityId() {
|
||||
$parser = new Parser($this->rawParser);
|
||||
|
||||
$steamId64 = $parser->convertSteamIdToCommunityId('[U:1:12345]');
|
||||
$this->assertEquals('76561197960278073', $steamId64);
|
||||
$steamId64 = $parser->convertSteamIdToCommunityId('[U:1:39743963]');
|
||||
$this->assertEquals('76561198000009691', $steamId64);
|
||||
}
|
||||
}
|
||||
252
test/Integration/Tests.js
Normal file
252
test/Integration/Tests.js
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
/**
|
||||
* parser server
|
||||
*/
|
||||
var DemoParser = require('tf2-demo');
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
var url = require('url');
|
||||
var https = require('https');
|
||||
var http = require('http');
|
||||
|
||||
app.set('port', (process.env.PORT || 80));
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
|
||||
app.get('/', function (request, response) {
|
||||
response.send('Hello World!');
|
||||
});
|
||||
|
||||
function handleDataStream(stream, cb) {
|
||||
var buffers = [];
|
||||
stream.on('data', function (buffer) {
|
||||
buffers.push(buffer);
|
||||
});
|
||||
stream.on('end', function () {
|
||||
try {
|
||||
var buffer = Buffer.concat(buffers);
|
||||
var demo = DemoParser.Demo.fromNodeBuffer(buffer);
|
||||
var parser = demo.getParser(true);
|
||||
var header = parser.readHeader();
|
||||
var match = parser.parseBody();
|
||||
var body = match.getState();
|
||||
body.header = header;
|
||||
cb(body);
|
||||
} catch (e) {
|
||||
cb(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
app.post('/parse', function (req, res) {
|
||||
handleDataStream(req, function (body) {
|
||||
res.set('Content-Type', 'application/json');
|
||||
res.write(JSON.stringify(body));
|
||||
res.end();
|
||||
})
|
||||
});
|
||||
|
||||
app.listen(9123);
|
||||
|
||||
|
||||
const chakram = require('chakram');
|
||||
const expect = chakram.expect;
|
||||
const root = 'http://localhost:8000/';
|
||||
const fs = require('fs');
|
||||
|
||||
process.env.PARSER_URL = `http://localhost:9123/parse`;
|
||||
process.env.EDIT_SECRET = 'edit_key';
|
||||
|
||||
chakram.setRequestDefaults({baseUrl: root});
|
||||
|
||||
before((done) => {
|
||||
console.log('spawn server');
|
||||
const server = require('child_process').spawn('php', ['-S', '0.0.0.0:8000', 'router.php'], {
|
||||
cwd: __dirname + '/../',
|
||||
env: process.env
|
||||
});
|
||||
server.stdout.on('data', (data) => {
|
||||
console.log(`stdout: ${data}`);
|
||||
});
|
||||
server.stderr.on('data', (data) => {
|
||||
console.log(`stderr: ${data}`);
|
||||
});
|
||||
after(() => {
|
||||
console.log('clean server');
|
||||
server && server.kill();
|
||||
});
|
||||
setTimeout(done, 1000);
|
||||
});
|
||||
|
||||
before("reset db", function () {
|
||||
chakram.post("reset");
|
||||
return chakram.wait();
|
||||
});
|
||||
|
||||
beforeEach("create test user", function () {
|
||||
chakram.post("testuser");
|
||||
return chakram.wait();
|
||||
});
|
||||
|
||||
afterEach("reset db", function () {
|
||||
chakram.post("reset");
|
||||
return chakram.wait();
|
||||
});
|
||||
|
||||
function uploadDemo(file) {
|
||||
return chakram.post("upload", undefined, {
|
||||
formData: {
|
||||
name: 'foo',
|
||||
blue: 'BLU',
|
||||
red: 'RED',
|
||||
demo: fs.createReadStream(file),
|
||||
key: 'key1'
|
||||
}
|
||||
}).then((response) => {
|
||||
return parseInt(response.body.match(/\/(\d+)/)[1], 10);
|
||||
});
|
||||
}
|
||||
|
||||
chakram.addMethod("text", function (respObj, text) {
|
||||
const body = respObj.response.body;
|
||||
this.assert(body === text,
|
||||
'expected response text ' + body + ' to equal ' + text,
|
||||
'expected response text ' + body + ' to not be equal to ' + text);
|
||||
});
|
||||
chakram.addMethod("containsText", function (respObj, text) {
|
||||
const body = respObj.response.body;
|
||||
this.assert(body.indexOf(text) !== -1,
|
||||
'expected response text ' + body + ' to contain ' + text,
|
||||
'expected response text ' + body + ' to not contain ' + text);
|
||||
});
|
||||
describe("Upload", function () {
|
||||
this.timeout(1000 * 30);
|
||||
it("fails without valid key", function () {
|
||||
const response = chakram.post("upload", undefined, {
|
||||
formData: {
|
||||
name: 'foo',
|
||||
blue: 'BLU',
|
||||
red: 'RED',
|
||||
demo: fs.createReadStream(__dirname + '/../data/product.dem'),
|
||||
key: 'dummy'
|
||||
}
|
||||
});
|
||||
expect(response).to.have.status(401);
|
||||
expect(response).to.be.text('Invalid key');
|
||||
return chakram.wait();
|
||||
});
|
||||
|
||||
it("returns the demo path on success", function () {
|
||||
const response = chakram.post("upload", undefined, {
|
||||
formData: {
|
||||
name: 'foo',
|
||||
blue: 'BLU',
|
||||
red: 'RED',
|
||||
demo: fs.createReadStream(__dirname + '/../data/product.dem'),
|
||||
key: 'key1'
|
||||
}
|
||||
});
|
||||
// expect(response).to.have.status(401);
|
||||
expect(response).to.be.containsText('STV available at: ');
|
||||
return chakram.wait();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Demo listing", function () {
|
||||
this.timeout(1000 * 30);
|
||||
|
||||
it("starts empty", function () {
|
||||
const response = chakram.get("demos");
|
||||
expect(response).to.have.status(200);
|
||||
expect(response).to.have.header("content-type", "application/json; charset=utf-8");
|
||||
expect(response).to.comprise.of.json([]);
|
||||
return chakram.wait();
|
||||
});
|
||||
|
||||
it("contains uploaded demo", function () {
|
||||
return uploadDemo(__dirname + '/../data/product.dem').then(id => {
|
||||
return chakram.get("demos").then(response => {
|
||||
const body = response.body;
|
||||
expect(body[0].id).to.be.equal(id);
|
||||
expect(body[0].name).to.be.equal('foo');
|
||||
expect(body[0].server).to.be.equal('UGC Highlander Match');
|
||||
expect(body[0].duration).to.be.equal(778);
|
||||
expect(body[0].nick).to.be.equal('SourceTV Demo');
|
||||
expect(body[0].map).to.be.equal('koth_product_rc8');
|
||||
expect(body[0].red).to.be.equal('RED');
|
||||
expect(body[0].blue).to.be.equal('BLU');
|
||||
expect(body[0].redScore).to.be.equal(3);
|
||||
expect(body[0].blueScore).to.be.equal(0);
|
||||
expect(body[0].playerCount).to.be.equal(18);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Set url", function () {
|
||||
this.timeout(1000 * 30);
|
||||
|
||||
it("fails with invalid key", function () {
|
||||
return uploadDemo(__dirname + '/../data/product.dem').then(id => {
|
||||
return chakram.get("demos").then(response => {
|
||||
const setUrl = chakram.post(`/demos/${id}/url`, undefined, {
|
||||
formData: {
|
||||
hash: 'asd',
|
||||
backend: 'foo',
|
||||
url: 'http://bar',
|
||||
path: 'bar',
|
||||
key: 'foo'
|
||||
}
|
||||
});
|
||||
expect(setUrl).to.be.containsText('Invalid key');
|
||||
expect(setUrl).to.have.status(500);
|
||||
return chakram.wait();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("fails with invalid hash", function () {
|
||||
return uploadDemo(__dirname + '/../data/product.dem').then(id => {
|
||||
return chakram.get("demos").then(response => {
|
||||
const setUrl = chakram.post(`/demos/${id}/url`, undefined, {
|
||||
formData: {
|
||||
hash: 'asd',
|
||||
backend: 'foo',
|
||||
url: 'http://bar',
|
||||
path: 'bar',
|
||||
key: 'edit_key'
|
||||
}
|
||||
});
|
||||
expect(setUrl).to.be.containsText('Invalid demo hash');
|
||||
expect(setUrl).to.have.status(500);
|
||||
return chakram.wait();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("changes url, backend and path on success", function () {
|
||||
return uploadDemo(__dirname + '/../data/product.dem').then(id => {
|
||||
return chakram.get(`demos/${id}`).then(response => {
|
||||
const hash = response.body.hash;
|
||||
|
||||
const setUrl = chakram.post(`/demos/${id}/url`, undefined, {
|
||||
formData: {
|
||||
hash: hash,
|
||||
backend: 'foo',
|
||||
url: 'http://bar',
|
||||
path: 'bar',
|
||||
key: 'edit_key'
|
||||
}
|
||||
});
|
||||
expect(setUrl).to.have.status(200);
|
||||
return setUrl.then(response => {
|
||||
return chakram.get(`demos/${id}`)
|
||||
}).then(response => {
|
||||
const body = response.body;
|
||||
expect(body.id).to.be.equal(id);
|
||||
expect(body.backend).to.be.equal('foo');
|
||||
expect(body.url).to.be.equal('http://bar');
|
||||
expect(body.path).to.be.equal('bar');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
39
test/Providers/ChatProviderTest.php
Normal file
39
test/Providers/ChatProviderTest.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Demo\ChatMessage;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class ChatProviderTest extends TestCase {
|
||||
/** @var ChatProvider */
|
||||
private $provider;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->provider = new ChatProvider($this->getDatabaseConnection());
|
||||
}
|
||||
|
||||
public function testGetEmptyChat() {
|
||||
$this->assertCount(0, $this->provider->getChat(1));
|
||||
}
|
||||
|
||||
public function testStoreRetrieve() {
|
||||
$message1 = new ChatMessage('foo', 2, 'bar');
|
||||
$message2 = new ChatMessage('foo2', 2, 'bar2');
|
||||
$message3 = new ChatMessage('foo2', 2, 'bar2');
|
||||
|
||||
$this->provider->storeChatMessage(1, $message1);
|
||||
$this->provider->storeChatMessage(1, $message2);
|
||||
$this->provider->storeChatMessage(2, $message3);
|
||||
|
||||
$result = $this->provider->getChat(1);
|
||||
sort($result);
|
||||
|
||||
$this->assertCount(2, $result);
|
||||
$this->assertEquals($message1, $result[0]);
|
||||
$this->assertEquals($message2, $result[1]);
|
||||
}
|
||||
}
|
||||
163
test/Providers/DemoListProviderTest.php
Normal file
163
test/Providers/DemoListProviderTest.php
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
<?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',
|
||||
'backend',
|
||||
'path'
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
199
test/Providers/DemoProviderTest.php
Normal file
199
test/Providers/DemoProviderTest.php
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Data\DemoPlayer;
|
||||
use Demostf\API\Data\Kill;
|
||||
use Demostf\API\Data\Player;
|
||||
use Demostf\API\Demo\Demo;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
use Demostf\API\Providers\KillProvider;
|
||||
use Demostf\API\Providers\PlayerProvider;
|
||||
use Demostf\API\Providers\UserProvider;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class DemoProviderTest extends TestCase {
|
||||
/** @var DemoProvider */
|
||||
private $provider;
|
||||
|
||||
/** @var UserProvider */
|
||||
private $userProvider;
|
||||
|
||||
/** @var PlayerProvider */
|
||||
private $playerProvider;
|
||||
|
||||
/** @var KillProvider */
|
||||
private $killProvider;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->provider = new DemoProvider($this->getDatabaseConnection());
|
||||
$this->userProvider = new UserProvider($this->getDatabaseConnection(), $this->getRandomGenerator());
|
||||
$this->playerProvider = new PlayerProvider($this->getDatabaseConnection());
|
||||
$this->killProvider = new KillProvider($this->getDatabaseConnection());
|
||||
}
|
||||
|
||||
public function testGetNonExisting() {
|
||||
$this->assertNull($this->provider->get(1));
|
||||
}
|
||||
|
||||
public function testStoreRetrieve() {
|
||||
$uploaderSteamId = $this->getSteamId('12345', 'test');
|
||||
$this->userProvider->store($uploaderSteamId);
|
||||
|
||||
$uploader = $this->userProvider->get($uploaderSteamId->getSteamId64());
|
||||
|
||||
$demo = new Demo(
|
||||
0,
|
||||
'http://example.com',
|
||||
'name',
|
||||
'server',
|
||||
12,
|
||||
'nick',
|
||||
'map',
|
||||
new \DateTime(),
|
||||
'RED',
|
||||
'BLUE',
|
||||
1,
|
||||
2,
|
||||
18,
|
||||
$uploader->getId(),
|
||||
'hash',
|
||||
'dummy',
|
||||
'path'
|
||||
);
|
||||
$demo->setUploaderUser($uploader);
|
||||
|
||||
$id = $this->provider->storeDemo($demo, 'dummy', 'path');
|
||||
|
||||
$retrieved = $this->provider->get($id);
|
||||
$this->assertInstanceOf(Demo::class, $retrieved);
|
||||
|
||||
$storedData = $demo->jsonSerialize();
|
||||
$storedData['id'] = $id;
|
||||
|
||||
$this->assertEquals($storedData, $retrieved->jsonSerialize());
|
||||
|
||||
$this->assertEquals($id, $this->provider->demoIdByHash('hash'));
|
||||
}
|
||||
|
||||
public function testRetrieveWithPlayers() {
|
||||
$uploaderSteamId = $this->getSteamId('12345', 'test');
|
||||
$this->userProvider->store($uploaderSteamId);
|
||||
|
||||
$steamId1 = $this->getSteamId('1', 'u1');
|
||||
$steamId2 = $this->getSteamId('2', 'u2');
|
||||
$steamId3 = $this->getSteamId('3', 'u3');
|
||||
$steamId4 = $this->getSteamId('4', 'u4');
|
||||
|
||||
$this->userProvider->store($steamId1);
|
||||
$this->userProvider->store($steamId2);
|
||||
$this->userProvider->store($steamId3);
|
||||
$this->userProvider->store($steamId4);
|
||||
|
||||
$user1 = $this->userProvider->get($steamId1->getSteamId64());
|
||||
$user2 = $this->userProvider->get($steamId2->getSteamId64());
|
||||
$user3 = $this->userProvider->get($steamId3->getSteamId64());
|
||||
$user4 = $this->userProvider->get($steamId4->getSteamId64());
|
||||
|
||||
$uploader = $this->userProvider->get($uploaderSteamId->getSteamId64());
|
||||
|
||||
$demo = new Demo(
|
||||
0,
|
||||
'http://example.com',
|
||||
'name',
|
||||
'server',
|
||||
12,
|
||||
'nick',
|
||||
'map',
|
||||
new \DateTime(),
|
||||
'RED',
|
||||
'BLUE',
|
||||
1,
|
||||
2,
|
||||
18,
|
||||
$uploader->getId(),
|
||||
'hash',
|
||||
'backend',
|
||||
'path',
|
||||
'dummy',
|
||||
'path'
|
||||
);
|
||||
|
||||
$id = $this->provider->storeDemo($demo, 'dummy', 'path');
|
||||
$player1 = $this->addPlayer($id, 101, $user1->getId(), 'red', 'scout');
|
||||
$player2 = $this->addPlayer($id, 102, $user2->getId(), 'red', 'soldier');
|
||||
$player3 = $this->addPlayer($id, 103, $user3->getId(), 'blue', 'engineer');
|
||||
$player4 = $this->addPlayer($id, 104, $user4->getId(), 'blue', 'spy');
|
||||
|
||||
$this->addKill($id, $user1->getId(), 0, $user3->getId(), 'pan');
|
||||
$this->addKill($id, $user1->getId(), $user2->getId(), $user3->getId(), 'pan');
|
||||
$this->addKill($id, $user4->getId(), 0, $user1->getId(), 'pan');
|
||||
|
||||
$retrieved = $this->provider->get($id, true);
|
||||
$this->assertInstanceOf(Demo::class, $retrieved);
|
||||
|
||||
$players = $retrieved->getPlayers();
|
||||
$this->assertCount(4, $players);
|
||||
usort($players, function (DemoPlayer $a, DemoPlayer $b) {
|
||||
return $a->getUserId() - $b->getUserId();
|
||||
});
|
||||
$this->assertEquals([
|
||||
new DemoPlayer($player1, $user1->getId(), 'user_' . $user1->getId(), 'red', 'scout', '1', 'foo_medium.jpg', 2, 0, 1),
|
||||
new DemoPlayer($player2, $user2->getId(), 'user_' . $user2->getId(), 'red', 'soldier', '2', 'foo_medium.jpg', 0, 1, 0),
|
||||
new DemoPlayer($player3, $user3->getId(), 'user_' . $user3->getId(), 'blue', 'engineer', '3', 'foo_medium.jpg', 0, 0, 2),
|
||||
new DemoPlayer($player4, $user4->getId(), 'user_' . $user4->getId(), 'blue', 'spy', '4', 'foo_medium.jpg', 1, 0, 0),
|
||||
], $players);
|
||||
}
|
||||
|
||||
private function addPlayer(int $demoId, int $demoUserId, int $userId, string $team, string $class): int {
|
||||
$player = new Player(0, $demoId, $demoUserId, $userId, 'user_' . $userId, $team, $class);
|
||||
return $this->playerProvider->store($player);
|
||||
}
|
||||
|
||||
private function addKill(int $demoId, int $attackerId, int $assisterId, int $victimId, string $weapon): int {
|
||||
$kill = new Kill(0, $demoId, $attackerId, $assisterId, $victimId, $weapon);
|
||||
return $this->killProvider->store($kill);
|
||||
}
|
||||
|
||||
public function testSetDemoUrl() {
|
||||
$uploaderSteamId = $this->getSteamId('12345', 'test');
|
||||
$this->userProvider->store($uploaderSteamId);
|
||||
|
||||
$uploader = $this->userProvider->get($uploaderSteamId->getSteamId64());
|
||||
|
||||
$demo = new Demo(
|
||||
0,
|
||||
'http://example.com',
|
||||
'name',
|
||||
'server',
|
||||
12,
|
||||
'nick',
|
||||
'map',
|
||||
new \DateTime(),
|
||||
'RED',
|
||||
'BLUE',
|
||||
1,
|
||||
2,
|
||||
18,
|
||||
$uploader->getId(),
|
||||
'hash',
|
||||
'dummy',
|
||||
'path'
|
||||
);
|
||||
|
||||
$id = $this->provider->storeDemo($demo, 'dummy', 'path');
|
||||
$id2 = $this->provider->storeDemo($demo, 'dummy', 'path');
|
||||
|
||||
$this->provider->setDemoUrl($id, 'foobackend', 'http://foo.example.com', 'bar');
|
||||
|
||||
$storedDemo = $this->provider->get($id);
|
||||
$this->assertEquals('http://foo.example.com', $storedDemo->getUrl());
|
||||
$this->assertEquals('foobackend', $storedDemo->getBackend());
|
||||
$this->assertEquals('bar', $storedDemo->getPath());
|
||||
|
||||
$storedDemo2 = $this->provider->get($id2);
|
||||
$this->assertEquals('http://example.com', $storedDemo2->getUrl());
|
||||
}
|
||||
}
|
||||
302
test/Providers/UploadProviderTest.php
Normal file
302
test/Providers/UploadProviderTest.php
Normal file
|
|
@ -0,0 +1,302 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Data\ParsedDemo;
|
||||
use Demostf\API\Demo\Demo;
|
||||
use Demostf\API\Demo\DemoSaver;
|
||||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Demo\Header;
|
||||
use Demostf\API\Demo\HeaderParser;
|
||||
use Demostf\API\Demo\Parser;
|
||||
use Demostf\API\Demo\RawParser;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
use Demostf\API\Providers\KillProvider;
|
||||
use Demostf\API\Providers\PlayerProvider;
|
||||
use Demostf\API\Providers\UploadProvider;
|
||||
use Demostf\API\Providers\UserProvider;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class UploadProviderTest extends TestCase {
|
||||
/** @var RawParser */
|
||||
private $rawParser;
|
||||
/** @var HeaderParser */
|
||||
private $headerParser;
|
||||
/** @var Parser */
|
||||
private $parser;
|
||||
/** @var DemoStore */
|
||||
private $demoStore;
|
||||
/** @var UserProvider */
|
||||
private $userProvider;
|
||||
/** @var DemoProvider */
|
||||
private $demoProvider;
|
||||
/** @var DemoSaver */
|
||||
private $demoSaver;
|
||||
/** @var UploadProvider */
|
||||
private $uploadProvider;
|
||||
/** @var string */
|
||||
private $tmpDir;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->rawParser = $this->getMockBuilder(RawParser::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->rawParser->expects($this->any())
|
||||
->method('parse')
|
||||
->will($this->returnCallback(function ($path) {
|
||||
$jsonPath = str_replace('.dem', '-raw.json', $path);
|
||||
return json_decode(file_get_contents($jsonPath), true);
|
||||
}));
|
||||
|
||||
$targetDir = tempnam(sys_get_temp_dir(), 'dummy_target_');
|
||||
unlink($targetDir);
|
||||
mkdir($targetDir);
|
||||
$this->tmpDir = $targetDir;
|
||||
|
||||
$this->headerParser = new HeaderParser();
|
||||
$this->parser = new Parser($this->rawParser);
|
||||
$this->demoStore = new DemoStore($targetDir, 'example.com');
|
||||
$this->userProvider = new UserProvider($this->getDatabaseConnection(), $this->getRandomGenerator());
|
||||
$this->demoProvider = new DemoProvider($this->getDatabaseConnection());
|
||||
$this->demoSaver = new DemoSaver(
|
||||
new KillProvider($this->getDatabaseConnection()),
|
||||
new PlayerProvider($this->getDatabaseConnection()),
|
||||
new ChatProvider($this->getDatabaseConnection()),
|
||||
$this->userProvider,
|
||||
$this->demoProvider
|
||||
);
|
||||
|
||||
$this->uploadProvider = new UploadProvider(
|
||||
$this->getDatabaseConnection(),
|
||||
'http://example.com',
|
||||
$this->headerParser,
|
||||
$this->parser,
|
||||
$this->demoStore,
|
||||
$this->userProvider,
|
||||
$this->demoProvider,
|
||||
$this->demoSaver
|
||||
);
|
||||
}
|
||||
|
||||
private function rmdirr($dir) {
|
||||
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
|
||||
$files = new \RecursiveIteratorIterator($it,
|
||||
\RecursiveIteratorIterator::CHILD_FIRST);
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
rmdir($file->getRealPath());
|
||||
} else {
|
||||
unlink($file->getRealPath());
|
||||
}
|
||||
}
|
||||
rmdir($dir);
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
$this->rmdirr($this->tmpDir);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testValidateHeaderToSmall() {
|
||||
$this->assertEquals('Demos needs to be at least 1KB is size', $this->uploadProvider->validateHeader(
|
||||
12,
|
||||
new Header(
|
||||
'HL2DEMO',
|
||||
1,
|
||||
2,
|
||||
'Server',
|
||||
'Nick',
|
||||
'cp_gullywash',
|
||||
'tf',
|
||||
12,
|
||||
12,
|
||||
12,
|
||||
12
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
public function testValidateHeaderToBig() {
|
||||
$this->assertEquals('Demos cant be more than 100MB in size', $this->uploadProvider->validateHeader(
|
||||
99999999999,
|
||||
new Header(
|
||||
'HL2DEMO',
|
||||
1,
|
||||
2,
|
||||
'Server',
|
||||
'Nick',
|
||||
'cp_gullywash',
|
||||
'tf',
|
||||
12,
|
||||
12,
|
||||
12,
|
||||
12
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
public function testValidateHeaderToLong() {
|
||||
$this->assertEquals('Demos cant be longer than one hour', $this->uploadProvider->validateHeader(
|
||||
9999,
|
||||
new Header(
|
||||
'HL2DEMO',
|
||||
1,
|
||||
2,
|
||||
'Server',
|
||||
'Nick',
|
||||
'cp_gullywash',
|
||||
'tf',
|
||||
999999,
|
||||
12,
|
||||
12,
|
||||
12
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
public function testValidateParsedToShortNoRounds() {
|
||||
$this->assertEquals('Demos must be at least 5 minutes long', $this->uploadProvider->validateParsed(
|
||||
new Header(
|
||||
'HL2DEMO',
|
||||
1,
|
||||
2,
|
||||
'Server',
|
||||
'Nick',
|
||||
'cp_gullywash',
|
||||
'tf',
|
||||
60,
|
||||
12,
|
||||
12,
|
||||
12
|
||||
),
|
||||
new ParsedDemo(0, 0, [], [], [])
|
||||
));
|
||||
}
|
||||
|
||||
public function testValidateParsedToShortRounds() {
|
||||
$this->assertNull($this->uploadProvider->validateParsed(
|
||||
new Header(
|
||||
'HL2DEMO',
|
||||
1,
|
||||
2,
|
||||
'Server',
|
||||
'Nick',
|
||||
'cp_gullywash',
|
||||
'tf',
|
||||
60,
|
||||
12,
|
||||
12,
|
||||
12
|
||||
),
|
||||
new ParsedDemo(1, 0, [], [], [])
|
||||
));
|
||||
}
|
||||
|
||||
public function testUploadInvalidKey() {
|
||||
$this->assertEquals('Invalid key',
|
||||
$this->uploadProvider->upload('asdasd', 'RED', 'BLU', 'asdasd', 'asdas')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Not an HL2 demo
|
||||
*/
|
||||
public function testUploadNonDemo() {
|
||||
file_put_contents($this->tmpDir . '/foo.dem', 'asd');
|
||||
|
||||
$steamId = $this->getSteamId('123', 'a');
|
||||
$token = $this->userProvider->store($steamId);
|
||||
|
||||
$this->uploadProvider->upload($token, 'RED', 'BLU', 'asdasd', $this->tmpDir . '/foo.dem');
|
||||
}
|
||||
|
||||
public function testUploadExisting() {
|
||||
$source = fopen(__DIR__ . '/../data/product.dem', 'r');
|
||||
file_put_contents($this->tmpDir . '/foo.dem', fread($source, 2048));
|
||||
fclose($source);
|
||||
$hash = md5_file($this->tmpDir . '/foo.dem');
|
||||
|
||||
$id = $this->demoProvider->storeDemo(
|
||||
new Demo(
|
||||
0,
|
||||
'a',
|
||||
'b',
|
||||
'c',
|
||||
12,
|
||||
'n',
|
||||
'm',
|
||||
new \DateTime(),
|
||||
'r',
|
||||
'b',
|
||||
1,
|
||||
2,
|
||||
2,
|
||||
1,
|
||||
$hash,
|
||||
'b',
|
||||
'p'
|
||||
),
|
||||
'test', 'test'
|
||||
);
|
||||
|
||||
$steamId = $this->getSteamId('123', 'a');
|
||||
$token = $this->userProvider->store($steamId);
|
||||
|
||||
$this->assertEquals('STV available at: http://example.com/' . $id,
|
||||
$this->uploadProvider->upload($token, 'RED', 'BLU', 'asdasd', $this->tmpDir . '/foo.dem')
|
||||
);
|
||||
}
|
||||
|
||||
private function saveSteamId($steamId, $name) {
|
||||
$steamId = $this->getSteamId($this->parser->convertSteamIdToCommunityId($steamId), $name);
|
||||
$this->userProvider->store($steamId);
|
||||
}
|
||||
|
||||
public function testUpload() {
|
||||
copy(__DIR__ . '/../data/product.dem', $this->tmpDir . '/foo.dem');
|
||||
copy(__DIR__ . '/../data/product-raw.json', $this->tmpDir . '/foo-raw.json');
|
||||
|
||||
$steamId = $this->getSteamId('123', 'a');
|
||||
$token = $this->userProvider->store($steamId);
|
||||
|
||||
// pre-save the names so we dont have to get them from steam
|
||||
$this->saveSteamId('[U:1:64229260]', 'Icewind');
|
||||
$this->saveSteamId('[U:1:115748435]', 'Foz');
|
||||
$this->saveSteamId('[U:1:115754284]', 'Deity');
|
||||
$this->saveSteamId('[U:1:92428736]', 'Kireek');
|
||||
$this->saveSteamId('[U:1:22958903]', 'Vinegar');
|
||||
$this->saveSteamId('[U:1:32061783]', 'Kimo');
|
||||
$this->saveSteamId('[U:1:67502510]', 'magikarp');
|
||||
$this->saveSteamId('[U:1:55128465]', 'Solar');
|
||||
$this->saveSteamId('[U:1:301587080]', 'ztreak');
|
||||
$this->saveSteamId('[U:1:22162172]', 'TheMasterOfDisaster');
|
||||
$this->saveSteamId('[U:1:13559571]', 'Sage');
|
||||
$this->saveSteamId('[U:1:71706948]', 'Sketis');
|
||||
$this->saveSteamId('[U:1:157204170]', 'Pyla');
|
||||
$this->saveSteamId('[U:1:30838206]', 'Heavy');
|
||||
$this->saveSteamId('[U:1:174774002]', 'Soldier');
|
||||
$this->saveSteamId('[U:1:92096346]', 'Fish');
|
||||
$this->saveSteamId('[U:1:143626373]', 'Pendulum');
|
||||
$this->saveSteamId('[U:1:30220936]', 'Jedi');
|
||||
|
||||
|
||||
$result = $this->uploadProvider->upload($token, 'RED', 'BLU', 'foodemo', $this->tmpDir . '/foo.dem');
|
||||
$this->assertStringStartsWith('STV available at: http://example.com/', $result);
|
||||
|
||||
$demoId = (int)substr($result, strlen('STV available at: http://example.com/'));
|
||||
|
||||
$demo = $this->demoProvider->get($demoId, true);
|
||||
|
||||
$this->assertNotNull($demo);
|
||||
|
||||
$this->assertEquals('koth_product_rc8', $demo->getMap());
|
||||
$this->assertEquals(0, $demo->getBlueScore());
|
||||
$this->assertEquals(3, $demo->getRedScore());
|
||||
}
|
||||
}
|
||||
78
test/Providers/UserProviderTest.php
Normal file
78
test/Providers/UserProviderTest.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Providers\UserProvider;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class UserProviderTest extends TestCase {
|
||||
/** @var UserProvider */
|
||||
private $provider;
|
||||
|
||||
/** @var \SteamId */
|
||||
private $steamId;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->steamId = $this->getSteamId('76561198024494988', 'Icewind');
|
||||
$this->provider = new UserProvider($this->getDatabaseConnection(), $this->getRandomGenerator());
|
||||
}
|
||||
|
||||
public function testGetNonExisting() {
|
||||
$this->assertNull($this->provider->get('76561198024494988'));
|
||||
}
|
||||
|
||||
public function testStoreRetrieve() {
|
||||
$this->provider->store($this->steamId);
|
||||
|
||||
$user = $this->provider->get('76561198024494988');
|
||||
|
||||
$this->assertEquals($this->steamId->getNickname(), $user->getName());
|
||||
$this->assertEquals($this->steamId->getSteamId64(), $user->getSteamId());
|
||||
}
|
||||
|
||||
public function returnTokenExisting() {
|
||||
$token1 = $this->provider->store($this->steamId);
|
||||
$token2 = $this->provider->store($this->steamId);
|
||||
|
||||
$this->assertEquals($token1, $token2);
|
||||
}
|
||||
|
||||
public function testDoubleInsert() {
|
||||
$this->provider->store($this->steamId);
|
||||
$this->provider->store($this->steamId);
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function testByKey() {
|
||||
$token = $this->provider->store($this->steamId);
|
||||
|
||||
$byKey = $this->provider->byKey($token);
|
||||
$this->assertEquals('76561198024494988', $byKey->getSteamId());
|
||||
}
|
||||
|
||||
public function testSearch() {
|
||||
$result = $this->provider->search('__NOT__FOUND__');
|
||||
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
public function testGetIdExisting() {
|
||||
$this->provider->store($this->steamId);
|
||||
|
||||
$user = $this->provider->get($this->steamId->getSteamId64());
|
||||
|
||||
$this->assertEquals($user->getId(), $this->provider->getUserId($this->steamId->getSteamId64()));
|
||||
}
|
||||
|
||||
public function testGetIdNew() {
|
||||
$id = $this->provider->getUserId($this->steamId->getSteamId64());
|
||||
|
||||
$user = $this->provider->get($this->steamId->getSteamId64());
|
||||
|
||||
|
||||
$this->assertEquals($user->getId(), $id);
|
||||
}
|
||||
}
|
||||
66
test/TestCase.php
Normal file
66
test/TestCase.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php namespace Demostf\API\Test;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\DriverManager;
|
||||
|
||||
abstract class TestCase extends \PHPUnit\Framework\TestCase {
|
||||
/** @var Connection */
|
||||
private $database;
|
||||
|
||||
protected function getDatabaseConnection() {
|
||||
if (!$this->database instanceof Connection) {
|
||||
$connectionParams = array(
|
||||
'dbname' => getenv('DB_DATABASE'),
|
||||
'user' => getenv('DB_USERNAME'),
|
||||
'password' => getenv('DB_PASSWORD'),
|
||||
'host' => getenv('DB_HOST'),
|
||||
'port' => getenv('DB_PORT'),
|
||||
'driver' => getenv('DB_TYPE'),
|
||||
);
|
||||
if ($connectionParams['driver'] === 'pgsql') {
|
||||
$connectionParams['driver'] = 'pdo_pgsql';
|
||||
}
|
||||
$this->database = DriverManager::getConnection($connectionParams);
|
||||
}
|
||||
return $this->database;
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
$this->clearDatabase();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
private function clearDatabase() {
|
||||
if ($this->database instanceof Connection) {
|
||||
$tables = $this->database->getSchemaManager()->listTables();
|
||||
foreach ($tables as $table) {
|
||||
$this->truncateTable($table->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function truncateTable(string $tableName) {
|
||||
$sql = sprintf('TRUNCATE TABLE %s;', $tableName);
|
||||
$this->getDatabaseConnection()->query($sql);
|
||||
}
|
||||
|
||||
protected function getRandomGenerator() {
|
||||
$factory = new \RandomLib\Factory;
|
||||
return $factory->getMediumStrengthGenerator();
|
||||
}
|
||||
|
||||
protected function getSteamId($steamId, $name) {
|
||||
$steamId = new \SteamId($steamId, false);
|
||||
$closure = \Closure::bind(function ($steamId) use ($name) {
|
||||
$steamId->nickname = $name;
|
||||
$steamId->imageUrl = 'foo';
|
||||
}, null, $steamId);
|
||||
$closure($steamId);
|
||||
return $steamId;
|
||||
}
|
||||
|
||||
}
|
||||
1
test/bootstrap.php
Normal file
1
test/bootstrap.php
Normal file
|
|
@ -0,0 +1 @@
|
|||
<?php require_once __DIR__ . '/../src/init.php';
|
||||
1597
test/data/product-analyse.json
Normal file
1597
test/data/product-analyse.json
Normal file
File diff suppressed because it is too large
Load diff
1
test/data/product-raw.json
Normal file
1
test/data/product-raw.json
Normal file
File diff suppressed because one or more lines are too long
BIN
test/data/product.dem
Normal file
BIN
test/data/product.dem
Normal file
Binary file not shown.
15
test/phpunit.xml
Normal file
15
test/phpunit.xml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<phpunit bootstrap="bootstrap.php">
|
||||
<testsuite name='API'>
|
||||
<directory suffix='Test.php'>./</directory>
|
||||
</testsuite>
|
||||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">../src/Providers</directory>
|
||||
<directory suffix=".php">../src/Demo</directory>
|
||||
<directory suffix=".php">../src/Data</directory>
|
||||
<directory suffix=".php">../src/Exception</directory>
|
||||
<directory suffix=".php">../src/Controllers</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
43
test/router.php
Normal file
43
test/router.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
$_SERVER['SCRIPT_NAME'] = '';
|
||||
|
||||
if ($_SERVER["REQUEST_URI"] === '/upload') {
|
||||
require __DIR__ . '/../src/public/upload.php';
|
||||
} else if ($_SERVER["REQUEST_URI"] === '/reset') {
|
||||
// allow the api tests to reset the database
|
||||
/** @var \Demostf\API\Container $container */
|
||||
$container = require __DIR__ . '/../src/init.php';
|
||||
$connection = $container->getConnection();
|
||||
|
||||
clearDatabase($connection);
|
||||
} else if ($_SERVER["REQUEST_URI"] === '/testuser') {
|
||||
// allow the api tests to create a test user
|
||||
/** @var \Demostf\API\Container $container */
|
||||
$container = require __DIR__ . '/../src/init.php';
|
||||
$connection = $container->getConnection();
|
||||
|
||||
$query = $connection->createQueryBuilder();
|
||||
$query->insert('users')
|
||||
->values([
|
||||
'steamid' => $query->createNamedParameter('steamid1'),
|
||||
'name' => $query->createNamedParameter('nickname1'),
|
||||
'avatar' => $query->createNamedParameter('avatar1'),
|
||||
'token' => $query->createNamedParameter('key1')
|
||||
])->add('orderBy', 'ON CONFLICT DO NOTHING')// hack to append arbitrary string to sql
|
||||
->execute();
|
||||
} else {
|
||||
require __DIR__ . '/../src/public/index.php';
|
||||
}
|
||||
|
||||
function clearDatabase(\Doctrine\DBAL\Connection $connection) {
|
||||
$tables = $connection->getSchemaManager()->listTables();
|
||||
foreach ($tables as $table) {
|
||||
truncateTable($connection, $table->getName());
|
||||
}
|
||||
}
|
||||
|
||||
function truncateTable(\Doctrine\DBAL\Connection $connection, string $tableName) {
|
||||
$sql = sprintf('TRUNCATE TABLE %s;', $tableName);
|
||||
$connection->query($sql);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue