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-missing.md

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

assertDatabaseMissing and assertModelMissing

Impact: HIGH (Confirms that delete operations actually remove data from the database)

Use assertDatabaseMissing() to verify that no row matching given constraints exists in a table. Use assertModelMissing() when you have the model instance. Always pair a delete operation with a missing assertion — a 200/204 response alone does not confirm the record was removed.

Bad Example

<?php

// Only checks HTTP status — not whether the DB row was removed
test('post is deleted', function () {
    $user = User::factory()->create();
    $post = Post::factory()->for($user)->create();

    $this->actingAs($user)
        ->deleteJson("/api/posts/{$post->id}")
        ->assertNoContent(); // passes even if delete logic has a bug and does nothing
});

// Queries the DB wrong way — assumes first() is the deleted model
test('user is removed', function () {
    $user = User::factory()->create();
    $this->delete("/admin/users/{$user->id}");

    expect(User::first())->toBeNull(); // fails if other users exist in DB
});

Good Example

<?php

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

uses(RefreshDatabase::class);

// assertModelMissing — clearest when you have the instance
test('deleting a post removes it from the database', function () {
    $user = User::factory()->create();
    $post = Post::factory()->for($user)->create();

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

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

// assertDatabaseMissing — when checking by attribute value
test('removing a tag detaches it from posts', function () {
    $tag  = Tag::factory()->create(['name' => 'laravel']);
    $post = Post::factory()->create();
    $post->tags()->attach($tag);

    $this->deleteJson("/api/tags/{$tag->id}")
        ->assertNoContent();

    $this->assertDatabaseMissing('tags', ['name' => 'laravel']);
    $this->assertDatabaseMissing('post_tag', ['tag_id' => $tag->id]);
});

// Assert both the model and pivot records are gone
test('deleting user removes their posts', function () {
    $user  = User::factory()->create();
    $posts = Post::factory()->for($user)->count(3)->create();

    $this->actingAs(User::factory()->admin()->create())
        ->deleteJson("/api/admin/users/{$user->id}")
        ->assertNoContent();

    $this->assertModelMissing($user);

    foreach ($posts as $post) {
        $this->assertModelMissing($post);
    }
});

Why It Matters

  • Behaviour verified: A 204 No Content alone says nothing about what changed in the DB
  • Cascade check: assertDatabaseMissing verifies pivot/related records are also cleaned up
  • Explicit intent: assertModelMissing clearly communicates what the test is verifying

Reference: Laravel Database Testing — assertDatabaseMissing

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