Skip to content

Testing helpers ​

Fusion Stack's test helpers send requests the way clients do and assert on what clients rely on: the JSON:API shape, error codes, versions, deprecation and idempotency. They work with Pest and PHPUnit.

php
use FusionStack\Laravel\Auth\ApiKey;
use FusionStack\Laravel\Testing\InteractsWithApi;

uses(InteractsWithApi::class);

it('lists orders for an old client', function () {
    [, $key] = ApiKey::issue('Test', scopes: ['orders:read']);

    $this->api()->version('2026-06-01')->withKey($key)
        ->getJson('/v1/orders')
        ->assertJsonApiCollection('orders')
        ->assertDeprecated('2027-06-01');
});

InteractsWithApi ​

A trait for a Laravel test case: uses(InteractsWithApi::class) in Pest, or use InteractsWithApi; in a PHPUnit class.

api() ​

Starts a PendingApiRequest.

assertVersionShape() ​

php
$this->assertVersionShape('2026-06-01', '/v1/customers/1', [
    'data' => ['type' => 'string', 'id' => 'string', 'attributes' => ['name' => 'string']],
], api: $this->api()->withKey($key));

Requests a URI in a version, checks it succeeds, and asserts its shape. Snapshotting each supported version's shape catches a breaking change shipped without a version change. Pass $api for a request that needs a key or other headers.

PendingApiRequest ​

A request built up with the headers clients send. Each method returns a copy, so you can configure one and reuse it:

php
$api = $this->api()->version('2026-06-01')->withKey($key);

$api->getJson('/v1/orders')->assertJsonApiCollection('orders');
$api->postJson('/v1/orders', $body)->assertJsonApiResource('orders');
MethodWhat it does
version()Sends the Api-Version header.
withKey()Authenticates with an API key's plain-text value, such as the second value ApiKey::issue() returns.
withToken()Authenticates with a bearer token from any configured guard, such as a Sanctum token.
withIdempotencyKey()Sends the Idempotency-Key header.
withHeaders()Sends other headers.
getJson(), postJson(), putJson(), patchJson(), deleteJson()Sends the request and returns an ApiTestResponse.
json()Sends a request with any method.

ApiTestResponse ​

Laravel's TestResponse, with these assertions added. Each returns the response, so they chain.

MethodAsserts that the response
assertJsonApiResource()Succeeded with one resource as data, of the given type and ID when given.
assertJsonApiCollection()Succeeded with a list of resources as data, each of the given type, and the given count when given.
assertApiError()Is an error document with an error of the given code. Pass the status, or an ErrorCode case to use its status.
assertIdempotentReplay()Was replayed from an earlier request with the same Idempotency-Key.
assertNotIdempotentReplay()Ran the request and was not replayed.
assertDeprecated()Was served in a deprecated version, sunsetting on the given date (Y-m-d) when given.
assertNotDeprecated()Was served in a version that is not deprecated.
assertShape()Has the given shape.

assertShape() ​

A shape is the body's keys, with 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
$response->assertShape([
    'data' => ['type' => 'string', 'id' => 'string', 'attributes' => ['name' => 'string', 'total' => 'integer']],
]);

TransformsVersionChanges ​

Unit tests a version change on its own, without routes or the rest of the version history. It boots no application, and it ignores the change's $resources, so the change sees every payload you give it. VersionChangeTestCase is a PHPUnit base class that uses it; use the trait directly in a Pest file whose directory already extends another test case.

php
use FusionStack\Laravel\Testing\VersionChangeTestCase;

final class RenameCustomerNameToFullNameTest extends VersionChangeTestCase
{
    public function test_old_clients_see_name(): void
    {
        $old = $this->assertRoundTrip(new RenameCustomerNameToFullName, [
            'data' => ['type' => 'customers', 'id' => '1', 'attributes' => ['full_name' => 'Ada']],
        ]);

        $this->assertSame('Ada', $old['data']['attributes']['name']);
    }
}
MethodWhat it does
upgradeRequest()Returns an older client's request body migrated to the shape after the change.
downgradeResponse()Returns a latest-shape document migrated back to the shape before the change. Pass a status to test an error document.
assertRoundTrip()Asserts that downgrading the document and upgrading the result gives it back, so old clients can send what they receive, and returns the downgraded document. Not for changes that drop data, such as a removed attribute.

InteractsWithMcp ​

From fusion-stack/mcp. Calls the MCP server as a client would, and returns laravel/mcp's test response.

php
use FusionStack\Mcp\Testing\InteractsWithMcp;

uses(InteractsWithMcp::class);

it('lists orders over MCP', function () {
    $this->callMcpTool('list_orders', ['include' => ['customer']], token: $key)
        ->assertOk()
        ->assertStructuredContentPath('data.0.customer.name', 'Ada');
});
MethodWhat it does
callMcpTool()Calls a tool by name with its arguments, as the holder of token when given.
readMcpResource()Reads a resource by its URI, such as api://v1/order-statuses, as the holder of token when given.

Released under the MIT License.