Skip to content

Commit

Permalink
Add macroable trait to Kafka class (#221)
Browse files Browse the repository at this point in the history
In order to make use of macros the Illuminate Macroable trait has been
added to Junges\Kafka\Kafka.

Co-authored-by: Alexander Lentner <[email protected]>
  • Loading branch information
lentex and Alexander Lentner authored Sep 23, 2023
1 parent 5097762 commit 445aa85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Kafka.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Junges\Kafka;

use Illuminate\Support\Traits\Macroable;
use Junges\Kafka\Consumers\ConsumerBuilder;
use Junges\Kafka\Contracts\CanConsumeMessagesFromKafka;
use Junges\Kafka\Contracts\CanProduceMessages;
Expand All @@ -10,6 +11,8 @@

class Kafka implements CanPublishMessagesToKafka, CanConsumeMessagesFromKafka
{
use Macroable;

/**
* Creates a new ProducerBuilder instance, setting brokers and topic.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/KafkaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Junges\Kafka\Tests;

use Illuminate\Support\Str;
use Junges\Kafka\Config\Sasl;
use Junges\Kafka\Consumers\ConsumerBuilder;
use Junges\Kafka\Contracts\KafkaProducerMessage;
use Junges\Kafka\Exceptions\CouldNotPublishMessage;
Expand Down Expand Up @@ -293,4 +294,18 @@ public function testSendMessageBatch()

Kafka::publishOn('test')->withBodyKey('foo', 'bar')->sendBatch($messageBatch);
}

public function testMacro()
{
$sasl = new Sasl(username: 'username', password: 'password', mechanisms: 'mechanisms');

Kafka::macro('defaultProducer', function (string $topic) use ($sasl) {
return $this->publishOn($topic)->withSasl($sasl);
});

$producer = Kafka::defaultProducer('test-topic');

$this->assertInstanceOf(ProducerBuilder::class, $producer);
$this->assertEquals($sasl, $this->getPropertyWithReflection('saslConfig', $producer));
}
}

0 comments on commit 445aa85

Please sign in to comment.