buckets/tests/bootstrap.php

63 lines
2 KiB
PHP
Raw Normal View History

2026-06-15 01:04:32 +02:00
<?php
2026-06-17 20:54:25 +02:00
use Doctrine\ORM\Tools\SchemaTool;
2026-06-15 01:04:32 +02:00
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).
(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);
$exitCode = $application->run(
new \Symfony\Component\Console\Input\ArrayInput([
'command' => 'doctrine:migrations:migrate',
'--no-interaction' => true,
]),
new \Symfony\Component\Console\Output\NullOutput(),
);
if ($exitCode !== 0) {
fwrite(STDERR, "bootstrap.php: doctrine:migrations:migrate failed (exit {$exitCode})\n");
exit(1);
}
$kernel->shutdown();
})();