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

Allow assertPublishedOnTimes to be used with batch messages #180

Merged
merged 2 commits into from
Mar 4, 2023
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
4 changes: 2 additions & 2 deletions src/Producers/MessageBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public function __construct()
* @param Message $message
* @return void
*/
public function push(Message $message)
public function push(Message $message): void
{
$this->messages->push($message);
}

/**
* Returns all messages from batch before producing them to kafka
*
* @return SplDoublyLinkedList
* @return SplDoublyLinkedList<\Junges\Kafka\Message\Message>
*/
public function getMessages(): SplDoublyLinkedList
{
Expand Down
2 changes: 2 additions & 0 deletions src/Support/Testing/Fakes/ProducerFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/KafkaFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down