wolfpack-it / laravel-gwt-plugin
Laravel 测试的行为驱动测试 (GWT) 插件
Requires
- php: ^8.2.0
- laravel/framework: ^10.0
Requires (Dev)
- laravel/pint: ^1.13.1
- phpunit/phpunit: ^10.3.5
This package is auto-updated.
Last update: 2024-09-19 12:14:27 UTC
README
Laravel 测试的行为驱动测试 (GWT) 插件
安装
composer require wolfpack-it/laravel-gwt-plugin --dev
用法 / 示例
基本上,您通过此包提供的 TestCase 扩展您的测试,这允许您使用包的方法。下面是一个示例:
use App\Mails\ConfirmationMail; use App\Models\Language; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Mail\Mailable; use Illuminate\Support\Facades\Mail; use Illuminate\Testing\TestResponse; use WolfpackIT\LaravelGWTPlugin\TestCase; // Extend the Laravel GWT Plugin TestCase class StorePostTest extends TestCase { public function test_user_can_store_post(): void { $this // Act as a random User ->as(User::factory()->create()) // Fake sending the email ->fake(Mail::class) // Given is the post data, stored in a param called `post` ->given(fn (): array => [ 'title' => 'My first post', 'content' => 'Lorem ipsum dolor amet sum it', ], 'post') // Given is the current language ->given(fn(): Language => Language::current()) // The $post and $language are automatically injected based on the results of the `given` methods ->when(fn (array $post, Language $language): TestResponse => $this->postJson(route('api.posts.store'), $post) ) // Instead of using a closure you can call a protected method as a callable with `(...)` ->then($this->responseContainsPostId(...)) // Add as much `then` methods as needed ->then($this->confirmationMailIsSent(...)); } // The $response is automatically injected based on the TestResponse result of the `when` method protected function responseContainsPostId(TestResponse $response): void { $response ->assertCreated() // Status code 201 ->assertJson(fn (AssertableJson $json) => $json->has('id') // .. add other assertions ); } protected function confirmationMailIsSent(Authenticatable $user): void { Mail::assertSent(ConfirmationMail::class, function (Mailable $mail) use ($user) { return $mail->hasTo($user->email); // .. add other assertions }); } }
方法
as
方法
在测试过程中,可以使用 Sanctum::actingAs 或 Passport::actingAs 方法来验证用户身份。要在 Given-When-Then 方法中使用此验证过的用户,可以使用 as(Authenticatable $user, ?string $injectAs = null)
方法。这个 Authenticatable 然后被添加到其他方法的自动注入中。
$this->as(User::factory()->create()); $this->as(Admin::factory()->create(), 'admin');
为了更改默认的认证提供者(Sanctum),您可以使用以下静态方法来更改这些设置。只要在 config/auth.php
中定义了 guardian,并且认证提供者类实现了 actingAs
方法,就可以使用这些设置。
TestScenario::setAuthGuard('admin'); TestScenario::setAuthProvider(TestScenario::PASSPORT_AUTH_PROVIDER);
fake
方法
在测试过程中,您可能需要模拟一些服务,如邮件或事件。为了在插件语法中使用这些服务,您可以使用 fake(string $facade)
方法。任何包含 fake
方法的 facade 都可以使用。
$this->fake(Mail::class);
throws
方法
throws 方法表示您期望 when 动作抛出一个异常。此方法需要异常类名(字符串)和可选的异常消息。
given
方法
Given 描述了测试开始前的先决条件和初始状态,并允许进行任何可能的预测试设置。此方法期望一个 Closure/Callable 作为第一个参数,并使用结果(返回类型)来填充场景参数中的先决条件。可调用函数可以使用来自先前 given 方法的条件通过自动注入进行链式调用。第二个参数用于帮助定义通过自动注入可以检索该参数的键。当使用两种相同类型的参数时,此 as
参数非常有用。示例
$this ->given(fn(): User => User::factory()->student()->create(), 'student') ->given(fn(): User => User::factory()->teacher()->create(), 'teacher');
when
方法
When 描述了测试期间采取的操作。此方法期望与 given 方法相同的参数。其中第二个参数的默认值为 'response'
。可调用函数可以像需要的那样使用来自先前 given 方法的所有条件。
$this->when(fn(array $postData): TestResponse => $this->postJson('/posts', $postData));
then
方法
Then 描述了在 when 子句中采取操作的结果。此方法仅期望一个 Closure/Callable,可以执行必要的断言。通过自动注入,此方法可以使用先前 when 方法的结果和先前 given 方法的条件。当尝试比较给定数据与给定响应时,这可能很有用。
$this->then(fn(TestResponse $response) => $response->assertOk());
重要提示
确保您的闭包/可调用函数实现返回类型,以便自动注入能够识别要将哪些条件或响应映射到哪个参数。
此包由 Pascal van Gemert @ WolfpackIT 创建。它现在是开源的,并受 MIT 许可 的许可。