Skip to content

Testing

The SDK ships helpers in FlowCatalyst\Testing for acting as a FlowCatalyst user, and its interfaces are small enough to fake the rest.

FlowCatalystTestPrincipal::make() builds a FlowCatalystAuthenticatable without any login flow.

use FlowCatalyst\Testing\FlowCatalystTestPrincipal;
$principal = FlowCatalystTestPrincipal::make(
roles: ['shop:editor'],
permissions: ['shop:orders:order:view'], // omit to resolve from the bound PermissionResolver
clients: ['acme'],
applications: ['shop'],
);
$principal->user->getRoles(); // ['shop:editor']
$principal->user->hasPermissionTo(['shop:orders:order:view']); // true

Other optional arguments are sub, email and name; defaults are derived from the roles (prn_test_<roles>, <roles>@example.test). With permissions given, no container is needed. With it omitted, the permissions come from the PermissionResolver bound in the container.

The InteractsWithFlowCatalyst trait wraps it for feature tests:

use FlowCatalyst\Testing\InteractsWithFlowCatalyst;
class OrdersTest extends TestCase
{
use InteractsWithFlowCatalyst;
public function test_editor_can_view(): void
{
$this->actingAsFlowCatalyst(['shop:editor'])
->get('/orders')->assertOk();
}
}

actingAsFlowCatalyst($principalOrRoles, string $guard = 'fc-session') takes a principal or a list of roles, and calls actingAs on the guard you name.

FakePermissionResolver implements PermissionResolver with fixed data: a flat permission list given to every caller, plus a map from role to permissions.

use FlowCatalyst\Auth\Contracts\PermissionResolver;
use FlowCatalyst\Testing\FakePermissionResolver;
$this->app->instance(PermissionResolver::class, new FakePermissionResolver(
permissions: ['shop:orders:order:view'],
byRole: ['shop:editor' => ['shop:orders:order:update']],
));

resolve(array $roles) returns the union, de-duplicated. See Permissions for how the real resolver works.

OutboxManager takes any OutboxDriver, an interface with two methods, insert(array $message) and insertBatch(array $messages). A recording driver lets you assert on the rows without a database:

use FlowCatalyst\Outbox\Contracts\OutboxDriver;
use FlowCatalyst\Outbox\DTOs\CreateEventDto;
use FlowCatalyst\Outbox\OutboxManager;
final class RecordingDriver implements OutboxDriver
{
public array $rows = [];
public function insert(array $message): void { $this->rows[] = $message; }
public function insertBatch(array $messages): void { array_push($this->rows, ...$messages); }
}
$driver = new RecordingDriver();
$outbox = new OutboxManager($driver, 'cli_1'); // the second argument is the row's client_id
$id = $outbox->createEvent(CreateEventDto::create('shop:sales:order:placed', ['a' => 1]));
$payload = json_decode($driver->rows[0]['payload']);
// $driver->rows[0]['type'] === 'EVENT'; $payload->deduplicationId === $id

Rows hold id, type, message_group, payload (a JSON string), status (0), the timestamps, client_id, payload_size and headers. The payload of an event carries its data as an embedded JSON string. To assert in a feature test against a real database, use Laravel’s usual assertDatabaseHas('outbox_messages', [...]) inside the test’s transaction. Set FLOWCATALYST_OUTBOX_STRICT_TRANSACTIONS=true in the test environment to catch writes made outside a transaction (RefreshDatabase wraps tests in one).

The control-plane client and outbox are facades, so Mockery works:

use FlowCatalyst\Facades\FlowCatalyst;
use FlowCatalyst\Facades\Outbox;
FlowCatalyst::shouldReceive('eventTypes->list')->andReturn($list);
Outbox::shouldReceive('createEvent')->andReturn('0HZXEQ5Y8JY5Z');

Prefer the recording driver when the event’s contents matter, and facade mocks when you only need the call to succeed.

Webhook tests sign a body with the configured secret; see Webhooks. For field requests see Field.

The SDK’s PHPUnit suite lives under tests/ (Unit and Feature). It includes a local mock OIDC issuer with a real RS256 key pair (tests/Support/MockIssuer.php), started with PHP’s built-in server, for end-to-end login and refresh tests. These are test-only classes under the FlowCatalyst\Tests namespace, which is a dev autoload mapping and is not available to applications. If tests slow to a crawl, check whether Xdebug is trying to connect.