Skip to content

Async actions: Async::fake() - #14

Merged
ogorzalka merged 1 commit into
mainfrom
feat/async-fake
Oct 7, 2026
Merged

ogorzalka merged 1 commit into
mainfrom
feat/async-fake

Conversation

@ogorzalka

Copy link
Copy Markdown
Member

Phase 2, step F: Async::fake().

Async::fake();

wp_update_post(['ID' => $event->ID, 'post_title' => 'New title']);

Async::assertDispatched(SyncEventToCrm::class, fn (AsyncPayload $payload) =>
    $payload->captured['status'] === 'publish'
);
Async::assertDispatchedTimes(SyncEventToCrm::class, 1);
  • Async::fake() returns an AsyncFake, a driver that records payloads. While it is active, Async::driver() returns it for any name: default, via(), retries.
  • Assertions (static on Async, as in the design doc, and on the fake): assertDispatched($handler, ?$callback), assertDispatchedTimes(), assertNotDispatched(), assertNothingDispatched().
    • A handler is matched by class name, by descriptor (Class@method, a function), or by closure.
    • The callback receives the payload and the delay.
    • Under PHPUnit or Pest they go through PHPUnit\Framework\Assert, so they are counted and read well in reports; otherwise AssertionError.
    • Asserting without fake() throws a LogicException that says so.
  • Faithful: arguments and captures are normalized as for a real driver (an object that cannot travel still fails), and when() and unique() apply.
  • runDispatched() runs the recorded handlers, retries included, then forgets them. Async::flush() removes the fake.

Tests

  • Unit: 233 passed, 14 new, including failure messages for each assertion.
  • Real WordPress: 1 new, 13 in total. Under fake() the fixture handler is recorded with its arguments, and no WP-Cron event is created.
  • PHPStan level 5 and Pint pass.

@ogorzalka
ogorzalka merged commit a5ed74e into main Oct 7, 2026
5 checks passed
@ogorzalka
ogorzalka deleted the feat/async-fake branch October 7, 2026 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant