LaravelPackages.net
Acme Inc.
Toggle sidebar
dex/pest-plugin-laravel-tester

Pest plugin to test Laravel components

186
1
4.1.0
About dex/pest-plugin-laravel-tester

dex/pest-plugin-laravel-tester is a Laravel package for pest plugin to test laravel components. It currently has 1 GitHub stars and 186 downloads on Packagist (latest version 4.1.0). Install it with composer require dex/pest-plugin-laravel-tester. Discover more Laravel packages by dex or browse all Laravel packages to compare alternatives.

Last updated

Pest Plugin Laravel Tester

Test Laravel Eloquent models, relationships, API endpoints and validation rules with near-zero boilerplate using Pest's higher-order syntax.

describe('Post', function () {
    beforeEach()->eloquent(Post::class)->endpoint('/api/post');

    test()->toBeCreate();
    test()->toBeUpdate();
    test()->toBeDelete();

    test()->toHaveBelongsToRelation(User::class, 'user');
    test()->toHaveHasManyRelation(Comment::class, 'comments');

    test()->toHaveIndexEndpoint();
    test()->toHaveStoreEndpoint();
    test()->toHaveShowEndpoint();
    test()->toHaveUpdateEndpoint();
    test()->toHaveDestroyEndpoint();

    test()->toValidateRequired('title');
    test()->toValidateMin('title', 10);
    test()->toValidateMax('title', 200);
});

Installation

composer require --dev dex/pest-plugin-laravel-tester

Add the Tester trait to your tests/Pest.php:

use Dex\Pest\Plugin\Laravel\Tester\Tester;

uses(Tester::class)->in(__DIR__);

Eloquent CRUD

Assert that a model can be created, updated, and deleted using its factory. Soft deletes are detected automatically.

describe('Post', function () {
    beforeEach()->eloquent(Post::class);

    test()->toBeCreate();
    test()->toBeUpdate();
    test()->toBeDelete(); // uses assertSoftDeleted() when the model uses SoftDeletes
});

Customize the factory state for a specific test:

describe('Post', function () {
    beforeEach()->eloquent(Post::class);
    beforeEach()->factory(fn ($factory) => $factory->state(['status' => 'published']));

    test()->toBeCreate()->assertDatabaseHas(Post::class, ['status' => 'published']);
});

Relationships

describe('Post relationships', function () {
    beforeEach()->eloquent(Post::class);

    test()->toHaveBelongsToRelation(User::class, 'user');
    test()->toHaveHasManyRelation(Comment::class, 'comments');
    test()->toHaveHasOneRelation(Comment::class, 'latestComment');
});

describe('User relationships', function () {
    beforeEach()->eloquent(User::class);

    test()->toHaveHasManyThroughRelation(Comment::class, Post::class, 'comments');
});

REST API Endpoints

Assert the full CRUD cycle of an API resource, including HTTP status codes and database state after each operation.

describe('Post API', function () {
    beforeEach()->eloquent(Post::class)->endpoint('/api/post');

    test()->toHaveIndexEndpoint();   // GET    /api/post       → 200
    test()->toHaveStoreEndpoint();   // POST   /api/post       → 201 + assertDatabaseCount(1)
    test()->toHaveShowEndpoint();    // GET    /api/post/{id}  → 200
    test()->toHaveUpdateEndpoint();  // PUT    /api/post/{id}  → 200 + assertDatabaseHas
    test()->toHaveDestroyEndpoint(); // DELETE /api/post/{id}  → 200 + assertDatabaseMissing
});

When the response wraps data under a key (e.g., Laravel's API resources or pagination):

test()->wrap('data')->toHaveIndexEndpoint();
test()->wrap('post')->toHaveShowEndpoint();

Transform the payload before sending or the response before asserting:

beforeEach()->transformPayload(fn ($data) => collect($data)->except('computed')->all());
beforeEach()->transformResult(fn ($data) => collect($data)->except('computed')->all());

Validation

Each method tests both the store (POST) and update (PUT) endpoints, asserting a 422 with the appropriate validation error.

describe('Post validation', function () {
    beforeEach()->eloquent(Post::class)->endpoint('/api/post');

    test()->toValidateRequired('title');
    test()->toValidateMin('title', 10);
    test()->toValidateMax('title', 200);
    test()->toValidateSize('code', 8);
});

License

Pest Plugin Laravel Tester is open-sourced software licensed under the MIT license.

Comments