Content · Web Development · PHP

PHPUnit: Testing Database Transactions

Write integration tests that assert the database state after commits and rollbacks. Rolling back each test transaction is unreliable for DDL and for code that commits

2 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Write integration tests that assert the database state after commits and rollbacks.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Write integration tests that assert the database state after commits and rollbacks.

Prerequisites


Use a dedicated development environment and sample data. Where a snippet uses an injected connection, repository, request value or mailer, supply it from your own application setup.

Example 1: Basic Use


A focused working pattern for this topic. Names such as $pdo, $authContext and $repo represent dependencies provided by the application unless initialized in the snippet.

<?php
final class DatabaseFeatureTest extends \PHPUnit\Framework\TestCase {
public function testRollbackKeepsOriginalBalance(): void {
    $db = new PDO('sqlite::memory:');
    $db->exec('CREATE TABLE wallet (balance INTEGER)');
    $db->exec('INSERT INTO wallet VALUES (50)');
    $db->beginTransaction();
    $db->exec('UPDATE wallet SET balance = 0');
    $db->rollBack();
    self::assertSame(50, (int) $db->query('SELECT balance FROM wallet')->fetchColumn());
}
}

Example 2: A Second Scenario


A second use case that adds a different operation or data flow specific to this topic.

<?php
final class AtomicityTest extends PHPUnit\Framework\TestCase {
    public function testCommittedInsertIsVisible(): void {
        $db = new PDO('sqlite::memory:'); $db->exec('CREATE TABLE items (id INTEGER)');
        $db->beginTransaction(); $db->exec('INSERT INTO items VALUES (4)'); $db->commit();
        self::assertSame(1, (int) $db->query('SELECT COUNT(*) FROM items')->fetchColumn());
    }
}

Example 3: Practical Checks


A practical validation, failure or deployment concern specific to this topic.

<?php
final class RollbackTest extends PHPUnit\Framework\TestCase {
    public function testRolledBackInsertDisappears(): void {
        $db = new PDO('sqlite::memory:'); $db->exec('CREATE TABLE items (id INTEGER)');
        $db->beginTransaction(); $db->exec('INSERT INTO items VALUES (7)'); $db->rollBack();
        self::assertSame(0, (int) $db->query('SELECT COUNT(*) FROM items')->fetchColumn());
    }
}

Important Behavior and Edge Cases


Rolling back each test transaction is unreliable for DDL and for code that commits independently.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
PHPUnit: Testing Database TransactionsWrite integration tests that assert the database state after commits and rollbacks.
Related API / patternUse this workflow as a focused starting point rather than a substitute for application-level tests.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesRolling back each test transaction is unreliable for DDL and for code that commits independently.
Using a related API interchangeablyUse this workflow as a focused starting point rather than a substitute for application-level tests.

Key Takeaways


  • Write integration tests that assert the database state after commits and rollbacks.
  • Rolling back each test transaction is unreliable for DDL and for code that commits independently.
  • Use this workflow as a focused starting point rather than a substitute for application-level tests.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.