Add repo hygiene rules and ignore secrets
This commit is contained in:
62
server/tests/Support/TestDatabase.php
Normal file
62
server/tests/Support/TestDatabase.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Support;
|
||||
|
||||
use App\Database\ConnectionFactory;
|
||||
use PDO;
|
||||
|
||||
final class TestDatabase
|
||||
{
|
||||
public static function create(): PDO
|
||||
{
|
||||
$dbName = getenv('DB_TEST_NAME') ?: (getenv('DB_NAME') ?: 'galaxyforge');
|
||||
return ConnectionFactory::create($dbName);
|
||||
}
|
||||
|
||||
public static function reset(PDO $pdo): void
|
||||
{
|
||||
$tables = [
|
||||
'user_permission_overrides',
|
||||
'user_roles',
|
||||
'role_permissions',
|
||||
'permissions',
|
||||
'roles',
|
||||
'build_jobs',
|
||||
'planet_buildings',
|
||||
'planets',
|
||||
'users',
|
||||
];
|
||||
foreach ($tables as $table) {
|
||||
$pdo->exec("DROP TABLE IF EXISTS {$table} CASCADE");
|
||||
}
|
||||
|
||||
$migration = __DIR__ . '/../../db/migrations/001_init.sql';
|
||||
$sql = file_get_contents($migration);
|
||||
if ($sql === false) {
|
||||
throw new \RuntimeException('Migration nicht lesbar.');
|
||||
}
|
||||
$pdo->exec($sql);
|
||||
}
|
||||
|
||||
public static function seedMinimal(PDO $pdo): array
|
||||
{
|
||||
$pdo->exec("INSERT INTO roles (key, name) VALUES ('player', 'Spieler')");
|
||||
$pdo->exec("INSERT INTO roles (key, name) VALUES ('admin', 'Admin')");
|
||||
|
||||
$pdo->exec("INSERT INTO permissions (key, module, description) VALUES ('planet.public.view', 'planet', 'Planet ansehen')");
|
||||
$pdo->exec("INSERT INTO permissions (key, module, description) VALUES ('planet.admin.generate', 'planet', 'Planeten generieren')");
|
||||
|
||||
$pdo->exec("INSERT INTO role_permissions (role_id, permission_id)
|
||||
SELECT r.id, p.id FROM roles r JOIN permissions p ON r.key = 'player' AND p.key = 'planet.public.view'");
|
||||
|
||||
$pdo->exec("INSERT INTO users (username, race_key) VALUES ('tester', 'human')");
|
||||
$userId = (int)$pdo->query("SELECT id FROM users WHERE username = 'tester'")->fetchColumn();
|
||||
|
||||
$pdo->exec("INSERT INTO user_roles (user_id, role_id)
|
||||
SELECT {$userId}, r.id FROM roles r WHERE r.key = 'player'");
|
||||
|
||||
return ['user_id' => $userId];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user