📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHPUnit Testing Integration Tests

Integration Tests

6 min read
Test multiple layers together using real or in-memory SQLite databases for integration testing.

Integration Tests

// Integration tests test multiple units working together
// Often uses a real (or test) database

class UserRegistrationTest extends TestCase {
    private PDO $pdo;

    protected function setUp(): void {
        $this->pdo = new PDO("sqlite::memory:");
        $this->pdo->exec("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT UNIQUE, password TEXT)");
    }

    public function testFullRegistrationFlow(): void {
        $repo    = new UserRepository($this->pdo);
        $hasher  = new PasswordHasher();
        $mailer  = $this->createMock(Mailer::class);
        $mailer->expects($this->once())->method("send");

        $service = new RegistrationService($repo, $hasher, $mailer);
        $user    = $service->register("Alice", "alice@example.com", "Secret123!");

        $this->assertInstanceOf(User::class, $user);
        $this->assertEquals("alice@example.com", $user->email);
        $this->assertNotEquals("Secret123!", $user->password); // should be hashed

        // Verify in DB
        $row = $this->pdo->query("SELECT * FROM users")->fetch();
        $this->assertEquals("alice@example.com", $row["email"]);
    }
}