Skip to content

Lesson 6 of 9 · 26m

Database Testing with Transactions and Real Constraints

Rollback-per-test isolation, migration strategy, SQLite versus production engines, and the constraint tests teams forget until data corrupts.

Database tests fail in two memorable ways. Either they are so isolated that they miss real SQL behavior, or they leak state and depend on execution order. Professional PHP suites use transactions for speed and determinism, and they are honest about which database engine they are actually testing.

Wrap each test in a transaction

The fastest reliable isolation pattern is begin in setup, roll back in teardown.

protected function setUp(): void
{
    $this->pdo = new PDO('mysql:host=127.0.0.1;dbname=app_test', 'root', 'secret', [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    ]);
    $this->pdo->beginTransaction();
}

protected function tearDown(): void { $this->pdo->rollBack(); }

Every test starts from the schema state plus whatever it inserts. No truncating between tests, no table ordering, no accidental dependence on the previous test.

Migrations are part of the test environment

The test database schema must come from migrations, not from a copy of production someone exported in 2023. Build it once per CI job or once per test process:

mysql -h 127.0.0.1 -uroot -psecret -e 'DROP DATABASE IF EXISTS app_test; CREATE DATABASE app_test'
php artisan migrate --database=testing --force
# or: bin/console doctrine:migrations:migrate --env=test --no-interaction

If your framework supports parallel migration databases or schema cloning, use it. Rebuilding the schema inside every test is how a 20-second suite becomes a 12-minute suite.

SQLite is a different database

SQLite is excellent for hydration and repository logic that uses portable SQL. It is not honest for features that depend on engine behavior:

  • FOR UPDATE row locking, transactional DDL, JSON operators, generated columns, collation, full-text search, and constraint edge cases

A useful compromise is two layers: fast SQLite repository tests for common behavior, plus a smaller MySQL or PostgreSQL suite for engine-specific behavior. Name them explicitly so nobody assumes coverage is broader than it is.

Test the constraints, not just the happy path

The most valuable database tests often prove what cannot happen:

public function test_email_must_be_unique(): void
{
    $repo->save(new User('ada@company.test', 'Ada'));
    $this->expectException(PDOException::class);
    $this->expectExceptionMessageMatches('/duplicate|unique/i');
    $repo->save(new User('ada@company.test', 'Other Ada'));
}

This test is not testing MySQL for the sake of it. It is testing that your migration and your repository agree on the invariant.

Field notes

  • Rollback isolation does not help code that commits its own transaction. Application transaction boundaries need design attention.
  • AUTO_INCREMENT values are not stable business data. Disable foreign-key checks only for schema setup, keep test credentials local, and make production configuration impossible by accident.

Was this lesson helpful?

Sign in to rate