From 2a1d2fad0213b4c56e6f28cc8161152344311ee7 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Thu, 15 Feb 2024 14:37:51 -0300 Subject: [PATCH] Fix passthru of missing methods to root (#249) * Make kafka manager aware of fake messages * Remove unused variable * Fix how root is instantiated * Fix how facade swap instances * Add tests --- src/Facades/Kafka.php | 4 +++- src/Kafka.php | 20 ++++++++++++++++++++ src/Support/Testing/Fakes/KafkaFake.php | 11 +++++------ src/Support/Testing/Fakes/ProducerFake.php | 1 - tests/KafkaTest.php | 19 +++++++++++++++++++ 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/Facades/Kafka.php b/src/Facades/Kafka.php index b935b7a9..2f28ea8a 100644 --- a/src/Facades/Kafka.php +++ b/src/Facades/Kafka.php @@ -28,7 +28,9 @@ class Kafka extends Facade */ public static function fake(): KafkaFake { - static::swap($fake = new KafkaFake(static::getFacadeRoot())); + static::swap($fake = new KafkaFake( + (static::getFacadeRoot())->shouldFake() + )); return $fake; } diff --git a/src/Kafka.php b/src/Kafka.php index 3573a899..0f7f71d2 100644 --- a/src/Kafka.php +++ b/src/Kafka.php @@ -8,11 +8,14 @@ use Junges\Kafka\Contracts\CanProduceMessages; use Junges\Kafka\Contracts\CanPublishMessagesToKafka; use Junges\Kafka\Producers\ProducerBuilder; +use Junges\Kafka\Support\Testing\Fakes\KafkaFake; class Kafka implements CanPublishMessagesToKafka, CanConsumeMessagesFromKafka { use Macroable; + private bool $shouldFake = false; + /** * Creates a new ProducerBuilder instance, setting brokers and topic. * @@ -22,6 +25,10 @@ class Kafka implements CanPublishMessagesToKafka, CanConsumeMessagesFromKafka */ public function publishOn(string $topic, string $broker = null): CanProduceMessages { + if ($this->shouldFake) { + return Facades\Kafka::fake()->publishOn($topic, $broker); + } + return new ProducerBuilder( topic: $topic, broker: $broker ?? config('kafka.brokers') @@ -38,10 +45,23 @@ public function publishOn(string $topic, string $broker = null): CanProduceMessa */ public function createConsumer(array $topics = [], string $groupId = null, string $brokers = null): ConsumerBuilder { + if ($this->shouldFake) { + return Facades\Kafka::fake()->createConsumer( + $topics, $groupId, $brokers + ); + } + return ConsumerBuilder::create( brokers: $brokers ?? config('kafka.brokers'), topics: $topics, groupId: $groupId ?? config('kafka.consumer_group_id') ); } + + public function shouldFake(): self + { + $this->shouldFake = true; + + return $this; + } } diff --git a/src/Support/Testing/Fakes/KafkaFake.php b/src/Support/Testing/Fakes/KafkaFake.php index c505d174..3986a967 100644 --- a/src/Support/Testing/Fakes/KafkaFake.php +++ b/src/Support/Testing/Fakes/KafkaFake.php @@ -9,6 +9,7 @@ use Junges\Kafka\Contracts\CanPublishMessagesToKafka; use Junges\Kafka\Contracts\KafkaConsumerMessage; use Junges\Kafka\Contracts\KafkaProducerMessage; +use Junges\Kafka\Kafka; use Junges\Kafka\Message\Message; use PHPUnit\Framework\Assert as PHPUnit; @@ -16,16 +17,15 @@ class KafkaFake implements CanPublishMessagesToKafka { use ForwardsCalls; - private CanPublishMessagesToKafka&CanConsumeMessagesFromKafka $manager; + private ?Kafka $manager; private array $publishedMessages = []; /** @var \Junges\Kafka\Contracts\KafkaConsumerMessage[] */ private array $messagesToConsume = []; - public function __construct(CanPublishMessagesToKafka&CanConsumeMessagesFromKafka $manager) + public function __construct(?Kafka $manager = null) { - $this->manager = $manager; - $this->makeProducerBuilderFake(); + $this->manager = $manager?->shouldFake(); } /** @@ -164,8 +164,7 @@ private function makeProducerBuilderFake(string $topic = '', ?string $broker = n return (new ProducerBuilderFake( topic: $topic, broker: $broker - ) - )->withProducerCallback(fn (Message $message) => $this->publishedMessages[] = $message); + ))->withProducerCallback(fn (Message $message) => $this->publishedMessages[] = $message); } /** diff --git a/src/Support/Testing/Fakes/ProducerFake.php b/src/Support/Testing/Fakes/ProducerFake.php index 4e244aa5..55bb96a2 100644 --- a/src/Support/Testing/Fakes/ProducerFake.php +++ b/src/Support/Testing/Fakes/ProducerFake.php @@ -10,7 +10,6 @@ class ProducerFake { - private array $messages = []; private ?Closure $producerCallback = null; public function __construct( diff --git a/tests/KafkaTest.php b/tests/KafkaTest.php index 1eb82302..6d527914 100644 --- a/tests/KafkaTest.php +++ b/tests/KafkaTest.php @@ -308,4 +308,23 @@ public function testMacro() $this->assertInstanceOf(ProducerBuilder::class, $producer); $this->assertEquals($sasl, $this->getPropertyWithReflection('saslConfig', $producer)); } + + /** @test */ + public function it_stores_published_messages_when_using_macros(): void + { + $expectedMessage = (new Message) + ->withBodyKey('test', ['test']) + ->withHeaders(['custom' => 'header']) + ->withKey($uuid = Str::uuid()->toString()); + + Kafka::macro('testProducer', function () use ($expectedMessage) { + return $this->publishOn('topic') + ->withMessage($expectedMessage); + }); + + Kafka::fake(); + Kafka::testProducer()->send(); + + Kafka::assertPublished($expectedMessage); + } }