Laravel testing
Skill fusengine/agents/plugins/laravel-expert/skills/laravel-testing
Write tests with Pest 4/PHPUnit 12, feature tests, unit tests, mocking, fakes, and factories. Use when testing controllers, services, models, or implementing TDD on Laravel 13.From its SKILL.md
npx -y skills add fusengine/agents --skill laravel-testingAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 22 stars22 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
6.7 KB, ~1.5k tokens by cl100k_base, as published. Nobody here has run it
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 --parallelfor 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([
['[email protected]', 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
RefreshDatabasetrait - 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
Laravel 13 Notes
PHPUnit 12 + Pest 4
Laravel 13 requires PHPUnit 12 and supports Pest 4. PHP attributes replace docblock annotations.
use PHPUnit\Framework\Attributes\Test;
use Illuminate\Foundation\Testing\Attributes\Seed;
use Illuminate\Foundation\Testing\Attributes\Seeder;
#[Seed] // runs DatabaseSeeder
#[Seeder(UserSeeder::class)] // runs a targeted seeder
final class UserTest extends TestCase
{
#[Test]
public function it_creates_user(): void { /* ... */ }
}
Str cache reset
Laravel 13 automatically resets Str caches (random, slug) between tests to avoid state leak. No manual setup required.
Migration from Pest 3
pest --initregeneratesPest.phpwith the new API- Datasets now support native PHP generators
expect()->toBeInstanceOf()→ strict typing required
What ships with it: 20 files
71.6 KB alongside SKILL.md
references/
- console-tests.md2.3 KB
- database-assertions.md2.7 KB
- database-basics.md2.1 KB
- database-factories.md2.9 KB
- http-assertions.md2.8 KB
- http-auth.md2.6 KB
- http-json.md2.7 KB
- http-requests.md2.6 KB
- mocking-fakes.md3.2 KB
- mocking-http.md2.8 KB
- mocking-services.md2.7 KB
- pest-arch.md2.7 KB
- pest-basics.md2.2 KB
- pest-datasets.md2.8 KB
- templates/ApiTest.php.md7.8 KB
- templates/ArchTest.php.md6.0 KB
- templates/FeatureTest.php.md5.6 KB
- templates/PestConfig.php.md6.1 KB
- templates/UnitTest.php.md6.1 KB
- troubleshooting.md2.7 KB