Integration Testing
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);
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
33
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 9
Amazonbot 6
Google 4
Unknown AI 3
Ahrefs 2
SEMrush 2
Majestic 1
ChatGPT 1
How they use it
crawler 27
crawler_json 1
Related categories
⚡
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