buckets/tests/bootstrap.php

67 lines
2.2 KiB
PHP
Raw Normal View History

2026-06-15 01:04:32 +02:00
<?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);
}
2026-06-17 20:54:25 +02:00
// 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).
2026-06-17 21:24:38 +02:00
(static function (): void {
$kernel = new App\Kernel('test', true);
2026-06-17 20:54:25 +02:00
$kernel->boot();
$container = $kernel->getContainer();
2026-06-17 21:24:38 +02:00
/** @var Doctrine\DBAL\Connection $connection */
2026-06-17 20:54:25 +02:00
$connection = $container->get('doctrine')->getConnection();
// 1. Ensure the database exists.
$schemaManager = $connection->createSchemaManager();
2026-06-17 21:24:38 +02:00
$dbName = $connection->getDatabase();
2026-06-17 20:54:25 +02:00
if (!in_array($dbName, $schemaManager->listDatabases(), true)) {
$schemaManager->createDatabase($dbName);
}
// 2. Run any pending migrations idempotently.
// --no-interaction suppresses the "are you sure?" prompt.
2026-06-17 21:24:38 +02:00
/** @var Symfony\Component\Console\Application $application */
$application = new Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
2026-06-17 20:54:25 +02:00
$application->setAutoExit(false);
2026-06-17 21:24:38 +02:00
// 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();
2026-06-17 20:54:25 +02:00
$exitCode = $application->run(
2026-06-17 21:24:38 +02:00
new Symfony\Component\Console\Input\ArrayInput([
'command' => 'doctrine:migrations:migrate',
2026-06-17 20:54:25 +02:00
'--no-interaction' => true,
]),
2026-06-17 21:24:38 +02:00
$output,
2026-06-17 20:54:25 +02:00
);
2026-06-17 21:24:38 +02:00
if (0 !== $exitCode) {
fwrite(\STDERR, "bootstrap.php: doctrine:migrations:migrate failed (exit {$exitCode})\n");
fwrite(\STDERR, $output->fetch());
2026-06-17 20:54:25 +02:00
exit(1);
}
$kernel->shutdown();
})();