1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 18:04:08 +02:00

upload and tests

This commit is contained in:
Robin Appelman 2017-04-10 00:58:50 +02:00
commit 03d3acebf5
21 changed files with 1026 additions and 152 deletions

View 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));
}
}

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Demostf\API\Test\Data;
namespace Demostf\API\Test\Demo;
use Demostf\API\Demo\DemoStore;
use Demostf\API\Test\TestCase;
@ -16,17 +16,16 @@ class DemoStoreTest extends TestCase {
$file = tempnam(sys_get_temp_dir(), 'dummy_');
file_put_contents($file, 'foobar');
$url = $demoStore->store($file, 'foodemo.dem');
$storedDemo = $demoStore->store($file, 'foodemo.dem');
$this->assertStringEndsWith('/foodemo.dem', $url);
$this->assertStringStartsWith('https://static.example.com/', $url);
$this->assertStringEndsWith('/foodemo.dem', $storedDemo->getUrl());
$this->assertStringStartsWith('https://static.example.com/', $storedDemo->getUrl());
$this->assertEquals('static', $storedDemo->getBackend());
$subPath = str_replace('https://static.example.com/', '', $url);
$this->assertStringEqualsFile($targetDir . '/' . $subPath, 'foobar');
unlink($targetDir . '/' . $subPath);
rmdir(dirname($targetDir . '/' . $subPath));
rmdir(dirname($targetDir . '/' . $subPath, 2));
$this->assertStringEqualsFile($storedDemo->getPath(), 'foobar');
unlink($storedDemo->getPath());
rmdir(dirname($storedDemo->getPath()));
rmdir(dirname($storedDemo->getPath(), 2));
rmdir($targetDir);
}
}

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Demostf\API\Test\Providers;
namespace Demostf\API\Test\Demo;
use Demostf\API\Demo\Header;
use Demostf\API\Demo\HeaderParser;
@ -10,19 +10,19 @@ class HeaderParserTest extends TestCase {
public function testParseFile() {
$parser = new HeaderParser();
$expected = new Header([
'type' => 'HL2DEMO',
'version' => 3,
'protocol' => 24,
'server' => 'UGC Highlander Match',
'nick' => 'SourceTV Demo',
'map' => 'koth_product_rc8',
'game' => 'tf',
'duration' => 778.4849853515625,
'ticks' => 51899,
'frames' => 25703,
'sigon' => 818263
]);
$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());

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Demostf\API\Test\Providers;
namespace Demostf\API\Test\Demo;
use Demostf\API\Demo\Parser;
use Demostf\API\Demo\RawParser;
@ -30,9 +30,31 @@ class ParserTest extends TestCase {
$result = $parser->analyse(__DIR__ . '/../data/product.dem');
$expected = json_decode(file_get_contents(__DIR__ . '/../data/product-analyse.json'), true);
$expectedRaw = json_decode(file_get_contents(__DIR__ . '/../data/product-analyse.json'), true);
$this->assertEquals($expected, $result);
$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());
}
/**
@ -52,4 +74,20 @@ class ParserTest extends TestCase {
$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);
}
}

View file

@ -0,0 +1,300 @@
<?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
),
'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());
}
}

View file

@ -58,4 +58,21 @@ class UserProviderTest extends TestCase {
$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);
}
}