This is the documentation for
v2.0.
You can switch versions in the menu on the left/at the top.
Check your current version with the following command:
composer show mateusjunges/laravel-kafka
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.
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()
{
\Junges\Kafka\Facades\Kafka::fake();
\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
),
]);
$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();
$this->assertNotNull($post->refresh()->published_at);
}
It also works for batch messages