jamesst20/kahlan-laravel

此包最新版本(1.0.0)没有可用的许可信息。

Kahlan 对 Laravel 的支持

1.0.0 2019-03-13 23:33 UTC

This package is auto-updated.

Last update: 2024-09-14 19:20:25 UTC


README

Kahlan 是一个功能齐全的单元测试和BDD测试框架,类似于 RSpec/JSpec,它使用 describe-it 语法,将 PHP 测试推进了一步。

为什么需要这个包

虽然你可以使用纯净的 Kahlan 库,但你无法访问 Laravel TestCase 辅助方法。

兼容性

目前只支持 Laravel 5.8,因为此库依赖于 Laravel 一起提供的 phpdotenv v3.0。它也只支持最新的 Kahlan 版本(目前为 v4.6),因为它依赖于在 v4.6 上部署的新 afterEach 行为。

安装

composer require --dev jamesst20/kahlan-laravel:^1.0

使用方法

获取应用程序实例:$this->app$this->laravel->appapp() 都是等效的方式,并将返回相同的实例。

在普通的 Laravel TestCase 中,我们可以通过执行 $this->insertHelperMethod 访问辅助方法,例如 $this->assertDatabaseHas('users', ['email' => 'test@email.com']);

在 kahlan 环境中,只需在方法前加上 $this->laravel->insertHelperMethod 即可。

请注意,$this->laravel$this->appbeforeAllafterAll 回调中不可用,但在 beforeEach 中立即可用。

示例

普通 Laravel TestCase(PHPUnit)

<?php

namespace Tests\Feature;

use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }

    public function testUserCreation() {
        factory(User::class)->create(['email' => 'test@email.com']);
        $this->laravel->assertDatabaseHas('users', ['email' => 'test@email.com']);
    }
}

Kahlan

use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

describe("Homepage", function() {
    it("loads successfully", function() {
        $response = $this->laravel->get('/');
        expect($response->getStatusCode())->toEqual(200);
    });
});

describe("User", function() {
    beforeAll(function() {
        $this->enabledTraits = array(RefreshDatabase::class);
    });

    it("should save user to database", function() {
        factory(User::class)->create(['email' => 'test@email.com']);
        // There are cleaner way than using assertDatabaseHas though.
        expect(function() {
            $this->laravel->assertDatabaseHas('users', ['email' => 'test@email.com']);
        })->not->toThrow();
    });
});

Laravel 特性

在 Kahlan-Laravel 中启用 Laravel 特性的推荐方法是通过在 beforeAll() 闭包中传递一个特性数组。

describe("Something that needs database", function() {
    beforeAll(function() {
        $this->enabledTraits = array(RefreshDatabase::class);
    });
});

当前支持的特性 RefreshDatabaseWithoutMiddlewareWithoutEventsWithFaker

环境

环境变量从 .env.testing 读取,也可以由 .env.kahlan 覆盖。

在初始化过程中,在 Kahlan 加载之前,如果存在 .env.testing,则仅加载此文件。稍后加载 .env.kahlan,并在冲突的情况下覆盖 .env.testing 变量。

请注意,如果不在测试环境中执行,Kahlan 会引发错误。

执行

目前有 3 种执行 Kahlan 的方法,但我目前只支持其中 2 种。

使用 artisan

APP_ENV=testing php artisan kahlan:run

使用二进制文件

vendor/bin/kahlan-laravel

两者的区别在于,二进制文件在引导之前会自动从 .env.testing.env.kahlan 中读取 APP_ENV

由于 Laravel 的当前实现,没有干净的方法来避免在 artisan 命令之前指定 APP_ENV=testing。如果未定义 APP_ENV,Laravel 会自动加载 .env,否则加载 .env.{environment}。虽然可以覆盖 .env 并使用测试配置,但在测试环境文件中可能没有生产变量可以覆盖。

缺少的功能

您告诉我!

致谢

感谢所有 Laravel、Symfony 和 Kahlan 的贡献者。

此库受到了 Sofa/laravel-kohlan 和 elephantly/kahlan-bundle 的启发。