mcpField
L
laravel-testing

laravel-testing

github.com/fusengineTesting & quality3486 installsUpdated 2d ago
Repository

About laravel-testing

laravel-testing is a testing skill for Laravel developers, covering feature, unit, and architecture tests with Pest and PHPUnit. It also provides guidance for HTTP, database, mocking, and Artisan command tests.

Key features

  1. 01Feature, unit, and architecture tests
  2. 02Pest syntax and datasets
  3. 03HTTP and JSON assertions
  4. 04Database, factories, and assertions
  5. 05Service, event, and HTTP mocking
  6. 06Artisan command tests

Use cases

  1. 01Validate HTTP endpoints and full-stack flows
  2. 02Test service or policy logic
  3. 03Check code architecture rules
  4. 04Fake external APIs, mail, queues, and events

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/fusengine/agents --skill laravel-testing

After installing, type/laravel-testingin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
console-tests.md
database-assertions.md
database-basics.md
database-factories.md
http-assertions.md
http-auth.md
http-json.md
http-requests.md
mocking-fakes.md
mocking-http.md
mocking-services.md
pest-arch.md
pest-basics.md
pest-datasets.md
troubleshooting.md
references/templates/
ApiTest.php.md
ArchTest.php.md
FeatureTest.php.md
PestConfig.php.md
UnitTest.php.md

Details

From the project README
Covers Laravel 13 testing with Pest 5 / PHPUnit 13 (Pest 4 / PHPUnit 12 on PHP 8.3): feature tests (HTTP, full stack), unit tests (isolated classes), and architecture tests; Pest syntax (it/test/describe), datasets; HTTP testing (requests, JSON assertions, auth/actingAs, status/redirect assertions); database testing (RefreshDatabase, factories, DB assertions); mocking (services, spies, Mail/Queue/Event fakes, Http::fake, time travel); console/Artisan command tests; and PHPUnit-attribute-based seeding (#[Seed], #[Seeder]).

Laravel Testing

Agent Workflow (MANDATORY)

Before ANY implementation, spawn 3 agents in parallel, one Agent call each with a name:

  1. fuse-ai-pilot:explore-codebase - Analyze existing test patterns
  2. fuse-ai-pilot:research-expert - Verify Pest/PHPUnit docs via Context7
  3. mcp__context7__query-docs - Check assertion and mocking patterns

After implementation, run fuse-ai-pilot:sniper for validation.


Overview

TypePurposeLocation
FeatureHTTP, full stacktests/Feature/
UnitIsolated classestests/Unit/
ArchCode architecturetests/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

  1. Use RefreshDatabase for database isolation
  2. Use factories for test data (never raw inserts)
  3. Mock external services - Never call real APIs
  4. Test edge cases - Empty, null, boundaries
  5. Run parallel - pest --parallel for speed

Reference Guide

Pest Basics
TopicReferenceWhen to Consult
Pest Syntaxpest-basics.mdit(), test(), describe()
Datasetspest-datasets.mdData providers, hooks
Architecturepest-arch.mdarch() tests
HTTP Testing
TopicReferenceWhen to Consult
Requestshttp-requests.mdGET, POST, headers
JSON APIhttp-json.mdAPI assertions
Authenticationhttp-auth.mdactingAs, guards
Assertionshttp-assertions.mdStatus, redirects
Database Testing
TopicReferenceWhen to Consult
Basicsdatabase-basics.mdRefreshDatabase
Factoriesdatabase-factories.mdFactory patterns
Assertionsdatabase-assertions.mdDB assertions
Mocking
TopicReferenceWhen to Consult
Servicesmocking-services.mdMock, spy
Fakesmocking-fakes.mdMail, Queue, Event
HTTP & Timemocking-http.mdHttp::fake, travel
Other
TopicReferenceWhen to Consult
Consoleconsole-tests.mdArtisan tests
Troubleshootingtroubleshooting.mdCommon errors
Templates
TemplateWhen to Use
FeatureTest.php.mdHTTP feature test
UnitTest.php.mdService unit test
ArchTest.php.mdArchitecture test
ApiTest.php.mdREST API test
PestConfig.php.mdPest 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 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

Laravel 13 Notes

PHPUnit 12/13 + Pest 4/5

The Laravel 13 skeleton ships PHPUnit 12 (^12.5); the framework also accepts PHPUnit 13. Pest 5 (current, requires PHP 8.4+ and PHPUnit 13, plus pestphp/pest-plugin-laravel ^5.0 which needs Laravel ^13.23) — stay on Pest 4 (PHPUnit 12) if the app still runs PHP 8.3. 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 factories reset

Laravel 13 resets custom Str factories (UUID / ULID / random string) during test teardown. Tests relying on a factory persisting across methods must set it in each test or setup hook.

#[UnitTest] (L13)

Illuminate\Foundation\Testing\Attributes\UnitTest on a test method skips booting the application for that test.

Migration from Pest 3 → 4
  • Bump pestphp/pest and all Pest plugins to ^4.0 (PHP 8.3+, PHPUnit 12)
  • Snapshot names changed: run ./vendor/bin/pest --update-snapshots
  • pest-plugin-watch and pest-plugin-faker are archived
Migration from Pest 4 → 5
  • Requires PHP 8.4+; bump pestphp/pest and all Pest plugins to ^5.0 (built on PHPUnit 13)
  • New in 5: --tia (test impact analysis, local only), first-party PHPStan/Rector plugins, Agent and Evals plugins, time-balanced sharding

Comments

Markdown supported