diff --git a/src/Producers/MessageBatch.php b/src/Producers/MessageBatch.php index 92c7c314..07bcc9af 100644 --- a/src/Producers/MessageBatch.php +++ b/src/Producers/MessageBatch.php @@ -32,7 +32,7 @@ public function __construct() * @param Message $message * @return void */ - public function push(Message $message) + public function push(Message $message): void { $this->messages->push($message); } @@ -40,7 +40,7 @@ public function push(Message $message) /** * Returns all messages from batch before producing them to kafka * - * @return SplDoublyLinkedList + * @return SplDoublyLinkedList<\Junges\Kafka\Message\Message> */ public function getMessages(): SplDoublyLinkedList { diff --git a/src/Support/Testing/Fakes/ProducerFake.php b/src/Support/Testing/Fakes/ProducerFake.php index d1c29fa8..9beccd5f 100644 --- a/src/Support/Testing/Fakes/ProducerFake.php +++ b/src/Support/Testing/Fakes/ProducerFake.php @@ -47,7 +47,9 @@ public function produceBatch(MessageBatch $messageBatch): int if ($this->producerCallback !== null) { $callback = $this->producerCallback; + /** @var Message $message */ foreach ($messageBatch->getMessages() as $message) { + $message->setTopicName($this->topic); $callback($message); $produced++; } diff --git a/tests/KafkaFakeTest.php b/tests/KafkaFakeTest.php index 23aa64f8..517fe429 100644 --- a/tests/KafkaFakeTest.php +++ b/tests/KafkaFakeTest.php @@ -152,6 +152,30 @@ public function testAssertPublishedOnTimes(): void } } + public function testAssertPublishedOnTimesForBatchMessages(): void + { + $producer = $this->fake->publishOn('batch-topic') + ->withConfigOption('key', 'value'); + + $message = new Message( + headers: ['header-key' => 'header-value'], + body: ['body-key' => 'body-value'], + key: 2 + ); + + $messageBatch = new MessageBatch(); + $messageBatch->push($message); + $messageBatch->push($message); + + $producer->sendBatch($messageBatch); + + $this->fake->assertPublishedTimes(2); + $this->fake->assertpublished(); + $this->fake->assertPublishedOnTimes('batch-topic', 2); + $this->fake->assertPublishedOn('batch-topic'); + + } + public function testICanPerformAssertionsUsingAssertPublishedOn(): void { $producer = $this->fake->publishOn('topic')