laravel-agent-rules / tests
CLAUDE.md9 starsChanged 2 months agoMITView on GitHub
Default test framework: Pest. Pest is required for new tests in projects using this ruleset; PHPUnit-style assertions still work under the hood. For boundary/validation tests that vary only by inputs, SHOULD use a Pest dataset rather than copy-pasting near-identical tests:
- Reads credentials
In this repository7 other files it gives its agents.
- app/CLAUDE.mdCLAUDE.md
- bootstrap/CLAUDE.mdCLAUDE.md
- CLAUDE.mdCLAUDE.md
- config/CLAUDE.mdCLAUDE.md
- database/CLAUDE.mdCLAUDE.md
- lang/CLAUDE.mdCLAUDE.md
- routes/CLAUDE.mdCLAUDE.md
The file
Show its rules and text
We split this CLAUDE.md into the rules it gives an agent
13 rules. About 0 are common, 7 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈7
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 3
Default test framework: Pest.
- Very specific: line 7
SHOULD: run the suite against an in-memory SQLite database (DB_CONNECTION=sqlite, DB_DATABASE=:memory: in phpunit.xml / .env.testing) for …
- Very specific: line 8
MUST: use LazilyRefreshDatabase, RefreshDatabase, or DatabaseTransactions so each database-using test starts from a clean schema.
- Distinctive: line 12
MUST: write a test for every class that contains logic (Actions, Support, Jobs, Commands, Livewire components, Controllers, Policies).
- Very specific: line 13
MUST: write a test for every route exposed to users (auth/redirect/permission boundaries).
- Distinctive: line 14
SHOULD: use coverage reports to find untested behaviour, not as a numeric gate agents grind toward.
- Distinctive: line 32
MUST: use lowercase, descriptive names that read like sentences.
- Distinctive: line 33
MUST: only assert things relevant to the class under test — keep assertions focused.
- Very specific: line 92
Import Illuminate\Support\Facades\Validator.
- Distinctive: line 102
When testing token-scoped endpoints, MUST test both granted and missing abilities:
- Distinctive: line 116
Prefer allowlisted fakes over blanket fakes so unrelated listeners stay live:
- Very specific: line 141
MUST: call Http::preventStrayRequests() in test setup when mocking an external API — any unmatched request will throw instead of silently …
- Very specific: line 157
MUST: place under tests/fixtures/.
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Testing (Pest)
Default test framework: Pest. Pest is required for new tests in projects using this ruleset; PHPUnit-style assertions still work under the hood.
Database
- SHOULD run the suite against an in-memory SQLite database (
DB_CONNECTION=sqlite,DB_DATABASE=:memory:inphpunit.xml/.env.testing) for fast, isolated tests. Prefer a real MySQL/Postgres test DB when the feature under test depends on engine-specific behaviour (full-text, certain JSON ops, advisory locks). - MUST use
LazilyRefreshDatabase,RefreshDatabase, orDatabaseTransactionsso each database-using test starts from a clean schema. PREFERLazilyRefreshDatabaseintests/Pest.phpfor Pest suites because tests that never touch the database avoid needless migration work.
When to write tests
- MUST write a test for every class that contains logic (Actions, Support, Jobs, Commands, Livewire components, Controllers, Policies).
- MUST write a test for every route exposed to users (auth/redirect/permission boundaries).
- SHOULD use coverage reports to find untested behaviour, not as a numeric gate agents grind toward.
What to test
- Code logic (Actions, Support classes, Jobs, Commands)
- HTTP layer (Controllers, Livewire components, route auth boundaries)
- Routing (named routes, redirects, middleware behaviour)
- Edge cases — happy path is necessary but not sufficient.
Test types
- Feature tests are the default. They exercise the full stack and catch the largest class of regressions for the smallest amount of code. Place under
tests/Feature/— seetests/Feature/CLAUDE.md. - Unit tests are used only for genuinely isolated logic (pure functions, complex calculations). Place under
tests/Unit/— seetests/Unit/CLAUDE.md. AVOID unit tests that mock the framework just to bypass it. - MUST mirror the source path under
app/in the test path, preserving class type, audience/version, and domain —App\Http\Controllers\Billing\InvoiceControllermaps totests/Feature/Http/Controllers/Billing/InvoiceControllerTest.php; itsApi\V1\Billingvariant maps totests/Feature/Http/Controllers/Api/V1/Billing/InvoiceControllerTest.php. Seeapp/CLAUDE.md. - Architecture tests enforce structural rules with Pest's
arch()— naming, layering, no debug leftovers. Place undertests/Architecture/— full matrix and rules intests/Architecture/CLAUDE.md.
How to write tests
- MUST use lowercase, descriptive names that read like sentences.
- MUST only assert things relevant to the class under test — keep assertions focused.
- SHOULD cover at least: happy path, validation failure, authorization failure, and one edge case per branch.
// PREFER — lowercase, descriptive
it('has a welcome page', function () {
$response = $this->get('/');
expect($response->status())->toBe(200);
});
// AVOID — uppercase, vague
test('PAYMENT', fn () => /* ... */);Create
php artisan make:test Actions/VerifyUserActionTestPest hooks
beforeEach()/afterEach()— per testbeforeAll()/afterAll()— once per file
Helpers and custom methods
- SHOULD extract repeated setup into a helper function in the test file.
- SHOULD promote a helper to
tests/Pest.phpwhen it is useful across multiple files.
function asAdmin(): \Tests\TestCase
{
$user = User::factory()->create(['admin' => true]);
return test()->actingAs($user);
}
it('can manage users', function () {
asAdmin()->get('/users')->assertOk();
});Datasets — parameterized tests
For boundary/validation tests that vary only by inputs, SHOULD use a Pest dataset rather than copy-pasting near-identical tests:
it('rejects invalid emails', function (string $email) {
$validator = Validator::make(['email' => $email], ['email' => ['required', 'email:rfc']]);
expect($validator->fails())->toBeTrue();
})->with([
'empty' => '',
'no at sign' => 'not-an-email',
'spaces' => 'a b@c.com',
]);Import Illuminate\Support\Facades\Validator. This checks validation rules in a Feature test with a booted app; use HTTP assertions against the actual Form Request endpoint when testing the request boundary. Factories persist attributes and do not perform request validation.
Useful assertions
assertSoftDeleted('posts', ['id' => $post->id])— soft delete happened.assertModelExists/assertModelMissing— model-aware existence; clearer than bareassertDatabaseHas.assertJsonStructure([...])— pin response shape (including any project pagination envelope).
Auth — Sanctum + abilities
When testing token-scoped endpoints, MUST test both granted and missing abilities:
use Laravel\Sanctum\Sanctum;
Sanctum::actingAs($user, ['posts:write']);
$this->postJson('/api/posts', $payload)->assertCreated();
Sanctum::actingAs($user, []); // no abilities
$this->postJson('/api/posts', $payload)->assertForbidden();Selective fakes
Prefer allowlisted fakes over blanket fakes so unrelated listeners stay live:
Event::fake([OrderShipped::class]);
Notification::fake();
Storage::fake('s3');
Notification::assertSentTo($user, OrderShipped::class);
Storage::disk('s3')->assertExists("invoices/{$order->id}.pdf");Mocking
Mock external boundaries (HTTP, mail, queue, filesystem, third-party SDKs). AVOID mocking your own application classes — refactor instead.
Http::fake([
'google.com/*' => Http::response('foo@gmail.com', 200),
]);
$response = resolve(FetchGoogleUserEmailAction::class)->handle();
expect($response)->toBe('foo@gmail.com');MUST call Http::preventStrayRequests() in test setup when mocking an external API — any unmatched request will throw instead of silently hitting the real endpoint.
use function Pest\Laravel\mock;
mock(Client::class)
->shouldReceive('acceptOrder')
->withArgs(fn ($givenOrder) => $givenOrder->is($order))
->once()
->andReturn(true);Fixtures
Static files used as test input (XML payloads, JSON snapshots, sample uploads).
- MUST place under
tests/fixtures/. - SHOULD reference via
base_path('tests/fixtures/...')rather than relative paths.
$payment = base_path('tests/fixtures/payment.xml');
app(ProcessPaymentAction::class)->handle($payment, /* ... */);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
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.

