Skip to content

Tests: isolated binktermphp_test database harness with a fail-closed guard - #506

Open
SkrawlCO wants to merge 1 commit into
awehttam:claudesbbsfrom
SkrawlCO:upstream/up-031-isolated-test-database
Open

SkrawlCO wants to merge 1 commit into
awehttam:claudesbbsfrom
SkrawlCO:upstream/up-031-isolated-test-database

Conversation

@SkrawlCO

@SkrawlCO SkrawlCO commented Oct 5, 2026

Copy link
Copy Markdown

TITLE: Tests: isolated binktermphp_test database harness with a fail-closed guard

Problem

Database-backed unit tests reach the database through Database::getInstance() or the application's .env DB_NAME, which is the configured application database. For example, UserManagerCreateTest drives scripts/user-manager.php against DB_NAME. There is no isolated test database and nothing stops a test run from writing to a live board's database.

Impact

Running the unit suite on a machine with a configured board risks writing fixtures into production data. Contributors cannot safely add tests for database-backed behavior.

Repair

  • tests/Unit/Support/TestDatabase.php: the shared way to get a PDO for the isolated binktermphp_test database. It uses DB_HOST/DB_PORT/DB_USER/DB_PASS, never reads DB_NAME, never calls Database::getInstance(), and verifies current_database() before returning the connection.
  • Database::setInstanceForTesting(PDO $pdo): routes every Database::getInstance() call, including those inside production code under test, to that connection. It checks current_database() itself, independently of the helper, and refuses anything except exactly binktermphp_test, leaving the existing singleton untouched. Database::resetInstanceForTesting() drops the singleton.
  • Production code paths are unchanged; the new methods are test-only.

Proof

  • tests/Unit/DatabaseTestIsolationTest.php, with no database server (PDO stubs):

    • connections reporting binktermphp, postgres, binktermphp_test_copy or an empty name are refused, and the singleton is unchanged;
    • a binktermphp_test connection is installed and session-initialized (SET TIME ZONE 'UTC');
    • reset clears the singleton.

    It errors on the current branch and passes with the change.

  • tests/Unit/TestDatabaseConnectionTest.php is the end-to-end check against a real binktermphp_test database. It skips when pdo_pgsql or the database is unavailable. (Proof against a real PostgreSQL instance is recorded separately.)

  • The rest of tests/Unit is unchanged.

…guard

Database-backed tests reached the configured application database, so
running the unit suite on a configured board could write into it.

Add TestDatabase (connects only to binktermphp_test and verifies it) and
Database::setInstanceForTesting()/resetInstanceForTesting(), which
independently refuse any connection that is not binktermphp_test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant