From 6b3e863f14dba2403fec56f849e97f36ddf877d9 Mon Sep 17 00:00:00 2001 From: Alexander Lentner Date: Fri, 22 Sep 2023 07:44:59 +0200 Subject: [PATCH] Add macroable trait to Kafka class In order to make use of macros the Illuminate Macroable trait has been added to Junges\Kafka\Kafka. --- src/Kafka.php | 3 +++ tests/KafkaTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Kafka.php b/src/Kafka.php index e1747565..3573a899 100644 --- a/src/Kafka.php +++ b/src/Kafka.php @@ -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; @@ -10,6 +11,8 @@ class Kafka implements CanPublishMessagesToKafka, CanConsumeMessagesFromKafka { + use Macroable; + /** * Creates a new ProducerBuilder instance, setting brokers and topic. * diff --git a/tests/KafkaTest.php b/tests/KafkaTest.php index 8867ef76..1eb82302 100644 --- a/tests/KafkaTest.php +++ b/tests/KafkaTest.php @@ -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; @@ -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)); + } }