mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
cleanup and tests
This commit is contained in:
parent
ae39548ddb
commit
05f48fd0a0
7 changed files with 110 additions and 25 deletions
|
|
@ -12,15 +12,23 @@ class DemoStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(string $sourcePath, string $name): string {
|
public function store(string $sourcePath, string $name): string {
|
||||||
rename($sourcePath, $this->generatePath($name));
|
$target = $this->generatePath($name);
|
||||||
|
if (!is_dir(dirname($target))) {
|
||||||
|
mkdir(dirname($target), 0777, true);
|
||||||
|
}
|
||||||
|
rename($sourcePath, $target);
|
||||||
return $this->getUrl($name);
|
return $this->getUrl($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generatePath(string $name): string {
|
private function generatePath(string $name): string {
|
||||||
return $this->root . '/' . substr($name, 0, 2) . '/' . substr($name, 2, 4) . '/' . $name;
|
return $this->root . $this->getPrefix($name) . $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getPrefix(string $name) {
|
||||||
|
return '/' . substr($name, 0, 2) . '/' . substr($name, 2, 2) . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getUrl(string $name): string {
|
private function getUrl(string $name): string {
|
||||||
return 'https://' . $this->webroot . '/' . substr($name, 0, 2) . '/' . substr($name, 2, 4) . '/' . $name;
|
return 'https://' . $this->webroot . $this->getPrefix($name) . $name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,24 @@
|
||||||
|
|
||||||
namespace Demostf\API\Demo;
|
namespace Demostf\API\Demo;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Higher level parser
|
* Higher level parser
|
||||||
*
|
*
|
||||||
* Processes the raw demo.js output to something more suitable for our purpose
|
* Processes the raw demo.js output to something more suitable for our purpose
|
||||||
*/
|
*/
|
||||||
class Parser {
|
class Parser {
|
||||||
|
const CLASSES = [
|
||||||
|
1 => 'scout',
|
||||||
|
2 => 'sniper',
|
||||||
|
3 => 'soldier',
|
||||||
|
4 => 'demoman',
|
||||||
|
5 => 'medic',
|
||||||
|
6 => 'heavyweapons',
|
||||||
|
7 => 'pyro',
|
||||||
|
8 => 'spy',
|
||||||
|
9 => 'engineer'
|
||||||
|
];
|
||||||
|
|
||||||
/** @var RawParser */
|
/** @var RawParser */
|
||||||
private $rawParser;
|
private $rawParser;
|
||||||
|
|
||||||
|
|
@ -81,17 +91,6 @@ class Parser {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getClassName(int $classId): string {
|
private function getClassName(int $classId): string {
|
||||||
$classes = [
|
return self::CLASSES[$classId] ?? 'Unknown';
|
||||||
1 => 'scout',
|
|
||||||
2 => 'sniper',
|
|
||||||
3 => 'soldier',
|
|
||||||
4 => 'demoman',
|
|
||||||
5 => 'medic',
|
|
||||||
6 => 'heavyweapons',
|
|
||||||
7 => 'pyro',
|
|
||||||
8 => 'spy',
|
|
||||||
9 => 'engineer'
|
|
||||||
];
|
|
||||||
return $classes[$classId] ?? 'Unknown';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
<?php declare(strict_types = 1);
|
|
||||||
|
|
||||||
namespace Demostf\API\Exception;
|
|
||||||
|
|
||||||
class NotFoundException extends \Exception {
|
|
||||||
|
|
||||||
}
|
|
||||||
27
tests/Data/DemoPlayerTest.php
Normal file
27
tests/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());
|
||||||
|
}
|
||||||
|
}
|
||||||
32
tests/Demo/DemoStoreTest.php
Normal file
32
tests/Demo/DemoStoreTest.php
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Demostf\API\Test\Data;
|
||||||
|
|
||||||
|
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');
|
||||||
|
|
||||||
|
$url = $demoStore->store($file, 'foodemo.dem');
|
||||||
|
|
||||||
|
$this->assertStringEndsWith('/foodemo.dem', $url);
|
||||||
|
$this->assertStringStartsWith('https://static.example.com/', $url);
|
||||||
|
|
||||||
|
$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));
|
||||||
|
rmdir($targetDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -55,4 +55,12 @@ class HeaderParserTest extends TestCase {
|
||||||
$parser = new HeaderParser();
|
$parser = new HeaderParser();
|
||||||
$parser->parseHeader(__FILE__);
|
$parser->parseHeader(__FILE__);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testNonExisting() {
|
||||||
|
$parser = new HeaderParser();
|
||||||
|
$parser->parseHeader('/non/existing');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use Demostf\API\Demo\RawParser;
|
||||||
use Demostf\API\Test\TestCase;
|
use Demostf\API\Test\TestCase;
|
||||||
|
|
||||||
class ParserTest extends TestCase {
|
class ParserTest extends TestCase {
|
||||||
/** @var RawParser */
|
/** @var RawParser|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
private $rawParser;
|
private $rawParser;
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
|
|
@ -34,4 +34,22 @@ class ParserTest extends TestCase {
|
||||||
|
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue