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.

rulesdb-assert-soft-deletes.md

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

Asserting Soft Deletes

Impact: MEDIUM (Verifies soft-delete behaviour without accidentally treating records as hard-deleted)

Use assertSoftDeleted() to confirm a record has a non-null deleted_at timestamp while still existing in the database. Use assertNotSoftDeleted() to assert a model is active. Use the built-in trashed() factory state to generate pre-soft-deleted records for restore tests.

Bad Example

<?php

// assertModelMissing is wrong for soft deletes — the row still exists
test('post is soft deleted', function () {
    $user = User::factory()->create();
    $post = Post::factory()->for($user)->create();

    $this->actingAs($user)->deleteJson("/api/posts/{$post->id}");

    $this->assertModelMissing($post); // WRONG: the row is still there with deleted_at set
});

// Manually checking deleted_at is verbose and bypasses built-in helpers
test('post has deleted_at', function () {
    $post = Post::factory()->create();
    $post->delete();

    $fresh = Post::withTrashed()->find($post->id);
    expect($fresh->deleted_at)->not->toBeNull(); // verbose
});

Good Example

<?php

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

uses(RefreshDatabase::class);

// assertSoftDeleted — confirms deleted_at is set, row still exists in DB
test('deleting a post soft-deletes it', function () {
    $user = User::factory()->create();
    $post = Post::factory()->for($user)->create();

    $this->actingAs($user)
        ->deleteJson("/api/posts/{$post->id}")
        ->assertNoContent();

    $this->assertSoftDeleted($post);
});

// assertNotSoftDeleted — verify active record is not accidentally trashed
test('restoring a post removes deleted_at', function () {
    $user = User::factory()->create();
    $post = Post::factory()->for($user)->trashed()->create();

    $this->actingAs($user)
        ->postJson("/api/posts/{$post->id}/restore")
        ->assertOk();

    $this->assertNotSoftDeleted($post);
});

// trashed() factory state — create pre-soft-deleted records
test('trashed posts do not appear in feed', function () {
    $user = User::factory()->create();

    Post::factory()->for($user)->count(2)->create();           // active
    Post::factory()->for($user)->count(1)->trashed()->create(); // soft-deleted

    $this->actingAs($user)
        ->getJson('/api/feed')
        ->assertJsonCount(2, 'data'); // trashed post excluded
});

// assertSoftDeleted with table name and attributes
test('membership is soft deleted on cancellation', function () {
    $membership = Membership::factory()->create();

    $this->postJson("/api/memberships/{$membership->id}/cancel")
        ->assertNoContent();

    $this->assertSoftDeleted('memberships', ['id' => $membership->id]);
});

Why It Matters

  • Correctness: assertModelMissing passes only if the row is gone — wrong for soft deletes
  • Built-in: trashed() factory state is available on all models with SoftDeletes automatically
  • Completeness: Tests both the delete (soft) and restore paths

Reference: Laravel Database Testing — assertSoftDeleted

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