diff --git a/src/Support/Testing/Fakes/ProducerBuilderFake.php b/src/Support/Testing/Fakes/ProducerBuilderFake.php index 96b8bcd..351550a 100644 --- a/src/Support/Testing/Fakes/ProducerBuilderFake.php +++ b/src/Support/Testing/Fakes/ProducerBuilderFake.php @@ -45,6 +45,7 @@ public static function create(string $broker = null): self public function onTopic(string $topic): self { + $this->topic = $topic; $this->message->onTopic($topic); return $this; @@ -157,6 +158,10 @@ public function send(bool $shouldFlush = false): bool { $producer = $this->build(); + if ($this->message->getTopicName() === null && $this->topic !== '') { + $this->message->onTopic($this->topic); + } + return $producer->produce($this->getMessage()); } diff --git a/tests/KafkaFakeTest.php b/tests/KafkaFakeTest.php index 6069518..28ee740 100644 --- a/tests/KafkaFakeTest.php +++ b/tests/KafkaFakeTest.php @@ -138,6 +138,25 @@ public function testAssertPublishedOn(): void } } + public function testAssertPublishedOnBySpecifyingMessageObject(): void + { + $message = Message::create()->withBody(['test' => ['test']])->withHeaders(['custom' => 'header'])->withKey(Str::uuid()->toString()); + + $producer = $this->fake->publish()->onTopic('topic')->withMessage($message); + + $producer->send(); + + $this->fake->assertPublished($producer->getMessage()); + + $this->fake->assertPublishedOn('topic', $producer->getMessage()); + + try { + $this->fake->assertPublishedOn('not-published-on-this-topic', $producer->getMessage()); + } catch (ExpectationFailedException $exception) { + $this->assertThat($exception, new ExceptionMessageIsOrContains('The expected message was not published.')); + } + } + public function testAssertPublishedOnTimes(): void { $producer = $this->fake->publish()