Laravel Testing
Agent Workflow (MANDATORY)
Before ANY implementation, launch in parallel:
- fuse-ai-pilot:explore-codebase - Analyze existing test patterns
- fuse-ai-pilot:research-expert - Verify Pest/PHPUnit docs via Context7
- mcp__context7__query-docs - Check assertion and mocking patterns
After implementation, run fuse-ai-pilot:sniper for validation.
Overview
| Type |
Purpose |
Location |
| Feature |
HTTP, full stack |
tests/Feature/ |
| Unit |
Isolated classes |
tests/Unit/ |
| Arch |
Code architecture |
tests/Arch.php |
Decision Guide: Test Type
What to test?
├── HTTP endpoint → Feature test
├── Service/Policy logic → Unit test
├── Code structure → Arch test
├── External API → Mock with Http::fake()
├── Mail/Queue/Event → Use Fakes
└── Database state → assertDatabaseHas()
Decision Guide: Test Strategy
Coverage strategy?
├── Feature tests (70%) → Critical flows
├── Unit tests (25%) → Business logic
├── E2E tests (5%) → User journeys
└── Arch tests → Structural rules
Critical Rules
- Use RefreshDatabase for database isolation
- Use factories for test data (never raw inserts)
- Mock external services - Never call real APIs
- Test edge cases - Empty, null, boundaries
- Run parallel -
pest --parallel for speed
Reference Guide
Pest Basics
| Topic |
Reference |
When to Consult |
| Pest Syntax |
pest-basics.md |
it(), test(), describe() |
| Datasets |
pest-datasets.md |
Data providers, hooks |
| Architecture |
pest-arch.md |
arch() tests |
HTTP Testing
| Topic |
Reference |
When to Consult |
| Requests |
http-requests.md |
GET, POST, headers |
| JSON API |
http-json.md |
API assertions |
| Authentication |
http-auth.md |
actingAs, guards |
| Assertions |
http-assertions.md |
Status, redirects |
Database Testing
| Topic |
Reference |
When to Consult |
| Basics |
database-basics.md |
RefreshDatabase |
| Factories |
database-factories.md |
Factory patterns |
| Assertions |
database-assertions.md |
DB assertions |
Mocking
| Topic |
Reference |
When to Consult |
| Services |
mocking-services.md |
Mock, spy |
| Fakes |
mocking-fakes.md |
Mail, Queue, Event |
| HTTP & Time |
mocking-http.md |
Http::fake, travel |
Other
| Topic |
Reference |
When to Consult |
| Console |
console-tests.md |
Artisan tests |
| Troubleshooting |
troubleshooting.md |
Common errors |
Templates
| Template |
When to Use |
| FeatureTest.php.md |
HTTP feature test |
| UnitTest.php.md |
Service unit test |
| ArchTest.php.md |
Architecture test |
| ApiTest.php.md |
REST API test |
| PestConfig.php.md |
Pest configuration |
Quick Reference
// Feature test
it('creates a post', function () {
$user = User::factory()->create();
$this->actingAs($user)
->postJson('/api/posts', ['title' => 'Test'])
->assertCreated()
->assertJsonPath('data.title', 'Test');
$this->assertDatabaseHas('posts', ['title' => 'Test']);
});
// With dataset
it('validates emails', function (string $email, bool $valid) {
// test logic
})->with([
['valid@test.com', true],
['invalid', false],
]);
// Mock facade
Mail::fake();
// ... action ...
Mail::assertSent(OrderShipped::class);
Commands
# Run all tests
php artisan test
# Pest directly
./vendor/bin/pest
# Parallel execution
./vendor/bin/pest --parallel
# Filter by name
./vendor/bin/pest --filter "user can"
# Coverage
./vendor/bin/pest --coverage --min=80
# Profile slow tests
./vendor/bin/pest --profile
Best Practices
DO
- Use
RefreshDatabase trait
- Follow AAA pattern (Arrange-Act-Assert)
- Name tests descriptively
- Test one thing per test
- Use factories for data
DON'T
- Create test dependencies
- Call real external APIs
- Use production database
- Skip edge cases
1---2name: laravel-testing3description: Write tests with Pest 3/PHPUnit, feature tests, unit tests, mocking, fakes, and factories. Use when testing controllers, services, models, or implementing TDD.4---5
6# Laravel Testing
7
8## Agent Workflow (MANDATORY)
9
10Before ANY implementation, launch in parallel:
11
121. **fuse-ai-pilot:explore-codebase** - Analyze existing test patterns
132. **fuse-ai-pilot:research-expert** - Verify Pest/PHPUnit docs via Context7
143. **mcp__context7__query-docs** - Check assertion and mocking patterns
15
16After implementation, run **fuse-ai-pilot:sniper** for validation.
17
18---
19
20## Overview
21
22| Type | Purpose | Location |
23|------|---------|----------|
24| **Feature** | HTTP, full stack | `tests/Feature/` |
25| **Unit** | Isolated classes | `tests/Unit/` |
26| **Arch** | Code architecture | `tests/Arch.php` |
27
28---
29
30## Decision Guide: Test Type
31
32```
33What to test?
34├── HTTP endpoint → Feature test
35├── Service/Policy logic → Unit test
36├── Code structure → Arch test
37├── External API → Mock with Http::fake()
38├── Mail/Queue/Event → Use Fakes
39└── Database state → assertDatabaseHas()
40```
41
42---
43
44## Decision Guide: Test Strategy
45
46```
47Coverage strategy?
48├── Feature tests (70%) → Critical flows
49├── Unit tests (25%) → Business logic
50├── E2E tests (5%) → User journeys
51└── Arch tests → Structural rules
52```
53
54---
55
56## Critical Rules
57
581. **Use RefreshDatabase** for database isolation
592. **Use factories** for test data (never raw inserts)
603. **Mock external services** - Never call real APIs
614. **Test edge cases** - Empty, null, boundaries
625. **Run parallel** - `pest --parallel` for speed
63
64---
65
66## Reference Guide
67
68### Pest Basics
69
70| Topic | Reference | When to Consult |
71|-------|-----------|-----------------|
72| **Pest Syntax** | [pest-basics.md](references/pest-basics.md) | it(), test(), describe() |
73| **Datasets** | [pest-datasets.md](references/pest-datasets.md) | Data providers, hooks |
74| **Architecture** | [pest-arch.md](references/pest-arch.md) | arch() tests |
75
76### HTTP Testing
77
78| Topic | Reference | When to Consult |
79|-------|-----------|-----------------|
80| **Requests** | [http-requests.md](references/http-requests.md) | GET, POST, headers |
81| **JSON API** | [http-json.md](references/http-json.md) | API assertions |
82| **Authentication** | [http-auth.md](references/http-auth.md) | actingAs, guards |
83| **Assertions** | [http-assertions.md](references/http-assertions.md) | Status, redirects |
84
85### Database Testing
86
87| Topic | Reference | When to Consult |
88|-------|-----------|-----------------|
89| **Basics** | [database-basics.md](references/database-basics.md) | RefreshDatabase |
90| **Factories** | [database-factories.md](references/database-factories.md) | Factory patterns |
91| **Assertions** | [database-assertions.md](references/database-assertions.md) | DB assertions |
92
93### Mocking
94
95| Topic | Reference | When to Consult |
96|-------|-----------|-----------------|
97| **Services** | [mocking-services.md](references/mocking-services.md) | Mock, spy |
98| **Fakes** | [mocking-fakes.md](references/mocking-fakes.md) | Mail, Queue, Event |
99| **HTTP & Time** | [mocking-http.md](references/mocking-http.md) | Http::fake, travel |
100
101### Other
102
103| Topic | Reference | When to Consult |
104|-------|-----------|-----------------|
105| **Console** | [console-tests.md](references/console-tests.md) | Artisan tests |
106| **Troubleshooting** | [troubleshooting.md](references/troubleshooting.md) | Common errors |
107
108### Templates
109
110| Template | When to Use |
111|----------|-------------|
112| [FeatureTest.php.md](references/templates/FeatureTest.php.md) | HTTP feature test |
113| [UnitTest.php.md](references/templates/UnitTest.php.md) | Service unit test |
114| [ArchTest.php.md](references/templates/ArchTest.php.md) | Architecture test |
115| [ApiTest.php.md](references/templates/ApiTest.php.md) | REST API test |
116| [PestConfig.php.md](references/templates/PestConfig.php.md) | Pest configuration |
117
118---
119
120## Quick Reference
121
122```php
123// Feature test
124it('creates a post', function () {
125 $user = User::factory()->create();
126
127 $this->actingAs($user)
128 ->postJson('/api/posts', ['title' => 'Test'])
129 ->assertCreated()
130 ->assertJsonPath('data.title', 'Test');
131
132 $this->assertDatabaseHas('posts', ['title' => 'Test']);
133});
134
135// With dataset
136it('validates emails', function (string $email, bool $valid) {
137 // test logic
138})->with([
139 ['valid@test.com', true],
140 ['invalid', false],
141]);
142
143// Mock facade
144Mail::fake();
145// ... action ...
146Mail::assertSent(OrderShipped::class);
147```
148
149---
150
151## Commands
152
153```bash
154# Run all tests
155php artisan test
156
157# Pest directly
158./vendor/bin/pest
159
160# Parallel execution
161./vendor/bin/pest --parallel
162
163# Filter by name
164./vendor/bin/pest --filter "user can"
165
166# Coverage
167./vendor/bin/pest --coverage --min=80
168
169# Profile slow tests
170./vendor/bin/pest --profile
171```
172
173---
174
175## Best Practices
176
177### DO
178- Use `RefreshDatabase` trait
179- Follow AAA pattern (Arrange-Act-Assert)
180- Name tests descriptively
181- Test one thing per test
182- Use factories for data
183
184### DON'T
185- Create test dependencies
186- Call real external APIs
187- Use production database
188- Skip edge cases