* SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * See the LICENSE file in the project root for the full license text. */ declare(strict_types=1); namespace App\Tests; use PDO; use PHPUnit\Framework\TestCase as PhpUnitTestCase; /** * Base TestCase with helpers for building an in-memory SQLite DB loaded with * the same migrations the app runs. Every test gets a fresh isolated database. */ abstract class TestCase extends PhpUnitTestCase { protected function makeDb(): PDO { $pdo = new PDO('sqlite::memory:', null, null, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]); $pdo->exec('PRAGMA foreign_keys = ON'); // Apply every NNN_*.sql in order — matches the production Migrator. $dir = __DIR__ . '/../migrations'; $files = glob($dir . '/*.sql') ?: []; sort($files); foreach ($files as $file) { if (!preg_match('#/\d{3,}_[A-Za-z0-9_\-]+\.sql$#', $file)) { continue; } $sql = file_get_contents($file); if ($sql === false) { $this->fail("Could not read migration: {$file}"); } $pdo->exec($sql); } return $pdo; } }