agentleFS
kwhornekwhorne/elyra-skills/skills/laravel-testing/SKILL.md

laravel-testing

Skill1 starsChanged 6 months agoMITView on GitHub

Test Laravel applications effectively with Pest or PHPUnit - feature vs unit boundaries, factories, database strategy, what to fake, and HTTP/Livewire assertions. Use when writing tests for Laravel code, reviewing a Laravel test suite, deciding how to test a controller, job, or command, or when tests are slow or brittle.

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Never seed global fixtures the whole suite shares — each test builds its own world

    line 58

“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Laravel Testing

In Laravel, the feature test is the workhorse: hit the route, assert the response and the database. Unit tests are for real logic, not for ceremony.

When to use

  • Writing tests for Laravel controllers, jobs, commands, actions, or models
  • Reviewing a Laravel test suite (coverage, speed, brittleness)
  • "How should I test this?" for anything framework-coupled
  • Tests are slow, flaky, or break on every refactor

Principles

  • Test through the front door. $this->post(route(...)) exercises routing, middleware, validation, authorization, and persistence in one honest test.
  • Fake the edges, not the middle. Mail::fake(), Queue::fake(), Http::fake(), Storage::fake() — but don't mock your own repositories and services into a tautology.
  • Factories are the vocabulary. States (->suspended(), ->paid()) make tests read like specifications.
  • Each test = one behavior. Arrange, act, assert — and assert the outcome (DB row, response, dispatched job), not the implementation path.

Process

1. Choose the test type

Code under testType
Route/controller behavior, auth, validationFeature (HTTP test)
Pure logic: calculators, value objects, rulesUnit (no framework boot if possible)
Queued jobFeature-style: instantiate, handle(), assert outcomes
Artisan command$this->artisan(...)->assertExitCode(0) + DB asserts
Livewire componentLivewire::test(...) — state, actions, events, validation
Blade/UI flows across pagesFew, high-value browser tests (Dusk/Pest browser)

2. Set the database strategy

  • RefreshDatabase + SQLite :memory: for speed — unless you use DB-specific features (JSON columns, full-text, triggers); then test against the real engine, matching production
  • LazilyRefreshDatabase skips migration cost for tests that never touch the DB

3. Build with factories

$user = User::factory()->create();
$order = Order::factory()->for($user)->paid()->create();   // states > attribute soup
  • Define states in the factory for every domain status the tests mention
  • make() when persistence isn't needed; create() only when the test reads it back
  • Never seed global fixtures the whole suite shares — each test builds its own world

4. Write the feature test

it('refunds a paid order', function () {
    Queue::fake();
    $order = Order::factory()->paid()->create();

    $this->actingAs($order->user)
        ->post(route('orders.refund', $order))
        ->assertRedirect()
        ->assertSessionHas('status');

    expect($order->fresh()->status)->toBe(OrderStatus::Refunded);
    Queue::assertPushed(ProcessRefund::class, fn ($job) => $job->orderId === $order->id);
});

Cover per endpoint: happy path, validation failure (assertSessionHasErrors / assertJsonValidationErrors), authorization (wrong user → 403), and the relevant edge case. Auth tests are the ones that prevent incidents.

5. Fake the boundaries

  • Http::fake([...]) with per-URL responses; Http::assertSent(...) for contract checks; add Http::preventStrayRequests() suite-wide
  • Time: $this->travel(3)->days() / Carbon::setTestNow() for anything date-dependent
  • Event::fake() narrowly (Event::fake([OrderShipped::class])) — global fakes silence model events your code depends on

6. Keep it fast and honest

php artisan test --parallel          # paratest
php artisan test --profile           # find the slow ones
  • Suite target: minutes, not tens of minutes; single test: milliseconds unless it's a browser test
  • A test that breaks when you rename a private method tests the wrong thing — assert outcomes

Output format

## Tests: <feature/area>

**Type mix:** N feature / M unit / K browser — why.
**DB strategy:** RefreshDatabase + <engine>.

### Coverage
| Behavior | Test |
|----------|------|
| happy path | ✅ … |
| validation | ✅ … |
| authorization | ✅ … |
| <edge case> | ✅ … |

**Faked:** Queue, Http (+preventStrayRequests). **Deliberately real:** …

Anti-patterns

  • ❌ Mocking Eloquent models or your own services so the test asserts the mock, not the behavior
  • ❌ Unit-testing a controller with mocked request/response instead of one feature test
  • ❌ Skipping authorization tests — the most expensive gap in any Laravel suite
  • ❌ Shared seeders creating hidden coupling between tests
  • ❌ SQLite in tests + MySQL JSON queries in production = green suite, broken prod
  • ❌ Event::fake() globally, then wondering why observers "don't work" in tests
  • ❌ Asserting assertStatus(200) and nothing else — the page can be 200 and wrong

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.