Migraciones y testeo
This commit is contained in:
46
app/test.bootstrap.php
Normal file
46
app/test.bootstrap.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
require_once implode(DIRECTORY_SEPARATOR, [
|
||||
__DIR__,
|
||||
'vendor',
|
||||
'autoload.php'
|
||||
]);
|
||||
|
||||
function setupDatabase(string $schemaFilename): void {
|
||||
printf("Loading database schema from %s", $schemaFilename);
|
||||
$start = microtime(true);
|
||||
|
||||
$dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}";
|
||||
$pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
|
||||
|
||||
$sql = file_get_contents($schemaFilename);
|
||||
$pdo->exec($sql);
|
||||
|
||||
$end = microtime(true);
|
||||
printf(" in %.2f seconds\n", $end - $start);
|
||||
}
|
||||
function truncateTables(): void {
|
||||
printf("Truncating tables");
|
||||
$start = microtime(true);
|
||||
|
||||
$dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}";
|
||||
$pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
|
||||
|
||||
$pdo->exec("SET FOREIGN_KEY_CHECKS=0");
|
||||
$statement = $pdo->query('SHOW TABLES');
|
||||
$tables = array_map(function(array $row) {
|
||||
return $row["Tables_in_{$_ENV['DB_DATABASE']}"];
|
||||
}, $statement->fetchAll(PDO::FETCH_ASSOC));
|
||||
|
||||
foreach ($tables as $table) {
|
||||
$pdo->exec("TRUNCATE TABLE `$table`");
|
||||
}
|
||||
$pdo->exec("SET FOREIGN_KEY_CHECKS=1");
|
||||
|
||||
$end = microtime(true);
|
||||
printf(" in %.2f seconds\n", $end - $start);
|
||||
}
|
||||
$schemaFilename = implode(DIRECTORY_SEPARATOR, [__DIR__, 'resources', 'database', 'schema.sql']);
|
||||
setupDatabase($schemaFilename);
|
||||
register_shutdown_function(function() {
|
||||
truncateTables();
|
||||
});
|
Reference in New Issue
Block a user