Projectslaravel-actionsFaking

Laravel Actions

Package

Synchronously dispatched action classes for Laravel.

Upgrade Guide

Faking

Faking has two independent axes:

  1. Scope — which actions are intercepted (fake, with / addFake, except / removeFake)
  2. Execution — what runs when an action is intercepted (allowExecution / disallowExecution)

Scope

use BradieTilley\Actions\Facades\Action;

Action::fake(); // fake all actions
Action::fake([SendInvoice::class, ProvisionTenant::class]);
Action::fake()->except(AssignDefaultRole::class);
Action::fake()->with(SendInvoice::class);

except means the action is not faked — it runs through the real dispatcher (handle(), middleware, events).

Execution mode

When an action is faked and execution is disallowed (the default):

ActionResult
implements IsFakeablehandleFake()
does notnothing (null), still recorded for assertions
Action::fake(); // stub / skip
Action::fake()->allowExecution(); // all faked actions run real handle()
Action::fake()->allowExecution(SendInvoice::class); // only that class
Action::fake()->allowExecution()->disallowExecution(SendInvoice::class);

allowExecution(Class) still treats the action as faked (useful when you called Action::fake() with no list) but runs the real handle(). Prefer except(Class) when you want the action completely outside the fake set.

IsFakeable

Implement IsFakeable and define handleFake() when suppressed actions need a realistic return value:

use BradieTilley\Actions\Action;
use BradieTilley\Actions\Contracts\IsFakeable;

class ResizeImage extends Action implements IsFakeable
{
    public function __construct(public readonly Image $image) {}

    public function handle(Resizer $resizer): Image
    {
        return $resizer->resize($this->image);
    }

    public function handleFake(): Image
    {
        return $this->image;
    }
}

Recording without faking

use BradieTilley\Actions\Facades\Action;

Action::getFacadeRoot()->enableRecording();

AssignDefaultRole::dispatch($user);

Action::assertDispatched(AssignDefaultRole::class);

Assertions

Action::assertDispatched(AssignDefaultRole::class);
Action::assertDispatchedTimes(AssignDefaultRole::class, 2);
Action::assertNotDispatched(SendInvoice::class);
Action::assertNothingDispatched();

Continue to Events.