定制 jcergolj/additional-test-assertions-for-laravel 二次开发

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

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

jcergolj/additional-test-assertions-for-laravel

最新稳定版本:v2.3

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-21