Skip to content

Testing helpers ​

fusion_stack::testing adds Fusion Stack's request options and assertions to the framework's TestRequest and TestResponse, as InteractsWithApi and ApiTestResponse do in PHP.

rust
use fusion_stack::errors::ApiErrorCode;
use fusion_stack::testing::{ApiRequest, ApiResponse};
use laravel::testing::prelude::*;

#[laravel::test]
async fn old_clients_see_name(app: Application) {
    app.get("/v1/customers/1")
        .api_version("2026-06-01")
        .await
        .assert_json_api_resource(Some("customers"), Some("1"))
        .assert_deprecated(Some("2027-06-01"))
        .assert_shape(json!({ "data": { "type": "string", "id": "string",
            "attributes": { "name": "string", "email": "string", "created_at": "string" } } }));
}

ApiRequest ​

PHP's PendingApiRequest, on TestRequest:

MethodHeader
api_version(version)Api-Version
api_key(key)Authorization: Bearer … (the framework's token() works for other tokens)
idempotency_key(key)Idempotency-Key

ApiResponse ​

PHP's ApiTestResponse, on TestResponse. Each returns the response, so they chain with the framework's own assertions.

MethodAsserts
assert_json_api_resource(kind, id)A successful JSON:API document whose data is one resource.
assert_json_api_collection(kind, count)A successful document whose data is a list of resources.
assert_api_error(code)An error document with the code: a &str, or an ErrorCode, which also checks its status.
assert_idempotent_replay() / assert_not_idempotent_replay()Idempotent-Replayed: true, or its absence.
assert_deprecated(sunset) / assert_not_deprecated()The Deprecation header, and the Sunset date when given.
assert_shape(json)The body's shape.

assert_shape() ​

Compares testing::shape(body): each value replaced by its JSON type (string, integer, number, boolean or null) and each list by the shape of its first item. Key order does not matter. PHP's assertVersionShape() is api_version(v) followed by assert_shape.

Version changes ​

PHP's TransformsVersionChanges, as functions:

rust
use fusion_stack::testing::assert_round_trip;

#[laravel::test]
fn the_rename_round_trips() {
    let old = assert_round_trip(
        &RenameCustomerNameToFullName,
        &json!({ "data": { "type": "customers", "id": "1", "attributes": { "full_name": "Ada" } } }),
    );
    assert_eq!(old["data"]["attributes"], json!({ "name": "Ada" }));
}
FunctionWhat it does
upgrade_request(&change, &payload)An older client's body, upgraded.
downgrade_response(&change, &document)A latest-shape document, downgraded.
assert_round_trip(&change, &document)Downgrading then upgrading gives the document back; returns the downgraded one.

Released under the MIT License.