eventsauce/pest-utilities

EventSauce 的测试工具。

3.4.1 2023-06-23 10:15 UTC

This package is auto-updated.

Last update: 2024-08-25 10:44:21 UTC


README

composer require --dev eventsauce/pest-utilities

用法

首先,创建一个基本的测试用例,如常规PHPUnit设置中所述

接下来,在您的Pest测试中使用基本测试用例

use function EventSauce\EventSourcing\PestTooling\expectToFail;
use function EventSauce\EventSourcing\PestTooling\given;
use function EventSauce\EventSourcing\PestTooling\nothingShouldHaveHappened;
use function EventSauce\EventSourcing\PestTooling\when;

uses(YourBaseTestCase::class);

it('you can use the object-oriented interface', function () {
    $this->given(
        new ShoppingCartInitiated(),
    )->when(function (ShoppingCart $cart): void {
        $cart->addProduct(new ProductId('garlic sauce'), 250);
    })->then(
        new ProductAddedToCart(new ProductId('garlic sauce'), 250)
    );
});

it('or the function based interface', function () {
    given(new ShoppingCartInitiated());
    when(function (ShoppingCart $cart): void {
        $cart->checkout();
    });
    expectToFail(SorryCantCheckout::becauseThereAreNoProductsInCart());
    nothingShouldHaveHappened();
});

it('or mix it all', function () {
    given(new ShoppingCartInitiated())
        ->when(function (ShoppingCart $cart): void {
            $cart->checkout();
        });
    expectToFail(SorryCantCheckout::becauseThereAreNoProductsInCart())
        ->nothingShouldHaveHappened();
});

it('can be used in a compact manner')
    ->given(new ShoppingCartInitiated())
    ->when(fn (ShoppingCart $cart) => $cart->add(new ProductId('garlic sauce'), 250))
    ->then(new ProductAddedToCart(new ProductId('garlic sauce'), 250))
    ->assertScenario(); // needed for a Pest bug