All skills
asyrafhussin avatar

/laravel-testing

@a5fe20e

Laravel 13 testing with Pest PHP 4 or PHPUnit 12. Use when writing feature tests, unit tests, or any test code in a Laravel application. Triggers on tasks involving HTTP tests, model factories, database assertions, mocking facades, authentication testing, or test organisation patterns.

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/laravel-testing

This session only. Nothing lands on disk.

rulesfactory-define.md

≈879 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Define Factories with Typed Fake Data

Impact: CRITICAL (Provides clean, realistic test data without manual setup)

Define model factories using Faker to generate realistic typed attributes. Use PHP 8.3 typed properties in the factory class. Every model that appears in tests should have a factory — raw DB::table()->insert() calls are fragile, bypass model events and casts, and duplicate schema knowledge across tests.

Bad Example

<?php

// Raw DB insert — bypasses model events, casts, and observers
test('user profile is created', function () {
    DB::table('users')->insert([
        'name'       => 'Test User',
        'email'      => 'test@test.com',
        'password'   => 'password',  // plaintext — not hashed
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $user = User::first();
    expect($user->profile)->not->toBeNull(); // profile observer never fired
});

// Factory without types and with hardcoded values
class UserFactory extends Factory
{
    public function definition()  // missing return type
    {
        return [
            'name'  => 'John',      // hardcoded — collisions in tests
            'email' => 'john@a.com',// always the same — unique constraint fails
        ];
    }
}

Good Example

<?php

namespace Database\Factories;

use App\Enums\UserRole;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
 * @extends Factory<User>
 */
class UserFactory extends Factory
{
    public function definition(): array
    {
        return [
            'name'              => fake()->name(),
            'email'             => fake()->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password'          => Hash::make('password'), // consistent test password
            'remember_token'    => Str::random(10),
            'role'              => UserRole::Member,
        ];
    }
}

Using the factory in tests:

<?php

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

// Create a persisted record
test('user has a profile', function () {
    $user = User::factory()->create();
    expect($user->profile)->not->toBeNull();
});

// Make without persisting (unit tests, no DB needed)
test('user full name is formatted', function () {
    $user = User::factory()->make(['name' => 'Alice Smith']);
    expect($user->full_name)->toBe('Alice Smith');
});

// Create multiple records
test('index returns all users', function () {
    User::factory()->count(5)->create();

    $this->getJson('/api/users')
        ->assertOk()
        ->assertJsonCount(5, 'data');
});

Why It Matters

  • Uniqueness: fake()->unique()->safeEmail() prevents constraint violations across test runs
  • Model events fire: create() goes through Eloquent — observers, casts, and booted traits work
  • Single source of truth: Factory definition lives once — changes propagate everywhere
  • Realistic data: Faker generates varied, realistic values that expose edge cases raw data misses

Reference: Laravel Eloquent Factories — Defining Factories

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill is a comprehensive testing guide for Laravel 13, covering both Pest PHP and PHPUnit frameworks. It provides clear rules and code examples for HTTP testing, model factories, database assertions, and mocking services. No security risks or malicious patterns were identified.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    25 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at a5fe20e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
Other metadata
metadata
{
  "author": "Laravel Community",
  "version": "1.1.0",
  "laravelVersion": "13.x",
  "phpVersion": "8.3+",
  "pestVersion": "4.x",
  "phpunitVersion": "12.x"
}

README badge

README badge for asyrafhussin/agent-skills/laravel-testing