bradietilley/pest-stories

一个用于在Pest PHP(用于Laravel)中创建复杂测试场景排列组合的工具

0.4.0 2023-05-07 04:37 UTC

This package is auto-updated.

Last update: 2024-09-07 07:30:31 UTC


README

编写大型测试套件的一种简洁方法。

Static Analysis Tests

简介

用户故事是对软件应用特性或功能的简短、简单描述,通常从最终用户的角度来写。

Pest Stories 是一个PHP包,它扩展了PestPHP测试框架,允许开发者以清晰且可重用的方式编写用户故事,使维护和测试软件应用更加容易。理念是测试应以人类可读的方式编写,反映用户故事。

安装

composer require bradietilley/pest-stories --dev

要将Stories添加到您的测试套件中,您必须通过Pest的uses()辅助函数添加以下特性

uses(BradieTilley\Stories\Concerns\Stories::class);

有关如何使用uses()辅助函数的信息,请参阅Pest的文档。

文档

阅读文档

示例

tests/Pest.php:

action('as_admin', function () {
    actingAs(User::factory()->admin()->create());
});

action('as_customer', function () {
    actingAs(User::factory()->customer()->create());
});

action('create_product', function (array $product) {
    return test()->postJson(route('products.store'), array_replace([
        'title' => 'Default',
        'sku' => 'default',
        'price' => 99.99,
    ], $product))
}, 'response');

action('response:ok', function (TestResponse $response) {
    $response->assertOk();
});

action('response:invalid', function (TestResponse $response) {
    $response->assertUnprocessable();
});

tests/Feature/Api/Products/CreateTest.php:

test('can create a product via the api')
    ->action('as_admin')
    ->assertDatabaseMissing('products', [
        'title' => 'Default',
    ])
    ->action('create_product')
    ->assertDatabaseHas('products', [
        'title' => 'Default',
    ]);

test('cannot create a product via the api as a customer')
    ->action('as_customer')
    ->assertDatabaseMissing('products', [
        'title' => 'Default',
    ])
    ->action('create_product')
    ->assertDatabaseMissing('products', [
        'title' => 'Default',
    ]);

test('can create a product via the api with a custom title')
    ->action('as_admin')
    ->assertDatabaseMissing('products', [ 'title' => 'Custom Product' ])
    ->action('create_product', [
        'product' => [
            'title' => 'Custom Product',
        ],
    ])
    ->assertDatabaseHas('products', [ 'title' => 'Custom Product' ]);

test('can create a product via the api with a custom price')
    ->action('as_admin')
    ->assertDatabaseMissing('products', [ 'price' => 12345.67 ])
    ->action('create_product', [
        'product' => [
            'price' => 12345.67,
        ],
    ])
    ->assertDatabaseHas('products', [ 'price' => 12345.67 ]);

当然,这只是介绍如何自定义Pest Stories以使其更适合您的方法。

作者