midnightsuyama/laravel-kahlan4

Laravel 的 Kahlan 规范套件

2.1.0 2022-03-10 16:01 UTC

This package is not auto-updated.

Last update: 2024-09-19 03:07:26 UTC


README

Latest Stable Version Build Status

Laravel 的 Kahlan 规范套件

功能

  • 支持 Laravel 测试方法
  • 支持 Laravel 测试特质
  • 支持 Laravel 测试断言

安装

$ composer require --dev midnightsuyama/laravel-kahlan4

使用

将此行添加到您的 kahlan-config.php 文件中(如果需要,请创建它)

<?php

LaravelKahlan4\Config::bootstrap($this);

$ ./vendor/bin/kahlan

示例

<?php
// spec/User.spec.php

describe('User', function() {
    it('has a name "example"', function() {
        $user = factory(App\User::class)->make(['name' => 'xample']);
        expect($user['name'])->toEqual('example');
    });
});
$ ./vendor/bin/kahlan
            _     _
  /\ /\__ _| |__ | | __ _ _ __
 / //_/ _` | '_ \| |/ _` | '_ \
/ __ \ (_| | | | | | (_| | | | |
\/  \/\__,_|_| |_|_|\__,_|_| |_|

The PHP Test Framework for Freedom, Truth and Justice.

src directory  :
spec directory : /spec

F                                                                   1 / 1 (100%)


User
  ✖ it has a name "example"
    expect->toEqual() failed in `./spec/User.spec.php` line 7

    It expect actual to be equal to expected (==).

    actual:
      (string) "xample"
    expected:
      (string) "example"


Expectations   : 1 Executed
Specifications : 0 Pending, 0 Excluded, 0 Skipped

Passed 0 of 1 FAIL (FAILURE: 1) in 0.817 seconds (using 20MB)

方法

$response = $this->laravel->get('/');

$this->laravelIlluminate\Foundation\Testing\TestCase 子类的实例。它可以调用 Illuminate\Foundation\Testing\Concerns 的测试方法。

特质

use Illuminate\Foundation\Testing\RefreshDatabase;

describe('User table', function() {
    beforeAll(function() {
        $this->laravel->useTrait(RefreshDatabase::class);
    });

    it('has no records', function() {
        $count = App\User::count();
        expect($count)->toEqual(0);
    });
});

如果您想使用测试特质,请在 beforeAll 中调用 $this->laravel->useTrait()。这个特质在调用 beforeEach 之前被设置。

支持的特质

  • Illuminate\Foundation\Testing\RefreshDatabase
  • Illuminate\Foundation\Testing\DatabaseMigrations
  • Illuminate\Foundation\Testing\DatabaseTransactions
  • Illuminate\Foundation\Testing\WithoutMiddleware
  • Illuminate\Foundation\Testing\WithoutEvents
  • Illuminate\Foundation\Testing\WithFaker

匹配器

提供自定义匹配器。 toPass* 匹配器与 Laravel 的 assert* 断言配对。

响应

describe('Response', function() {
    it('has a 200 status code', function() {
        $response = $this->laravel->get('/');
        expect($response)->toPassStatus(200);
    });
});

身份验证

describe('User', function() {
    it('is authenticated', function() {
        $user = factory(App\User::class)->make();
        $this->laravel->actingAs($user);
        expect($this->laravel)->toPassAuthenticated();
    });
});

数据库

describe('User table', function() {
    it('has records', function() {
        $user = factory(App\User::class)->create();
        expect($this->laravel)->toPassDatabaseHas('users', ['email' => $user['email']]);
    });
});

通知

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Notification;

describe('User', function() {
    it('is notified', function() {
        $notification = Notification::fake();
        expect($notification)->toPassNothingSent();

        $user = factory(App\User::class)->create();
        $user->notify(new VerifyEmail);
        expect($notification)->toPassSentTo($user, VerifyEmail::class);
    });
});

测试

$ cd test-project
$ composer install
$ touch database/database.sqlite
$ php artisan migrate --env=testing
$ ./vendor/bin/kahlan
            _     _
  /\ /\__ _| |__ | | __ _ _ __
 / //_/ _` | '_ \| |/ _` | '_ \
/ __ \ (_| | | | | | (_| | | | |
\/  \/\__,_|_| |_|_|\__,_|_| |_|

The PHP Test Framework for Freedom, Truth and Justice.

src directory  :
spec directory : /test-project/spec

.............................................................     61 / 61 (100%)



Expectations   : 61 Executed
Specifications : 0 Pending, 0 Excluded, 0 Skipped

Passed 61 of 61 PASS in 7.760 seconds (using 37MB)

许可证

使用 MIT 许可证 许可。

致谢

sofa/laravel-kahlan 启发。