From 19a7e309426a4bfa8c282904938f6f81389b0b15 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Wed, 14 Feb 2024 20:12:03 -0300 Subject: [PATCH] Rename consumer builder --- .../{ConsumerBuilder.php => Builder.php} | 4 +- src/Contracts/ConsumerBuilder.php | 6 +- src/Facades/Kafka.php | 2 +- src/Factory.php | 6 +- ...onsumerBuilderFake.php => BuilderFake.php} | 6 +- src/Support/Testing/Fakes/KafkaFake.php | 4 +- tests/Consumers/ConsumerBuilderTest.php | 56 +++++++++---------- tests/KafkaTest.php | 8 +-- 8 files changed, 46 insertions(+), 46 deletions(-) rename src/Consumers/{ConsumerBuilder.php => Builder.php} (99%) rename src/Support/Testing/Fakes/{ConsumerBuilderFake.php => BuilderFake.php} (94%) diff --git a/src/Consumers/ConsumerBuilder.php b/src/Consumers/Builder.php similarity index 99% rename from src/Consumers/ConsumerBuilder.php rename to src/Consumers/Builder.php index 25042c50..0da8a326 100644 --- a/src/Consumers/ConsumerBuilder.php +++ b/src/Consumers/Builder.php @@ -21,7 +21,7 @@ use Junges\Kafka\Support\Timer; use RdKafka\TopicPartition; -class ConsumerBuilder implements ConsumerBuilderContract +class Builder implements ConsumerBuilderContract { use InteractsWithConfigCallbacks; use Conditionable; @@ -83,7 +83,7 @@ protected function __construct(protected string $brokers, array $topics = [], pr /** @inheritDoc */ public static function create(string $brokers, array $topics = [], string $groupId = null): self { - return new ConsumerBuilder( + return new Builder( brokers: $brokers, topics: $topics, groupId: $groupId diff --git a/src/Contracts/ConsumerBuilder.php b/src/Contracts/ConsumerBuilder.php index 04eed0e0..13562582 100644 --- a/src/Contracts/ConsumerBuilder.php +++ b/src/Contracts/ConsumerBuilder.php @@ -42,7 +42,7 @@ public function withMaxMessages(int $maxMessages): self; * Define the max number seconds that a consumer should run * * @param int $maxTime - * @return \Junges\Kafka\Consumers\ConsumerBuilder + * @return \Junges\Kafka\Consumers\Builder */ public function withMaxTime(int $maxTime): self; @@ -50,7 +50,7 @@ public function withMaxTime(int $maxTime): self; * Specify the max retries attempts. * * @param int $maxCommitRetries - * @return \Junges\Kafka\Consumers\ConsumerBuilder + * @return \Junges\Kafka\Consumers\Builder */ public function withMaxCommitRetries(int $maxCommitRetries): self; @@ -71,7 +71,7 @@ public function withSasl(string $username, string $password, string $mechanisms, * the message itself and the second is the next handler * * @param callable(mixed, callable): void $middleware - * @return \Junges\Kafka\Consumers\ConsumerBuilder + * @return \Junges\Kafka\Consumers\Builder */ public function withMiddleware(callable $middleware): self; diff --git a/src/Facades/Kafka.php b/src/Facades/Kafka.php index 9a3a69e2..df7c978d 100644 --- a/src/Facades/Kafka.php +++ b/src/Facades/Kafka.php @@ -8,7 +8,7 @@ /** * @method static \Junges\Kafka\Contracts\MessageProducer publish(string $broker = null) - * @method static \Junges\Kafka\Consumers\ConsumerBuilder createConsumer(array $topics = [], string $groupId = null, string $brokers = null) + * @method static \Junges\Kafka\Consumers\Builder createConsumer(array $topics = [], string $groupId = null, string $brokers = null) * @method static void assertPublished(ProducerMessage $expectedMessage = null, callable $callback = null) * @method static void assertPublishedTimes(int $times = 1, ProducerMessage $expectedMessage = null, callable $callback = null) * @method static void assertPublishedOn(string $topic, ProducerMessage $expectedMessage = null, callable $callback = null) diff --git a/src/Factory.php b/src/Factory.php index 0ef4d64f..cf1e5006 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -3,10 +3,10 @@ namespace Junges\Kafka; use Illuminate\Support\Traits\Macroable; -use Junges\Kafka\Consumers\ConsumerBuilder; +use Junges\Kafka\Consumers\Builder as ConsumerBuilder; use Junges\Kafka\Contracts\KafkaManager; use Junges\Kafka\Contracts\MessageProducer; -use Junges\Kafka\Producers\Builder; +use Junges\Kafka\Producers\Builder as ProducerBuilder; class Factory implements KafkaManager { @@ -15,7 +15,7 @@ class Factory implements KafkaManager /** Creates a new ProducerBuilder instance, setting brokers and topic. */ public function publish(string $broker = null): MessageProducer { - return new Builder( + return new ProducerBuilder( broker: $broker ?? config('kafka.brokers') ); } diff --git a/src/Support/Testing/Fakes/ConsumerBuilderFake.php b/src/Support/Testing/Fakes/BuilderFake.php similarity index 94% rename from src/Support/Testing/Fakes/ConsumerBuilderFake.php rename to src/Support/Testing/Fakes/BuilderFake.php index 7c112793..a89744a0 100644 --- a/src/Support/Testing/Fakes/ConsumerBuilderFake.php +++ b/src/Support/Testing/Fakes/BuilderFake.php @@ -8,13 +8,13 @@ use Junges\Kafka\Config\NullBatchConfig; use Junges\Kafka\Consumers\CallableBatchConsumer; use Junges\Kafka\Consumers\CallableConsumer; -use Junges\Kafka\Consumers\ConsumerBuilder; +use Junges\Kafka\Consumers\Builder; use Junges\Kafka\Contracts\ConsumerBuilder as ConsumerBuilderContract; use Junges\Kafka\Contracts\HandlesBatchConfiguration; use Junges\Kafka\Contracts\MessageConsumer; use Junges\Kafka\Support\Timer; -class ConsumerBuilderFake extends ConsumerBuilder implements ConsumerBuilderContract +class BuilderFake extends Builder implements ConsumerBuilderContract { /** @var \Junges\Kafka\Contracts\ConsumerMessage[] */ private array $messages = []; @@ -22,7 +22,7 @@ class ConsumerBuilderFake extends ConsumerBuilder implements ConsumerBuilderCont /** @inheritDoc */ public static function create(string $brokers, array $topics = [], string $groupId = null): self { - return new ConsumerBuilderFake( + return new BuilderFake( brokers: $brokers, topics: $topics, groupId: $groupId diff --git a/src/Support/Testing/Fakes/KafkaFake.php b/src/Support/Testing/Fakes/KafkaFake.php index b631f222..b98052a9 100644 --- a/src/Support/Testing/Fakes/KafkaFake.php +++ b/src/Support/Testing/Fakes/KafkaFake.php @@ -34,9 +34,9 @@ public function publish(?string $broker = null): ProducerBuilderFake } /** Return a ConsumerBuilder instance. */ - public function createConsumer(array $topics = [], string $groupId = null, string $brokers = null): ConsumerBuilderFake + public function createConsumer(array $topics = [], string $groupId = null, string $brokers = null): BuilderFake { - return ConsumerBuilderFake::create( + return BuilderFake::create( brokers: $brokers ?? config('kafka.brokers'), topics: $topics, groupId: $groupId ?? config('kafka.consumer_group_id') diff --git a/tests/Consumers/ConsumerBuilderTest.php b/tests/Consumers/ConsumerBuilderTest.php index cd0d5059..b96d3f2e 100644 --- a/tests/Consumers/ConsumerBuilderTest.php +++ b/tests/Consumers/ConsumerBuilderTest.php @@ -8,7 +8,7 @@ use Junges\Kafka\Config\Config; use Junges\Kafka\Config\Sasl; use Junges\Kafka\Consumers\Consumer; -use Junges\Kafka\Consumers\ConsumerBuilder; +use Junges\Kafka\Consumers\Builder; use Junges\Kafka\Contracts\Committer; use Junges\Kafka\Contracts\CommitterFactory; use Junges\Kafka\Exceptions\ConsumerException; @@ -22,14 +22,14 @@ final class ConsumerBuilderTest extends LaravelKafkaTestCase { public function testItReturnsAConsumerInstance(): void { - $consumer = ConsumerBuilder::create('broker')->build(); + $consumer = Builder::create('broker')->build(); $this->assertInstanceOf(Consumer::class, $consumer); } public function testItCanSubscribeToATopic(): void { - $consumer = ConsumerBuilder::create('broker'); + $consumer = Builder::create('broker'); $consumer->subscribe('foo'); @@ -40,7 +40,7 @@ public function testItCanSubscribeToATopic(): void public function testItDoesNotSubscribeToATopicTwice(): void { - $consumer = ConsumerBuilder::create('broker'); + $consumer = Builder::create('broker'); $consumer->subscribe('foo', 'foo'); @@ -51,7 +51,7 @@ public function testItDoesNotSubscribeToATopicTwice(): void public function testICanChangeDeserializersOnTheFly(): void { - $consumer = ConsumerBuilder::create('broker'); + $consumer = Builder::create('broker'); $consumer->usingDeserializer(new JsonDeserializer()); @@ -62,7 +62,7 @@ public function testICanChangeDeserializersOnTheFly(): void public function testItCanSubscribeToMoreThanOneTopicsAtOnce(): void { - $consumer = ConsumerBuilder::create('broker'); + $consumer = Builder::create('broker'); $consumer->subscribe('foo', 'bar'); @@ -70,7 +70,7 @@ public function testItCanSubscribeToMoreThanOneTopicsAtOnce(): void $this->assertEquals(['foo', 'bar'], $topics); - $consumer = ConsumerBuilder::create('broker'); + $consumer = Builder::create('broker'); $consumer->subscribe(['foo', 'bar']); @@ -81,7 +81,7 @@ public function testItCanSubscribeToMoreThanOneTopicsAtOnce(): void public function testItCanSetConsumerGroupId(): void { - $consumer = ConsumerBuilder::create('broker')->withConsumerGroupId('foo'); + $consumer = Builder::create('broker')->withConsumerGroupId('foo'); $groupId = $this->getPropertyWithReflection('groupId', $consumer); @@ -92,12 +92,12 @@ public function testItThrowsInvalidArgumentExceptionIfCreatingWithInvalidTopic() { $this->expectException(InvalidArgumentException::class); - ConsumerBuilder::create('broker', [1234], 'group'); + Builder::create('broker', [1234], 'group'); } public function testItCanSaveTheCommitBatchSize(): void { - $consumer = ConsumerBuilder::create('broker') + $consumer = Builder::create('broker') ->withCommitBatchSize(1); $commitValue = $this->getPropertyWithReflection('commit', $consumer); @@ -107,7 +107,7 @@ public function testItCanSaveTheCommitBatchSize(): void public function testItUsesTheCorrectHandler(): void { - $consumer = ConsumerBuilder::create('broker')->withHandler(new FakeConsumer()); + $consumer = Builder::create('broker')->withHandler(new FakeConsumer()); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -118,7 +118,7 @@ public function testItUsesTheCorrectHandler(): void public function testItCanSetMaxMessages(): void { - $consumer = ConsumerBuilder::create('broker')->withMaxMessages(2); + $consumer = Builder::create('broker')->withMaxMessages(2); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -129,7 +129,7 @@ public function testItCanSetMaxMessages(): void public function testItCanSetMaxCommitRetries(): void { - $consumer = ConsumerBuilder::create('broker')->withMaxCommitRetries(2); + $consumer = Builder::create('broker')->withMaxCommitRetries(2); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -140,7 +140,7 @@ public function testItCanSetMaxCommitRetries(): void public function testItCanSetTheDeadLetterQueue(): void { - $consumer = ConsumerBuilder::create('broker')->subscribe('test')->withDlq('test-topic-dlq'); + $consumer = Builder::create('broker')->subscribe('test')->withDlq('test-topic-dlq'); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -151,7 +151,7 @@ public function testItCanSetTheDeadLetterQueue(): void public function testItUsesDlqSuffixIfDlqIsNull(): void { - $consumer = ConsumerBuilder::create('broker', ['foo'])->withDlq(); + $consumer = Builder::create('broker', ['foo'])->withDlq(); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -162,7 +162,7 @@ public function testItUsesDlqSuffixIfDlqIsNull(): void public function testItCanSetSasl(): void { - $consumer = ConsumerBuilder::create('broker') + $consumer = Builder::create('broker') ->withSasl('username', 'password', 'mechanisms'); $expectedSaslConfig = new Sasl('username', 'password', 'mechanisms'); @@ -176,7 +176,7 @@ public function testItCanSetSasl(): void public function testItCanAddMiddlewaresToTheHandler(): void { - $consumer = ConsumerBuilder::create('broker', ['foo'], 'group') + $consumer = Builder::create('broker', ['foo'], 'group') ->withMiddleware(function ($message, callable $next) { $next($message); }); @@ -192,7 +192,7 @@ public function testItCanAddMiddlewaresToTheHandler(): void public function testItCanAddInvokableClassesAsMiddleware(): void { - $consumer = ConsumerBuilder::create('broker', ['foo'], 'group') + $consumer = Builder::create('broker', ['foo'], 'group') ->withMiddleware(new TestMiddleware()); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -206,7 +206,7 @@ public function testItCanAddInvokableClassesAsMiddleware(): void public function testItCanSetSecurityProtocol(): void { - $consumer = ConsumerBuilder::create('broker', ['foo'], 'group') + $consumer = Builder::create('broker', ['foo'], 'group') ->withSecurityProtocol('security'); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -218,7 +218,7 @@ public function testItCanSetSecurityProtocol(): void public function testItCanSetSecurityProtocolViaSaslConfig(): void { - $consumer = ConsumerBuilder::create('broker', ['foo'], 'group') + $consumer = Builder::create('broker', ['foo'], 'group') ->withSasl( 'username', 'password', @@ -237,7 +237,7 @@ public function testItCanSetSecurityProtocolViaSaslConfig(): void public function testItCanSetAutoCommit(): void { - $consumer = ConsumerBuilder::create('broker')->withAutoCommit(); + $consumer = Builder::create('broker')->withAutoCommit(); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -245,7 +245,7 @@ public function testItCanSetAutoCommit(): void $this->assertTrue($autoCommit); - $consumer = ConsumerBuilder::create('broker')->withAutoCommit(false); + $consumer = Builder::create('broker')->withAutoCommit(false); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -256,7 +256,7 @@ public function testItCanSetAutoCommit(): void public function testItCanSetStopAfterLastMessage(): void { - $consumer = ConsumerBuilder::create('broker')->stopAfterLastMessage(); + $consumer = Builder::create('broker')->stopAfterLastMessage(); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -264,7 +264,7 @@ public function testItCanSetStopAfterLastMessage(): void $this->assertTrue($autoCommit); - $consumer = ConsumerBuilder::create('broker')->stopAfterLastMessage(false); + $consumer = Builder::create('broker')->stopAfterLastMessage(false); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -275,7 +275,7 @@ public function testItCanSetStopAfterLastMessage(): void public function testItCanSetConsumerOptions(): void { - $consumer = ConsumerBuilder::create('broker') + $consumer = Builder::create('broker') ->withOptions([ 'auto.offset.reset' => 'latest', 'enable.auto.commit' => 'false', @@ -294,7 +294,7 @@ public function testItCanSetConsumerOptions(): void public function testItCanSpecifyBrokersUsingWithBrokers(): void { - $consumer = ConsumerBuilder::create('broker')->withBrokers('my-test-broker'); + $consumer = Builder::create('broker')->withBrokers('my-test-broker'); $this->assertInstanceOf(Consumer::class, $consumer->build()); @@ -311,7 +311,7 @@ public function make(KafkaConsumer $kafkaConsumer, Config $config): Committer return new VoidCommitter(); } }; - $consumer = ConsumerBuilder::create('broker') + $consumer = Builder::create('broker') ->usingCommitterFactory($adhocCommitterFactory) ->build(); @@ -323,7 +323,7 @@ public function testItCantCreateAConsumerWithDlqWithoutSubscribingToAnyTopics(): { $this->expectException(ConsumerException::class); - ConsumerBuilder::create('broker')->withDlq(); + Builder::create('broker')->withDlq(); } } diff --git a/tests/KafkaTest.php b/tests/KafkaTest.php index 69c683c5..0adcc347 100644 --- a/tests/KafkaTest.php +++ b/tests/KafkaTest.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; use Junges\Kafka\Config\Sasl; -use Junges\Kafka\Consumers\ConsumerBuilder; +use Junges\Kafka\Consumers\Builder as ConsumerBuilder; use Junges\Kafka\Contracts\ProducerMessage; use Junges\Kafka\Events\BatchMessagePublished; use Junges\Kafka\Events\CouldNotPublishMessage as CouldNotPublishMessageEvent; @@ -17,7 +17,7 @@ use Junges\Kafka\Message\Message; use Junges\Kafka\Message\Serializers\JsonSerializer; use Junges\Kafka\Producers\MessageBatch; -use Junges\Kafka\Producers\Builder; +use Junges\Kafka\Producers\Builder as ProducerBuilder; use Mockery as m; use RdKafka\Producer; use RdKafka\ProducerTopic; @@ -193,7 +193,7 @@ public function testICanDisableDebugUsingWithDebugDisabledMethod(): void return $mockedProducer; }); - /** @var Builder $producer */ + /** @var ProducerBuilder $producer */ $producer = Kafka::publish() ->withConfigOptions([ 'metadata.broker.list' => 'broker', @@ -343,7 +343,7 @@ public function testMacro(): void $producer = Kafka::defaultProducer(); - $this->assertInstanceOf(Builder::class, $producer); + $this->assertInstanceOf(ProducerBuilder::class, $producer); $this->assertEquals($sasl, $this->getPropertyWithReflection('saslConfig', $producer)); } }