Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror logic to enable fake builder set the topic name for all the messages at once #279

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading