mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
upload framework
This commit is contained in:
parent
754b1ce108
commit
a1a9504f11
30 changed files with 1578 additions and 155 deletions
52
tests/Providers/UserProviderTest.php
Normal file
52
tests/Providers/UserProviderTest.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php 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 = new \SteamId('76561198024494988');
|
||||
$this->provider = new UserProvider($this->getDatabaseConnection(), $this->getRandomGenerator());
|
||||
}
|
||||
|
||||
public function testGetNonExisting() {
|
||||
$this->assertFalse($this->provider->get('76561198024494988'));
|
||||
}
|
||||
|
||||
public function testStoreRetrieve() {
|
||||
$this->provider->store($this->steamId);
|
||||
|
||||
$user = $this->provider->get('76561198024494988');
|
||||
|
||||
$this->assertEquals($this->steamId->getNickname(), $user['name']);
|
||||
$this->assertEquals($this->steamId->getSteamId64(), '76561198024494988');
|
||||
}
|
||||
|
||||
public function testDoubleInsert() {
|
||||
$this->provider->store($this->steamId);
|
||||
$this->provider->store($this->steamId);
|
||||
}
|
||||
|
||||
public function testByKey() {
|
||||
$this->provider->store($this->steamId);
|
||||
|
||||
$user = $this->provider->get('76561198024494988');
|
||||
|
||||
$byKey = $this->provider->byKey($user['token']);
|
||||
$this->assertEquals('76561198024494988', $byKey['steamid']);
|
||||
}
|
||||
|
||||
public function testSearch() {
|
||||
$result = $this->provider->search('__NOT__FOUND__');
|
||||
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
}
|
||||
55
tests/TestCase.php
Normal file
55
tests/TestCase.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?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();
|
||||
}
|
||||
}
|
||||
1
tests/bootstrap.php
Normal file
1
tests/bootstrap.php
Normal file
|
|
@ -0,0 +1 @@
|
|||
<?php require_once __DIR__ . '/../src/init.php';
|
||||
11
tests/phpunit.xml
Normal file
11
tests/phpunit.xml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?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</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
Loading…
Add table
Add a link
Reference in a new issue