diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..5eb5d0e --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,61 @@ + 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 = []; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..131ab9a --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,16 @@ +