TestCase.php 898 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests;
  4. use PDO;
  5. use PHPUnit\Framework\TestCase as PhpUnitTestCase;
  6. /**
  7. * Base TestCase with helpers for building an in-memory SQLite DB loaded with
  8. * the same migrations the app runs. Every test gets a fresh isolated database.
  9. */
  10. abstract class TestCase extends PhpUnitTestCase
  11. {
  12. protected function makeDb(): PDO
  13. {
  14. $pdo = new PDO('sqlite::memory:', null, null, [
  15. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  16. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  17. PDO::ATTR_EMULATE_PREPARES => false,
  18. ]);
  19. $pdo->exec('PRAGMA foreign_keys = ON');
  20. $sql = file_get_contents(__DIR__ . '/../migrations/001_init.sql');
  21. if ($sql === false) {
  22. $this->fail('Could not read migrations/001_init.sql');
  23. }
  24. $pdo->exec($sql);
  25. return $pdo;
  26. }
  27. }