buckets/tests/bootstrap.php
2026-06-17 23:49:43 +02:00

66 lines
2.2 KiB
PHP

<?php
use Symfony\Component\Dotenv\Dotenv;
require dirname(__DIR__).'/vendor/autoload.php';
if (method_exists(Dotenv::class, 'bootEnv')) {
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
}
if ($_SERVER['APP_DEBUG']) {
umask(0000);
}
// Bootstrap the test database so that a fresh checkout / CI never hits
// "database does not exist" or schema drift.
//
// Strategy: use the Symfony kernel (test env) to reach Doctrine, then:
// 1. CREATE DATABASE IF NOT EXISTS — via the DBAL schema manager
// 2. Run pending migrations — via the MigrationsBundle console command
//
// This runs once per phpunit invocation (not per test — DAMA wraps each test
// in a transaction and rolls it back, so the schema itself must already exist).
(static function (): void {
$kernel = new App\Kernel('test', true);
$kernel->boot();
$container = $kernel->getContainer();
/** @var Doctrine\DBAL\Connection $connection */
$connection = $container->get('doctrine')->getConnection();
// 1. Ensure the database exists.
$schemaManager = $connection->createSchemaManager();
$dbName = $connection->getDatabase();
if (!in_array($dbName, $schemaManager->listDatabases(), true)) {
$schemaManager->createDatabase($dbName);
}
// 2. Run any pending migrations idempotently.
// --no-interaction suppresses the "are you sure?" prompt.
/** @var Symfony\Component\Console\Application $application */
$application = new Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$application->setAutoExit(false);
// Capture output in a buffer so a failed migration surfaces its diagnostics
// on STDERR instead of being silently swallowed.
$output = new Symfony\Component\Console\Output\BufferedOutput();
$exitCode = $application->run(
new Symfony\Component\Console\Input\ArrayInput([
'command' => 'doctrine:migrations:migrate',
'--no-interaction' => true,
]),
$output,
);
if (0 !== $exitCode) {
fwrite(\STDERR, "bootstrap.php: doctrine:migrations:migrate failed (exit {$exitCode})\n");
fwrite(\STDERR, $output->fetch());
exit(1);
}
$kernel->shutdown();
})();