Kafka fake

When testing your application, you usually don't want to publish or consume real Kafka messages. Calling Kafka::fake() replaces the Kafka manager with a fake, so no message leaves your application, and you can make assertions about the messages it published:

use Junges\Kafka\Facades\Kafka;

public function test_orders_are_published()
{
    Kafka::fake();

    $this->post('/orders', ['product' => 'book']);

    Kafka::assertPublishedOn('orders', callback: function ($message) {
        return $message->getBody()['product'] === 'book';
    });
}
Sponsorship

Support Laravel Kafka by sponsoring me!

Laravel Kafka is free and Open Source software, built to empower developers like you. Your support helps maintain and enhance the project. If you find it valuable, please consider sponsoring me on GitHub. Every contribution makes a difference and keeps the development going strong! Thank you!

Become a Sponsor Want to hide this message? Sponsor at any tier of $10/month or more!

The fake replaces every connection , so messages published with publish, publishSync or through Kafka::connection('name') are all recorded, and connections that are not configured can be used without a configuration. Global middlewares are kept, so faked consumers go through them like real ones.

The following assertions are available:

They accept the expected message, a callback receiving each published message and returning whether it matches, or both, in which case a message must match both. When comparing messages, their ids are ignored, since every message gets its own.

To test consumers, including consumer classes , see mocking your kafka consumer .