定制 dex/pest-plugin-laravel-tester 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dex/pest-plugin-laravel-tester

Composer 安装命令:

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

包简介

Pest plugin to test Laravel components

README 文档

README

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');
});

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.

dex/pest-plugin-laravel-tester 适用场景与选型建议

dex/pest-plugin-laravel-tester 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 164 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 08 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「php」 「testing」 「plugin」 「test」 「unit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dex/pest-plugin-laravel-tester 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dex/pest-plugin-laravel-tester 我们能提供哪些服务?
定制开发 / 二次开发

基于 dex/pest-plugin-laravel-tester 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 164
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 14
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-23