← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Integration Testing

testing PHP 5.0+ Intermediate

Also Known As

integration tests component testing service integration test

TL;DR

Tests that verify multiple components work correctly together — often involving real databases, HTTP clients, or third-party services.

Explanation

Integration tests verify that components interact correctly when combined — e.g., that a repository correctly persists and retrieves domain objects from a real database, or that an HTTP controller returns the expected response for a given request. They are slower and more expensive than unit tests but catch bugs that only emerge from component interactions. In PHP, integration tests typically use a test database, in-memory SQLite, or containers (via Docker or Laravel TestContainers). Follow the Test Pyramid: many unit tests, fewer integration tests, few E2E tests.

Diagram

flowchart TD
    subgraph Unit Tests - Isolated
        UT[Test business logic<br/>all dependencies mocked<br/>fast milliseconds]
    end
    subgraph Integration Tests - Real Dependencies
        IT[Test component interactions<br/>real DB real cache real queue<br/>slower seconds]
    end
    subgraph What Integration Tests Catch
        N1[N+1 query bugs<br/>mocks hide these]
        N2[Transaction isolation issues]
        N3[ORM mapping problems]
        N4[Migration correctness]
    end
    IT --> N1 & N2 & N3 & N4
    style UT fill:#238636,color:#fff
    style IT fill:#d29922,color:#fff

Common Misconception

Integration tests are just slow unit tests and should be minimised. Integration tests verify that components work together — they catch contract mismatches between layers that unit tests with mocks miss entirely. A test pyramid needs both, with unit tests forming the base.

Why It Matters

Integration tests verify that components work correctly together — they catch the mismatches between units that pass individually but fail when combined. A suite of only unit tests gives false confidence about real behaviour.

Common Mistakes

  • Using real external services (payment APIs, email providers) in integration tests — use sandbox environments or contract tests.
  • Not isolating test data — tests that share database state interfere with each other and produce random failures.
  • Making integration tests so broad they take minutes to run — they should be slower than unit tests but not by orders of magnitude.
  • Not running integration tests in CI — they catch the bugs that matter most.

Code Examples

💡 Note
Integration tests hit real infrastructure (database, queue) — use a test database and fakes for external services.
✗ Vulnerable
// Mock that doesn't match real behaviour:
$mockRepo = $this->createMock(UserRepository::class);
$mockRepo->method('findByEmail')->willReturn($user); // Always returns a user
// Real DB: findByEmail throws on DB connection failure
// Integration test catches what the mock hides
✓ Fixed
class OrderApiTest extends TestCase {
    use RefreshDatabase; // reset DB per test

    public function test_place_order_stores_and_emails(): void {
        Mail::fake();
        $user = User::factory()->create();

        $response = $this->actingAs($user)
            ->postJson('/api/orders', ['sku' => 'WIDGET-1', 'qty' => 2]);

        $response->assertStatus(201);
        $this->assertDatabaseHas('orders', ['user_id' => $user->id]);
        Mail::assertSent(OrderConfirmation::class);
    }
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 33
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 2 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Perplexity 9 Amazonbot 6 Google 4 Unknown AI 3 Ahrefs 2 SEMrush 2 Majestic 1 ChatGPT 1
crawler 27 crawler_json 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Run integration tests against a real database in a Docker container in CI — use transactions with rollback to keep tests isolated and fast
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
No integration test suite; only unit tests; business logic untested against real DB schema
Auto-detectable: ✗ No phpunit pest docker
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File Tests: Update

✓ schema.org compliant