| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- 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;
- }
- }
|