Skip to content

Commit

Permalink
Fix passthru of missing methods to root (#249)
Browse files Browse the repository at this point in the history
* Make kafka manager aware of fake messages

* Remove unused variable

* Fix how root is instantiated

* Fix how facade swap instances

* Add tests
  • Loading branch information
mateusjunges authored Feb 15, 2024
1 parent 5a2bc10 commit 2a1d2fa
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/Facades/Kafka.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Kafka.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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')
Expand All @@ -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;
}
}
11 changes: 5 additions & 6 deletions src/Support/Testing/Fakes/KafkaFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
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;

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();
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Support/Testing/Fakes/ProducerFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class ProducerFake
{
private array $messages = [];
private ?Closure $producerCallback = null;

public function __construct(
Expand Down
19 changes: 19 additions & 0 deletions tests/KafkaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 2a1d2fa

Please sign in to comment.