From 6e4ef2f5d1ca1b0ef73bd4e74169152c6f7e3355 Mon Sep 17 00:00:00 2001 From: Mateus Date: Sat, 4 Mar 2023 13:25:37 +0200 Subject: [PATCH 1/2] Allow assertPublishedOnTimes to be used with batch messages --- src/Producers/MessageBatch.php | 4 ++-- src/Support/Testing/Fakes/ProducerFake.php | 2 ++ tests/KafkaFakeTest.php | 24 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Producers/MessageBatch.php b/src/Producers/MessageBatch.php index 92c7c314..a4717e3d 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 */ 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') From a03675912543ad52e662018e9e4537438ddfd0cd Mon Sep 17 00:00:00 2001 From: Mateus Date: Sat, 4 Mar 2023 13:42:29 +0200 Subject: [PATCH 2/2] Use FQCN --- src/Producers/MessageBatch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Producers/MessageBatch.php b/src/Producers/MessageBatch.php index a4717e3d..07bcc9af 100644 --- a/src/Producers/MessageBatch.php +++ b/src/Producers/MessageBatch.php @@ -40,7 +40,7 @@ public function push(Message $message): void /** * Returns all messages from batch before producing them to kafka * - * @return SplDoublyLinkedList + * @return SplDoublyLinkedList<\Junges\Kafka\Message\Message> */ public function getMessages(): SplDoublyLinkedList {