Skip to content

Commit

Permalink
Fix passthru of missing methods into root instance (#250)
Browse files Browse the repository at this point in the history
* Remove unused variable

* Add tests

* Pass method call to fake instance when faking messages

* Use fake instance when faking messages

* Rename createConsumer to consumer

* Revert "Rename createConsumer to consumer"

This reverts commit 8dce52a.
  • Loading branch information
mateusjunges authored Feb 15, 2024
1 parent e24fe03 commit 32dc3d0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Facades/Kafka.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class Kafka extends Facade
/** Replace the bound instance with a fake. */
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/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
use Junges\Kafka\Consumers\Builder as ConsumerBuilder;
use Junges\Kafka\Contracts\KafkaManager;
use Junges\Kafka\Contracts\MessageProducer;
use Junges\Kafka\Facades\Kafka;
use Junges\Kafka\Producers\Builder as ProducerBuilder;

class Factory implements KafkaManager
{
use Macroable;

private bool $shouldFake = false;

/** Creates a new ProducerBuilder instance, setting brokers and topic. */
public function publish(string $broker = null): MessageProducer
{
if ($this->shouldFake) {
return Kafka::fake()->publish($broker);
}

return new ProducerBuilder(
broker: $broker ?? config('kafka.brokers')
);
Expand All @@ -23,10 +30,23 @@ public function publish(string $broker = null): MessageProducer
/** Return a ConsumerBuilder instance. */
public function createConsumer(array $topics = [], string $groupId = null, string $brokers = null): ConsumerBuilder
{
if ($this->shouldFake) {
return 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;
}
}
1 change: 0 additions & 1 deletion src/Support/Testing/Fakes/ProducerFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class ProducerFake implements Producer
{
use ManagesTransactions;

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 @@ -346,4 +346,23 @@ public function testMacro(): void
$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'])
->onTopic('topic')
->withKey($uuid = Str::uuid()->toString());

Kafka::macro('testProducer', function () use ($expectedMessage) {
return $this->publish()->withMessage($expectedMessage);
});

Kafka::fake();
Kafka::testProducer()->send();

Kafka::assertPublished($expectedMessage);
}
}

0 comments on commit 32dc3d0

Please sign in to comment.