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.

rulesfake-storage.md

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

Faking Storage with Storage::fake()

Impact: HIGH (Prevents real file writes during tests and enables assertion on file upload behaviour)

Call Storage::fake('disk') before any code that reads or writes files. This swaps the real filesystem with an in-memory fake. Use UploadedFile::fake() to generate fake files for upload tests. Assert presence or absence of files with Storage::disk()->assertExists().

Bad Example

<?php

// Writes real files to disk — pollutes storage, fails in CI environments
test('user can upload avatar', function () {
    $user = User::factory()->create();
    $file = new \Illuminate\Http\UploadedFile(
        storage_path('test-image.jpg'), // relies on a file existing locally
        'avatar.jpg',
        'image/jpeg',
        null,
        true
    );

    $this->actingAs($user)
        ->postJson('/api/avatar', ['file' => $file])
        ->assertOk();
    // Real file written to storage/app/public — not cleaned up after test
});

Good Example

<?php

use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

// Basic upload test
test('user can upload an avatar', function () {
    Storage::fake('avatars');

    $user = User::factory()->create();
    $file = UploadedFile::fake()->image('avatar.jpg', 200, 200);

    $this->actingAs($user)
        ->postJson('/api/avatar', ['avatar' => $file])
        ->assertOk();

    Storage::disk('avatars')->assertExists("users/{$user->id}/avatar.jpg");
});

// Assert file is not present when upload is rejected
test('oversized image is rejected and not stored', function () {
    Storage::fake('avatars');

    $user = User::factory()->create();
    $file = UploadedFile::fake()->image('big.jpg')->size(5001); // 5001 KB > limit

    $this->actingAs($user)
        ->postJson('/api/avatar', ['avatar' => $file])
        ->assertUnprocessable()
        ->assertJsonValidationErrors(['avatar']);

    Storage::disk('avatars')->assertMissing("users/{$user->id}/big.jpg");
});

// Assert file is removed on delete
test('deleting avatar removes file from storage', function () {
    Storage::fake('avatars');

    $user = User::factory()->create([
        'avatar_path' => "users/{$user->id}/avatar.jpg",
    ]);
    Storage::disk('avatars')->put("users/{$user->id}/avatar.jpg", 'fake-content');

    $this->actingAs($user)
        ->deleteJson('/api/avatar')
        ->assertNoContent();

    Storage::disk('avatars')->assertMissing("users/{$user->id}/avatar.jpg");
});

// Fake multiple disks
test('document is moved from temp to permanent disk', function () {
    Storage::fake('temp');
    Storage::fake('documents');

    $user = User::factory()->create();
    $file = UploadedFile::fake()->create('report.pdf', 512, 'application/pdf');

    $this->actingAs($user)
        ->postJson('/api/documents', ['file' => $file])
        ->assertStatus(201);

    Storage::disk('documents')->assertExists("reports/{$user->id}/report.pdf");
    Storage::disk('temp')->assertMissing("reports/{$user->id}/report.pdf");
});

UploadedFile::fake() Helpers

UploadedFile::fake()->image('photo.jpg')           // JPEG image
UploadedFile::fake()->image('photo.png', 400, 300) // image with dimensions
UploadedFile::fake()->create('doc.pdf', 1024)      // file with size in KB
UploadedFile::fake()->create('doc.pdf', 1024, 'application/pdf') // with MIME

Key Assertions

Method Description
Storage::disk('s3')->assertExists($path) File exists at path
Storage::disk('s3')->assertMissing($path) File does not exist at path
Storage::disk('s3')->assertDirectoryEmpty($dir) Directory has no files

Why It Matters

  • No real I/O: No files written to disk or S3 — tests are fast, clean, and portable
  • CI-safe: No dependency on local filesystem paths or cloud credentials
  • Assertions: Explicitly verify that files are created at the expected paths

Reference: Laravel Mocking — Storage Fake

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