jcergolj/additional-test-assertions-for-laravel
Composer 安装命令:
composer require jcergolj/additional-test-assertions-for-laravel
包简介
Additional Test Assertions for Laravel
README 文档
README
If you like what you are seeing condider checking out those packages too:
Installation
Required PHP >=8.0
composer require --dev jcergolj/additional-test-assertions-for-laravel
Assertions
assertMiddlewareIsApplied
class UsersTest extends TestCase { /** @test */ public function assert_auth_middleware_is_applied() { $response = $this->delete(route('users.index')); $response->assertMiddlewareIsApplied('auth'); } }
assertViewHasComponent
# users/index.blade.php <?php <x-layouts.app> <x-users-table /> </x-layouts.app> # test class UsersTest extends TestCase { /** @test */ public function assert_view_has_users_table_component() { $response = $this->get(route('users.index')); $response->assertViewHasComponent('components.users-table'); } }
assertPushedAll
Assert that all jobs were dispatched. Inspired by Http::assertSentInOrder(), however here order of dispatched job is not important.
# routes/web.php <?php Route::get('dispatch-jobs', function () { FirstTestJob::dispatch('Joe'); FirstTestJob::dispatch('Will'); SecondTestJob::dispatch('Jane'); SecondTestJob::dispatch('Bob'); SecondTestJob::dispatch('Bob'); }); # FirstTestJob.php and SecondTestJob.php are the same <?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class FirstTestJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public function __construct(public string $name) { } public function handle(): void { } } # test use Illuminate\Support\Facades\Queue; use Jcergolj\AdditionalTestAssertionsForLaravel\Facades\CustomQueueFake; class ExampleTest extends TestCase { /** @test */ function assert_all_job_has_been_dispatched() { Queue::fake(); $this->get('dispatch-jobs'); CustomQueueFake::assertPushedAll([ function (FirstTestJob $job) { $this->assertSame('Joe', $job->name); return true; }, function (FirstTestJob $job) { $this->assertSame('Will', $job->name); return true; }, function (SecondTestJob $job) { $this->assertSame('Bob', $job->name); return true; }, function (SecondTestJob $job) { $this->assertSame('Bob', $job->name); return true; }, // test passes even if jane's job is referenced at the end function (SecondTestJob $job) { $this->assertSame('Jane', $job->name); return true; }, ]); } }
Additional model test assertions
assertHasObserver
<?php namespace Tests\Unit\Models; use App\Models\User; use Tests\TestCase; class UserTest extends TestCase { // add this trait use \Jcergolj\AdditionalTestAssertionsForLaravel\Traits\HasModelAssertions; /** @test */ public function assert_user_has_saving_observer_applied() { $this->assertModelHasObserver(User::class, 'saving'); $this->assertModelHasObserver(app(User::class), 'saving'); } }
assertModelHasGlobalScope
<?php namespace Tests\Unit\Models; use App\Models\Scopes\UserCustomScope; use App\Models\User; use Tests\TestCase; class UserTest extends TestCase { // add this trait use \Jcergolj\AdditionalTestAssertionsForLaravel\Traits\HasModelAssertions; /** @test */ public function assert_user_has_user_custom_global_scope_applied() { $this->assertModelHasGlobalScopeApplied(User::class, UserCustomScope::class); $this->assertModelHasGlobalScopeApplied(app(User::class), UserCustomScope::class); } }
jcergolj/additional-test-assertions-for-laravel 适用场景与选型建议
jcergolj/additional-test-assertions-for-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.4k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jcergolj/additional-test-assertions-for-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jcergolj/additional-test-assertions-for-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-21