Mocking your kafka consumer

If you want to test that your consumers are working correctly, you can mock and execute the consumer to ensure that everything works as expected.

Support Laravel Kafka by sponsoring me!

Do you find Laravel Kafka valuable and wanna support its development?

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!

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

You just need to tell kafka which messages the consumer should receive and then start your consumer. This package will run all the specified messages through the consumer and stop after the last message, so you can perform whatever assertions you want to.

For example, let's say you want to test that a simple blog post was published after consuming a post-published message:

public function test_post_is_marked_as_published()
{
    // First, you use the fake method:
    \Junges\Kafka\Facades\Kafka::fake();
    
    // Then, tells Kafka what messages the consumer should receive:
    \Junges\Kafka\Facades\Kafka::shouldReceiveMessages([
        new \Junges\Kafka\Message\ConsumedMessage(
            topicName: 'mark-post-as-published-topic',
            partition: 0,
            headers: [],
            body: ['post_id' => 1],
            key: null,
            offset: 0,
            timestamp: 0
        ),
    ]);
    
    // Now, instantiate your consumer and start consuming messages. It will consume only the messages
    // specified in `shouldReceiveMessages` method:
    $consumer = \Junges\Kafka\Facades\Kafka::consumer(['mark-post-as-published-topic'])
        ->withHandler(function (\Junges\Kafka\Contracts\ConsumerMessage $message) use (&$posts) {
            $post = Post::find($message->getBody()['post_id']);
    
            $post->update(['published_at' => now()->format("Y-m-d H:i:s")]);
    
            return 0;

        })->build();
        
    $consumer->consume();

    // Now, you can test if the post published_at field is not empty, or anything else you want to test:
    
    $this->assertNotNull($post->refresh()->published_at);
}

It also works for batch messages

Previous
Assert published on times