1
0

TestCase.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /*
  3. * Copyright 2026 Alessandro Chiapparini <sprint_planer_web@chiapparini.org>
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * See the LICENSE file in the project root for the full license text.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Tests;
  12. use PDO;
  13. use PHPUnit\Framework\TestCase as PhpUnitTestCase;
  14. /**
  15. * Base TestCase with helpers for building an in-memory SQLite DB loaded with
  16. * the same migrations the app runs. Every test gets a fresh isolated database.
  17. */
  18. abstract class TestCase extends PhpUnitTestCase
  19. {
  20. protected function makeDb(): PDO
  21. {
  22. $pdo = new PDO('sqlite::memory:', null, null, [
  23. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  24. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  25. PDO::ATTR_EMULATE_PREPARES => false,
  26. ]);
  27. $pdo->exec('PRAGMA foreign_keys = ON');
  28. // Apply every NNN_*.sql in order — matches the production Migrator.
  29. $dir = __DIR__ . '/../migrations';
  30. $files = glob($dir . '/*.sql') ?: [];
  31. sort($files);
  32. foreach ($files as $file) {
  33. if (!preg_match('#/\d{3,}_[A-Za-z0-9_\-]+\.sql$#', $file)) {
  34. continue;
  35. }
  36. $sql = file_get_contents($file);
  37. if ($sql === false) {
  38. $this->fail("Could not read migration: {$file}");
  39. }
  40. $pdo->exec($sql);
  41. }
  42. return $pdo;
  43. }
  44. }