Skip to content

Commit

Permalink
Mirror logic to enable fake builder set the topic name for all the me…
Browse files Browse the repository at this point in the history
…ssages at once (#279)
  • Loading branch information
cvairlis authored Apr 29, 2024
1 parent d1a7da2 commit 66bf2fd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Support/Testing/Fakes/ProducerBuilderFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
19 changes: 19 additions & 0 deletions tests/KafkaFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 66bf2fd

Please sign in to comment.