mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
add basic api tests
This commit is contained in:
parent
64b0aff075
commit
e00e6ece5f
30 changed files with 350 additions and 17 deletions
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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue