1
0
Fork 0
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:
Robin Appelman 2017-07-16 01:12:07 +02:00
commit e00e6ece5f
30 changed files with 350 additions and 17 deletions

43
test/router.php Normal file
View 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);
}