50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MeshVault\Tests\Integration;
|
|
|
|
use PDO;
|
|
use Phinx\Config\Config;
|
|
use Phinx\Migration\Manager;
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
use Symfony\Component\Console\Output\NullOutput;
|
|
|
|
/** Runs the production migrations against SQLite and manages scratch directories. */
|
|
final class RepositoryDatabase
|
|
{
|
|
/** Never a hand-maintained test schema: drift must fail the suite. */
|
|
public static function migrate(string $path): PDO
|
|
{
|
|
$pdo = new PDO('sqlite:' . $path, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
|
$config = new Config([
|
|
'paths' => ['migrations' => dirname(__DIR__, 2) . '/db/migrations'],
|
|
'environments' => [
|
|
'default_migration_table' => 'phinxlog',
|
|
'default_database' => 'test',
|
|
'test' => ['adapter' => 'sqlite', 'connection' => $pdo],
|
|
],
|
|
]);
|
|
(new Manager($config, new ArrayInput([]), new NullOutput()))->migrate('test');
|
|
$pdo->exec('PRAGMA foreign_keys = ON');
|
|
$pdo->exec('PRAGMA busy_timeout = 5000');
|
|
return $pdo;
|
|
}
|
|
|
|
public static function directory(string $prefix): string
|
|
{
|
|
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'meshvault-' . $prefix . '-' . bin2hex(random_bytes(6));
|
|
mkdir($path, 0777, true);
|
|
return (string) realpath($path);
|
|
}
|
|
|
|
public static function remove(string $path): void
|
|
{
|
|
foreach (array_diff(@scandir($path) ?: [], ['.', '..']) as $entry) {
|
|
$child = $path . DIRECTORY_SEPARATOR . $entry;
|
|
is_dir($child) ? self::remove($child) : @unlink($child);
|
|
}
|
|
@rmdir($path);
|
|
}
|
|
}
|