Skip to content

Commit 953d971

Browse files
authored
Merge pull request #263 from php-enqueue/rdkafka-topic-default-config
[rdkafka] do not pass config if it was not set explisitly.
2 parents 22df923 + 4488a22 commit 953d971

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

Diff for: pkg/rdkafka/RdKafkaTopic.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class RdKafkaTopic implements PsrTopic, PsrQueue
3434
public function __construct($name)
3535
{
3636
$this->name = $name;
37-
$this->conf = new TopicConf();
3837
}
3938

4039
/**
@@ -54,13 +53,21 @@ public function getQueueName()
5453
}
5554

5655
/**
57-
* @return TopicConf
56+
* @return TopicConf|null
5857
*/
5958
public function getConf()
6059
{
6160
return $this->conf;
6261
}
6362

63+
/**
64+
* @param TopicConf|null $conf
65+
*/
66+
public function setConf(TopicConf $conf = null)
67+
{
68+
$this->conf = $conf;
69+
}
70+
6471
/**
6572
* @return int
6673
*/

Diff for: pkg/rdkafka/Tests/RdKafkaProducerTest.php

+69-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testShouldUseSerializerToEncodeMessageAndPutToExpectedTube()
6464
$kafkaProducer
6565
->expects($this->once())
6666
->method('newTopic')
67-
->with('theQueueName', $this->isInstanceOf(TopicConf::class))
67+
->with('theQueueName')
6868
->willReturn($kafkaTopic)
6969
;
7070

@@ -81,6 +81,74 @@ public function testShouldUseSerializerToEncodeMessageAndPutToExpectedTube()
8181
$producer->send(new RdKafkaTopic('theQueueName'), $message);
8282
}
8383

84+
public function testShouldPassNullAsTopicConfigIfNotSetOnTopic()
85+
{
86+
// guard
87+
$kafkaTopic = $this->createKafkaTopicMock();
88+
$kafkaTopic
89+
->expects($this->once())
90+
->method('produce')
91+
;
92+
93+
$kafkaProducer = $this->createKafkaProducerMock();
94+
$kafkaProducer
95+
->expects($this->once())
96+
->method('newTopic')
97+
->with('theQueueName', null)
98+
->willReturn($kafkaTopic)
99+
;
100+
101+
$serializer = $this->createSerializerMock();
102+
$serializer
103+
->expects($this->once())
104+
->method('toString')
105+
->willReturn('aSerializedMessage')
106+
;
107+
108+
$producer = new RdKafkaProducer($kafkaProducer, $serializer);
109+
110+
$topic = new RdKafkaTopic('theQueueName');
111+
112+
// guard
113+
$this->assertNull($topic->getConf());
114+
115+
$producer->send($topic, new RdKafkaMessage());
116+
}
117+
118+
public function testShouldPassCustomConfAsTopicConfigIfSetOnTopic()
119+
{
120+
$conf = new TopicConf();
121+
122+
// guard
123+
$kafkaTopic = $this->createKafkaTopicMock();
124+
$kafkaTopic
125+
->expects($this->once())
126+
->method('produce')
127+
;
128+
129+
$kafkaProducer = $this->createKafkaProducerMock();
130+
$kafkaProducer
131+
->expects($this->once())
132+
->method('newTopic')
133+
->with('theQueueName', $this->identicalTo($conf))
134+
->willReturn($kafkaTopic)
135+
;
136+
137+
$serializer = $this->createSerializerMock();
138+
$serializer
139+
->expects($this->once())
140+
->method('toString')
141+
->willReturn('aSerializedMessage')
142+
;
143+
144+
$producer = new RdKafkaProducer($kafkaProducer, $serializer);
145+
146+
$topic = new RdKafkaTopic('theQueueName');
147+
$topic->setConf($conf);
148+
149+
$producer->send($topic, new RdKafkaMessage());
150+
}
151+
84152
public function testShouldAllowGetPreviouslySetSerializer()
85153
{
86154
$producer = new RdKafkaProducer($this->createKafkaProducerMock(), $this->createSerializerMock());

Diff for: pkg/rdkafka/Tests/RdKafkaTopicTest.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ public function testCouldSetGetKey()
2727
$this->assertSame('key', $topic->getKey());
2828
}
2929

30-
public function testShouldReturnConfInstance()
30+
public function testShouldReturnNullAsConfIfNotSet()
3131
{
3232
$topic = new RdKafkaTopic('topic');
3333

34-
$this->assertInstanceOf(TopicConf::class, $topic->getConf());
34+
$this->assertNull($topic->getConf());
35+
}
36+
37+
public function testShouldAllowGetPreviouslySetConf()
38+
{
39+
$topic = new RdKafkaTopic('topic');
40+
41+
$conf = new TopicConf();
42+
$topic->setConf($conf);
43+
44+
$this->assertSame($conf, $topic->getConf());
3545
}
3646
}

0 commit comments

Comments
 (0)