Laravel Testing
Agent Workflow (MANDATORY)
Before ANY implementation, use TeamCreate to spawn 3 agents:
- 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-testing-33description: Write tests with Pest 3/PHPUnit, feature tests, unit tests, mocking, fakes, and factories. Use when testing controllers, services, models, or implementing TDD.4metadata:5 source: https://skillmd.com/skills/fusengine/laravel-testing-36---78# Laravel Testing910## Agent Workflow (MANDATORY)1112Before ANY implementation, use `TeamCreate` to spawn 3 agents:13141. **fuse-ai-pilot:explore-codebase** - Analyze existing test patterns152. **fuse-ai-pilot:research-expert** - Verify Pest/PHPUnit docs via Context7163. **mcp__context7__query-docs** - Check assertion and mocking patterns1718After implementation, run **fuse-ai-pilot:sniper** for validation.1920---2122## Overview2324| Type | Purpose | Location |25|------|---------|----------|26| **Feature** | HTTP, full stack | `tests/Feature/` |27| **Unit** | Isolated classes | `tests/Unit/` |28| **Arch** | Code architecture | `tests/Arch.php` |2930---3132## Decision Guide: Test Type3334```35What to test?36├── HTTP endpoint → Feature test37├── Service/Policy logic → Unit test38├── Code structure → Arch test39├── External API → Mock with Http::fake()40├── Mail/Queue/Event → Use Fakes41└── Database state → assertDatabaseHas()42```4344---4546## Decision Guide: Test Strategy4748```49Coverage strategy?50├── Feature tests (70%) → Critical flows51├── Unit tests (25%) → Business logic52├── E2E tests (5%) → User journeys53└── Arch tests → Structural rules54```5556---5758## Critical Rules59601. **Use RefreshDatabase** for database isolation612. **Use factories** for test data (never raw inserts)623. **Mock external services** - Never call real APIs634. **Test edge cases** - Empty, null, boundaries645. **Run parallel** - `pest --parallel` for speed6566---6768## Reference Guide6970### Pest Basics7172| Topic | Reference | When to Consult |73|-------|-----------|-----------------|74| **Pest Syntax** | [pest-basics.md](references/pest-basics.md) | it(), test(), describe() |75| **Datasets** | [pest-datasets.md](references/pest-datasets.md) | Data providers, hooks |76| **Architecture** | [pest-arch.md](references/pest-arch.md) | arch() tests |7778### HTTP Testing7980| Topic | Reference | When to Consult |81|-------|-----------|-----------------|82| **Requests** | [http-requests.md](references/http-requests.md) | GET, POST, headers |83| **JSON API** | [http-json.md](references/http-json.md) | API assertions |84| **Authentication** | [http-auth.md](references/http-auth.md) | actingAs, guards |85| **Assertions** | [http-assertions.md](references/http-assertions.md) | Status, redirects |8687### Database Testing8889| Topic | Reference | When to Consult |90|-------|-----------|-----------------|91| **Basics** | [database-basics.md](references/database-basics.md) | RefreshDatabase |92| **Factories** | [database-factories.md](references/database-factories.md) | Factory patterns |93| **Assertions** | [database-assertions.md](references/database-assertions.md) | DB assertions |9495### Mocking9697| Topic | Reference | When to Consult |98|-------|-----------|-----------------|99| **Services** | [mocking-services.md](references/mocking-services.md) | Mock, spy |100| **Fakes** | [mocking-fakes.md](references/mocking-fakes.md) | Mail, Queue, Event |101| **HTTP & Time** | [mocking-http.md](references/mocking-http.md) | Http::fake, travel |102103### Other104105| Topic | Reference | When to Consult |106|-------|-----------|-----------------|107| **Console** | [console-tests.md](references/console-tests.md) | Artisan tests |108| **Troubleshooting** | [troubleshooting.md](references/troubleshooting.md) | Common errors |109110### Templates111112| Template | When to Use |113|----------|-------------|114| [FeatureTest.php.md](references/templates/FeatureTest.php.md) | HTTP feature test |115| [UnitTest.php.md](references/templates/UnitTest.php.md) | Service unit test |116| [ArchTest.php.md](references/templates/ArchTest.php.md) | Architecture test |117| [ApiTest.php.md](references/templates/ApiTest.php.md) | REST API test |118| [PestConfig.php.md](references/templates/PestConfig.php.md) | Pest configuration |119120---121122## Quick Reference123124```php125// Feature test126it('creates a post', function () {127 $user = User::factory()->create();128129 $this->actingAs($user)130 ->postJson('/api/posts', ['title' => 'Test'])131 ->assertCreated()132 ->assertJsonPath('data.title', 'Test');133134 $this->assertDatabaseHas('posts', ['title' => 'Test']);135});136137// With dataset138it('validates emails', function (string $email, bool $valid) {139 // test logic140})->with([141 ['valid@test.com', true],142 ['invalid', false],143]);144145// Mock facade146Mail::fake();147// ... action ...148Mail::assertSent(OrderShipped::class);149```150151---152153## Commands154155```bash156# Run all tests157php artisan test158159# Pest directly160./vendor/bin/pest161162# Parallel execution163./vendor/bin/pest --parallel164165# Filter by name166./vendor/bin/pest --filter "user can"167168# Coverage169./vendor/bin/pest --coverage --min=80170171# Profile slow tests172./vendor/bin/pest --profile173```174175---176177## Best Practices178179### DO180- Use `RefreshDatabase` trait181- Follow AAA pattern (Arrange-Act-Assert)182- Name tests descriptively183- Test one thing per test184- Use factories for data185186### DON'T187- Create test dependencies188- Call real external APIs189- Use production database190- Skip edge cases