Skip to content

Add Messenger assertions - #241

Merged
TavoNiievez merged 1 commit into
Codeception:mainfrom
TavoNiievez:messenger-assertions
Jun 25, 2026
Merged

Add Messenger assertions#241
TavoNiievez merged 1 commit into
Codeception:mainfrom
TavoNiievez:messenger-assertions

Conversation

@TavoNiievez

@TavoNiievez TavoNiievez commented Jun 25, 2026

Copy link
Copy Markdown
Member

What

Adds a MessengerAssertionsTrait so functional tests can assert against Symfony Messenger, which until now had no coverage in this module — even though its Messenger-based siblings (Mailer, Notifier) are fully covered.

New $I-> steps:

Step Purpose
seeMessageDispatched(string $messageClass, ?string $bus = null) a message of the given class was dispatched (optionally per bus)
dontSeeMessageDispatched(string $messageClass, ?string $bus = null) no message of the given class was dispatched
seeDispatchedMessageCount(int $expectedCount, ?string $bus = null) exact number of dispatched messages (optionally per bus)
grabDispatchedMessageClasses(?string $bus = null) the dispatched message class names, in dispatch order

These follow the module's naming convention: see*/dontSee* for module-native assertions (no upstream Symfony web-test equivalent, so not assert*), grab* for getters. grabDispatchedMessageClasses() returns class-strings — hence the explicit name, distinct from the object-returning grabSentEmails/grabSentNotifications.

How

Reads Symfony's messenger profiler data collector via the existing grabCollector() helper — the same pattern the Events/Form/Twig traits use. Wires the messenger collector into DataCollectorName and the grabCollector() conditional return-type map.

The collector stores cloned VarDumper snapshots (message class, bus, exceptions), not live message objects, so payload-level assertions are intentionally out of scope. The class-name reader narrows the Data tree to the message.type leaf via ArrayAccess rather than recursively materializing each entry's whole snapshot (message payload, stamps, caller, exception trace) — handling both the plain-string type (Symfony ≤ 7.x) and the ClassStub representation (8.x).

Tests

Adds a self-contained Messenger setup to the test app (framework.messenger default bus + a sample message, handler, controller action and route) and tests/MessengerAssertionsTest.php covering counts, per-bus filtering, positive/negative dispatch checks and the no-dispatch case. Passes phpunit, phpstan (level max) and cs-check.

Note: enabling framework.messenger routes Mailer through the bus by default, so the test app sets mailer.message_bus: false to keep mail synchronous.

Functional tests

Per CONTRIBUTING.md, the matching functional tests are submitted as companion PRs to Codeception/symfony-module-tests (branches 5.4, 6.4, 7.4, 8.0).

Add MessengerAssertionsTrait so functional tests can assert which messages were
dispatched, how many, and on which bus. Like the Events/Mailer/Notifier traits,
it reads Symfony's profiler "messenger" data collector.

Steps: seeMessageDispatched, dontSeeMessageDispatched, seeDispatchedMessageCount
and grabDispatchedMessageClasses. Per the module convention these use the
see*/grab* idiom rather than assert*, which is reserved for direct ports of
Symfony's own test assertions.

Wire a dispatchable TestMessage into the test app (synchronous handler, no
transport) and cover the steps in tests/MessengerAssertionsTest.php.

symfony/messenger is a dev-only dependency; remove it before the Symfony 7.4
functional run, where the fixture app pulls in API Platform and would otherwise
auto-register a Messenger processor bound to an undefined messenger.default_bus.
@TavoNiievez
TavoNiievez force-pushed the messenger-assertions branch from 60c90d4 to 4c55de6 Compare June 25, 2026 18:52
@TavoNiievez
TavoNiievez merged commit 7f0cd5d into Codeception:main Jun 25, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant