test: add base TestCase and bootstrap for in-memory SQLite
This commit is contained in:
61
tests/TestCase.php
Normal file
61
tests/TestCase.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Libs\Database;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\TestCase as FrameworkTestCase;
|
||||
|
||||
/**
|
||||
* TestCase - DuckBrain test harness
|
||||
*
|
||||
* Base class for the framework's own tests. Deliberately lightweight: this
|
||||
* repo has no migrations/ nor duckbrain-commands, so each test defines its
|
||||
* schema with raw DDL through createTable(), and tables registered there are
|
||||
* dropped automatically after the class finishes.
|
||||
*/
|
||||
abstract class TestCase extends FrameworkTestCase
|
||||
{
|
||||
/**
|
||||
* @var list<string> Tables created through createTable() for this class.
|
||||
*/
|
||||
private static array $tables = [];
|
||||
|
||||
/**
|
||||
* Returns the PDO connection to the in-memory test database.
|
||||
*
|
||||
* @return PDO
|
||||
*/
|
||||
protected static function db(): PDO
|
||||
{
|
||||
return Database::getInstance(DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a table in the test database and registers it for cleanup.
|
||||
*
|
||||
* @param string $name
|
||||
* Table name.
|
||||
*
|
||||
* @param string $columns
|
||||
* Raw column definition, as accepted by the test
|
||||
* engine (sqlite): "id INTEGER PRIMARY KEY, x TEXT".
|
||||
*/
|
||||
protected static function createTable(string $name, string $columns): void
|
||||
{
|
||||
static::db()->exec("CREATE TABLE IF NOT EXISTS {$name} ({$columns})");
|
||||
static::$tables[] = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops every table registered through createTable() during this class.
|
||||
*/
|
||||
public static function tearDownAfterClass(): void
|
||||
{
|
||||
foreach (array_unique(static::$tables) as $table) {
|
||||
static::db()->exec("DROP TABLE IF EXISTS {$table}");
|
||||
}
|
||||
|
||||
static::$tables = [];
|
||||
}
|
||||
}
|
||||
16
tests/bootstrap.php
Normal file
16
tests/bootstrap.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
// Test bootstrap for the DuckBrain development harness.
|
||||
// Run PHPUnit from the project root (autoload.php resolves config.php via cwd).
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// Define DB constants BEFORE config.php so the test database wins over the
|
||||
// real configuration (config.php's define() warnings are suppressed by @).
|
||||
define('DB_TYPE', 'sqlite');
|
||||
define('DB_HOST', 'localhost');
|
||||
define('DB_NAME', ':memory:');
|
||||
define('DB_USER', '');
|
||||
define('DB_PASS', '');
|
||||
|
||||
@require_once __DIR__ . '/../autoload.php';
|
||||
Reference in New Issue
Block a user