Skip to content

Record the updates in the test environment and add MercureAssertionsTrait - #140

Open
Amoifr wants to merge 2 commits into
symfony:mainfrom
Amoifr:mercure-assertions
Open

Amoifr wants to merge 2 commits into
symfony:mainfrom
Amoifr:mercure-assertions

Conversation

@Amoifr

@Amoifr Amoifr commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #138, following the API discussed there.

When framework.test is enabled, each hub is now decorated with a recording hub that keeps the updates instead of publishing them. No when@test config or HubStub is needed anymore, it doesn't depend on kernel.debug and the profiler, and it also covers FrankenPhpHub, which never goes through the HTTP client. publish() returns the update's id, or a generated urn:uuid: one.

The new MercureAssertionsTrait reads these updates from a KernelTestCase or a WebTestCase, where $hub = null means all the hubs:

use Symfony\Bundle\MercureBundle\Test\MercureAssertionsTrait;

$client->request('POST', '/books', ['title' => 'Dune']);

$this->assertMercureUpdateCount(1);
$this->assertMercureUpdatePublished('https://example.com/books/1');
$this->assertJsonStringEqualsJsonString('{"title":"Dune"}', $this->getMercureUpdate()->getData());

A few implementation notes:

  • The recording hub is applied before the profiler's TraceableHub, so the profiler still lists the updates in the test env.
  • The recorder is tagged kernel.reset, like the traceable hub, so each request starts empty (same behavior as MailerAssertionsTrait).
  • The recorder service is public: the test container only exposes private services once they're instantiated, so assertMercureUpdateCount(0) would otherwise fail before anything was published.
  • An unknown hub name throws an \InvalidArgumentException listing the defined hubs, rather than silently counting 0 updates.

In the issue I suggested leaving real publishing in the test env out until someone asks for it, but Panther's web server runs in the test env by default, so its Mercure end-to-end tests would silently stop receiving updates. The new record_updates_in_test hub option (default true) lets them opt out, and the CHANGELOG flags it as a behavior change.

Docs: symfony/symfony-docs#23471

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.

Help testing the updates that an app publishes

1 participant