Skip to content

Testing

Workflowable provides testing utilities to help you test workflows in your application.

TestsWorkflows Trait

The TestsWorkflows trait provides helper methods for creating and asserting workflow behavior in your tests.

php
use Workflowable\Workflowable\Tests\Fixtures\TestsWorkflows;

// In Pest
uses(TestsWorkflows::class);

it('processes an order workflow', function () {
    $this->registerAction('process_order', ProcessOrder::class);

    $workflow = $this->createWorkflow('order_flow', [
        'process' => ['type' => 'action', 'handler' => 'process_order'],
    ], [
        'process' => 'completed',
    ]);

    $instance = $this->startWorkflow('order_flow', ['orderId' => 1]);

    $this->assertWorkflowStatus($instance, 'completed');
    $this->assertWorkflowVariable($instance, 'processed', true);
});

Available Assertions

MethodDescription
assertWorkflowStatus($instance, $status)Assert instance is in a specific status
assertWorkflowAtStep($instance, $step)Assert instance is at a specific step
assertWorkflowVariable($instance, $key, $value)Assert a workflow variable value
assertStoredEventExists($instance, $eventClass)Assert a stored domain event exists

Testing Conditional Branching

php
it('routes high-value orders to extra processing', function () {
    $this->registerAction('process', ProcessOrder::class);
    $this->registerAction('high_value', HighValueProcessing::class);
    $this->registerConditional('is_high_value', IsHighValue::class);

    $this->createWorkflow('order_flow', [
        'check' => ['type' => 'conditional', 'handler' => 'is_high_value'],
        'high_value' => ['type' => 'action', 'handler' => 'high_value'],
        'process' => ['type' => 'action', 'handler' => 'process'],
    ], [
        'check' => ['true' => 'high_value', 'false' => 'process'],
        'high_value' => 'completed',
        'process' => 'completed',
    ]);

    // High value - goes to high_value step
    $instance = $this->startWorkflow('order_flow', ['high_value' => true]);
    $this->assertWorkflowStatus($instance, 'completed');

    // Low value - goes to process step
    $instance = $this->startWorkflow('order_flow', ['high_value' => false]);
    $this->assertWorkflowStatus($instance, 'completed');
});

Running the Package Test Suite

bash
composer test

The test suite requires a MariaDB instance. A docker-compose.yml is provided:

bash
docker compose up -d
composer test