Skip to content

Instantly share code, notes, and snippets.

@bpolaszek
Last active August 26, 2025 13:52
Show Gist options
  • Select an option

  • Save bpolaszek/b0b28276123cbe4f19d4fe05c10dcc91 to your computer and use it in GitHub Desktop.

Select an option

Save bpolaszek/b0b28276123cbe4f19d4fe05c10dcc91 to your computer and use it in GitHub Desktop.
Symfony / Doctrine tests/bootstrap.php
<?php
declare(strict_types=1);
namespace App\Tests;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Process\Process;
use function dirname;
use function file_exists;
use function function_exists;
use function getenv;
use function is_readable;
use function is_writable;
use function method_exists;
require dirname(__DIR__).'/vendor/autoload.php';
if (method_exists(Dotenv::class, 'bootEnv')) {
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
}
function is_tty_available(): bool
{
return match (true) {
function_exists('posix_isatty') => \posix_isatty(\STDOUT),
\PHP_OS_FAMILY === 'Windows' => false !== getenv('TERM') || false !== getenv('ANSICON'),
default => file_exists('/dev/tty') && is_readable('/dev/tty') && is_writable('/dev/tty'),
};
}
function run_symfony_command(string $commandName, string ...$args): void
{
$useTty = is_tty_available();
$process = new Process([$_SERVER['_PHP'] ?? 'php', 'bin/console', $commandName, ...$args]);
$process->setTty($useTty);
$process->mustRun();
if (!$useTty) {
echo $process->getOutput();
}
}
// Set PHPUNIT_SKIP_BOOTSTRAP=1 to skip the bootstrap process
// if you're sure that your test database hasn't been altered.
if ($_SERVER['PHPUNIT_SKIP_BOOTSTRAP'] ?? false) {
return;
}
run_symfony_command('doctrine:database:drop', '--if-exists', '--force');
run_symfony_command('doctrine:database:create');
run_symfony_command('doctrine:migrations:migrate', '--no-interaction');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment