You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/Support/TrafficTestDatabase.php

38 lines
1.4 KiB
PHTML

<?php
use Illuminate\Database\Capsule\Manager as Capsule;
/** Two independent connections. Opt in to InnoDB using a local test server socket. */
function trafficTestDatabase(): Capsule
{
$socket = getenv('VIRT_TRAFFIC_TEST_MYSQL_SOCKET');
if ($socket) {
$admin = new PDO('mysql:unix_socket=' . $socket, get_current_user() ?: 'root');
$database = 'virt_traffic_test_' . bin2hex(random_bytes(8));
$admin->exec("CREATE DATABASE `$database`");
register_shutdown_function(static fn () => $admin->exec("DROP DATABASE `$database`"));
$config = ['driver' => 'mysql', 'unix_socket' => $socket,
'database' => $database, 'username' => get_current_user() ?: 'root'];
} else {
$path = tempnam(sys_get_temp_dir(), 'virt-traffic-test-');
$config = ['driver' => 'sqlite', 'database' => $path];
register_shutdown_function(static function () use ($path): void {
foreach ([$path, $path . '-wal', $path . '-shm'] as $file) {
if (is_file($file)) {
unlink($file);
}
}
});
}
$capsule = new Capsule();
foreach (['default', 'peer'] as $name) {
$capsule->addConnection($config, $name);
}
$capsule->setAsGlobal();
$capsule->bootEloquent();
if (!$socket) {
$capsule->getConnection()->statement('PRAGMA journal_mode=WAL');
}
return $capsule;
}