Skip to content

Commit f68dbc4

Browse files
authored
Up infection (#31)
* up msi * up msi
1 parent b78f86e commit f68dbc4

File tree

7 files changed

+162
-12
lines changed

7 files changed

+162
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ install-dependencies-lowest:
3737
infection-testing:
3838
make coverage
3939
cp -f build/logs/phpunit/junit.xml build/logs/phpunit/coverage/junit.xml
40-
${INFECTION} --coverage=build/logs/phpunit/coverage --min-msi=76 --threads=`nproc`
40+
${INFECTION} --coverage=build/logs/phpunit/coverage --min-msi=91 --threads=`nproc`
4141

4242
xdebug-enable:
4343
sudo php-ext-enable xdebug

tests/Unit/Conf/KafkaConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function testConfigValues($inputValue, $expectedValue): void
107107
return;
108108
}
109109

110+
self::assertEquals($config['metadata.broker.list'], 'localhost');
110111
self::assertEquals($expectedValue, $config['group.id']);
111112
self::assertEquals('100', $config['auto.commit.interval.ms']);
112113
self::assertArrayHasKey('default_topic_conf', $config);

tests/Unit/Consumer/KafkaConsumerBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function testAddConfig(): void
116116
],
117117
$reflectionProperty->getValue($clone)
118118
);
119+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
119120
}
120121

121122
/**
@@ -132,6 +133,7 @@ public function testSetDecoder(): void
132133
$reflectionProperty->setAccessible(true);
133134

134135
self::assertInstanceOf(DecoderInterface::class, $reflectionProperty->getValue($clone));
136+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
135137
}
136138

137139
/**
@@ -146,6 +148,7 @@ public function testSetConsumerGroup(): void
146148
$reflectionProperty->setAccessible(true);
147149

148150
self::assertSame('test-consumer', $reflectionProperty->getValue($clone));
151+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
149152
}
150153

151154
/**
@@ -160,6 +163,7 @@ public function testSetConsumerTypeLow(): void
160163
$actualConsumerType->setAccessible(true);
161164

162165
self::assertSame(KafkaConsumerBuilder::CONSUMER_TYPE_LOW_LEVEL, $actualConsumerType->getValue($clone));
166+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
163167
}
164168

165169
/**
@@ -174,6 +178,7 @@ public function testSetConsumerTypeHigh(): void
174178
$actualConsumerType->setAccessible(true);
175179

176180
self::assertSame(KafkaConsumerBuilder::CONSUMER_TYPE_HIGH_LEVEL, $actualConsumerType->getValue($clone));
181+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
177182
}
178183

179184
/**
@@ -192,6 +197,7 @@ public function testSetErrorCallback(): void
192197
$reflectionProperty->setAccessible(true);
193198

194199
self::assertSame($callback, $reflectionProperty->getValue($clone));
200+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
195201
}
196202

197203
/**
@@ -210,6 +216,7 @@ public function testSetRebalanceCallback(): void
210216
$reflectionProperty->setAccessible(true);
211217

212218
self::assertSame($callback, $reflectionProperty->getValue($clone));
219+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
213220
}
214221

215222
/**
@@ -228,6 +235,7 @@ public function testSetConsumeCallback(): void
228235
$reflectionProperty->setAccessible(true);
229236

230237
self::assertSame($callback, $reflectionProperty->getValue($clone));
238+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
231239
}
232240

233241
/**
@@ -246,6 +254,7 @@ public function testSetOffsetCommitCallback(): void
246254
$reflectionProperty->setAccessible(true);
247255

248256
self::assertSame($callback, $reflectionProperty->getValue($clone));
257+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
249258
}
250259

251260
/**
@@ -264,6 +273,7 @@ public function testSetLogCallback(): void
264273
$reflectionProperty->setAccessible(true);
265274

266275
self::assertSame($callback, $reflectionProperty->getValue($clone));
276+
self::assertNotSame($clone, $this->kafkaConsumerBuilder);
267277
}
268278

269279
/**
@@ -273,6 +283,7 @@ public function testSetLogCallback(): void
273283
public function testBuildFailMissingBrokers(): void
274284
{
275285
self::expectException(KafkaConsumerBuilderException::class);
286+
self::expectExceptionMessage(KafkaConsumerBuilderException::NO_BROKER_EXCEPTION_MESSAGE);
276287

277288
$this->kafkaConsumerBuilder->build();
278289
}
@@ -284,6 +295,7 @@ public function testBuildFailMissingBrokers(): void
284295
public function testBuildFailMissingTopics(): void
285296
{
286297
self::expectException(KafkaConsumerBuilderException::class);
298+
self::expectExceptionMessage(KafkaConsumerBuilderException::NO_TOPICS_EXCEPTION_MESSAGE);
287299

288300
$this->kafkaConsumerBuilder->withAdditionalBroker('localhost')->build();
289301
}

tests/Unit/Consumer/KafkaHighLevelConsumerTest.php

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use RdKafka\Metadata\Collection as RdKafkaMetadataCollection;
2121
use RdKafka\Metadata\Partition as RdKafkaMetadataPartition;
2222
use RdKafka\Metadata\Topic as RdKafkaMetadataTopic;
23+
use RdKafka\TopicPartition as RdKafkaTopicPartition;
2324

2425
/**
2526
* @covers \Jobcloud\Kafka\Consumer\AbstractKafkaConsumer
@@ -107,7 +108,14 @@ function () use ($rdKafkaMetadataTopicMock) {
107108
$decoderMock = $this->getMockForAbstractClass(DecoderInterface::class);
108109
$kafkaConsumer = new KafkaHighLevelConsumer($rdKafkaConsumerMock, $kafkaConfigurationMock, $decoderMock);
109110

110-
$rdKafkaConsumerMock->expects(self::once())->method('assign');
111+
$rdKafkaConsumerMock->expects(self::once())->method('assign')->with(
112+
$this->callback(
113+
function (array $assignment) {
114+
self::assertCount(2, $assignment);
115+
return true;
116+
}
117+
)
118+
);
111119
$rdKafkaConsumerMock
112120
->expects(self::once())
113121
->method('getMetadata')
@@ -216,24 +224,73 @@ public function testUnsubscribeFailure(): void
216224
* @throws KafkaConsumerCommitException
217225
*/
218226
public function testCommitSuccesss(): void
227+
{
228+
$message = $this->getMockForAbstractClass(KafkaConsumerMessageInterface::class);
229+
$message->expects(self::exactly(1))->method('getOffset')->willReturn(0);
230+
$message->expects(self::exactly(1))->method('getTopicName')->willReturn('test');
231+
$message->expects(self::exactly(1))->method('getPartition')->willReturn(1);
232+
$message2 = $this->getMockForAbstractClass(KafkaConsumerMessageInterface::class);
233+
$message2->expects(self::exactly(1))->method('getOffset')->willReturn(1);
234+
$message2->expects(self::exactly(2))->method('getTopicName')->willReturn('test');
235+
$message2->expects(self::exactly(2))->method('getPartition')->willReturn(1);
236+
$message3 = $this->getMockForAbstractClass(KafkaConsumerMessageInterface::class);
237+
$message3->expects(self::exactly(2))->method('getOffset')->willReturn(2);
238+
$message3->expects(self::exactly(1))->method('getTopicName')->willReturn('test');
239+
$message3->expects(self::exactly(1))->method('getPartition')->willReturn(1);
240+
$message4 = $this->getMockForAbstractClass(KafkaConsumerMessageInterface::class);
241+
$message4->expects(self::exactly(1))->method('getOffset')->willReturn(0);
242+
$message4->expects(self::exactly(2))->method('getTopicName')->willReturn('test');
243+
$message4->expects(self::exactly(2))->method('getPartition')->willReturn(2);
244+
245+
246+
$rdKafkaConsumerMock = $this->createMock(RdKafkaHighLevelConsumer::class);
247+
$kafkaConfigurationMock = $this->createMock(KafkaConfiguration::class);
248+
$decoderMock = $this->getMockForAbstractClass(DecoderInterface::class);
249+
$kafkaConsumer = new KafkaHighLevelConsumer($rdKafkaConsumerMock, $kafkaConfigurationMock, $decoderMock);
250+
$rdKafkaConsumerMock->expects(self::once())->method('commit')->with(
251+
$this->callback(
252+
function (array $topicPartitions) {
253+
self::assertCount(2, $topicPartitions);
254+
self::assertInstanceOf(RdKafkaTopicPartition::class, $topicPartitions['test-1']);
255+
self::assertInstanceOf(RdKafkaTopicPartition::class, $topicPartitions['test-2']);
256+
self::assertEquals(3, $topicPartitions['test-1']->getOffset());
257+
self::assertEquals(1, $topicPartitions['test-2']->getOffset());
258+
259+
return true;
260+
}
261+
)
262+
);
263+
264+
$kafkaConsumer->commit([$message2, $message, $message3, $message4]);
265+
}
266+
267+
/**
268+
* @throws KafkaConsumerCommitException
269+
*/
270+
public function testCommitSingleSuccesss(): void
219271
{
220272
$message = $this->getMockForAbstractClass(KafkaConsumerMessageInterface::class);
221273
$message->expects(self::exactly(1))->method('getOffset')->willReturn(0);
222274
$message->expects(self::exactly(2))->method('getTopicName')->willReturn('test');
223275
$message->expects(self::exactly(2))->method('getPartition')->willReturn(1);
224-
$message2 = $this->getMockForAbstractClass(KafkaConsumerMessageInterface::class);
225-
$message2->expects(self::exactly(2))->method('getOffset')->willReturn(1);
226-
$message2->expects(self::exactly(1))->method('getTopicName')->willReturn('test');
227-
$message2->expects(self::exactly(1))->method('getPartition')->willReturn(1);
228276

229277

230278
$rdKafkaConsumerMock = $this->createMock(RdKafkaHighLevelConsumer::class);
231279
$kafkaConfigurationMock = $this->createMock(KafkaConfiguration::class);
232280
$decoderMock = $this->getMockForAbstractClass(DecoderInterface::class);
233281
$kafkaConsumer = new KafkaHighLevelConsumer($rdKafkaConsumerMock, $kafkaConfigurationMock, $decoderMock);
234-
$rdKafkaConsumerMock->expects(self::once())->method('commit');
282+
$rdKafkaConsumerMock->expects(self::once())->method('commit')->with(
283+
$this->callback(
284+
function (array $topicPartitions) {
285+
self::assertCount(1, $topicPartitions);
286+
self::assertInstanceOf(RdKafkaTopicPartition::class, $topicPartitions['test-1']);
287+
self::assertEquals(1, $topicPartitions['test-1']->getOffset());
288+
return true;
289+
}
290+
)
291+
);
235292

236-
$kafkaConsumer->commit([$message, $message2]);
293+
$kafkaConsumer->commit($message);
237294
}
238295

239296
/**
@@ -367,9 +424,10 @@ public function testKafkaConsumeWithDecode(): void
367424
$message->key = 'test';
368425
$message->payload = null;
369426
$message->topic_name = 'test_topic';
370-
$message->partition = 9;
371-
$message->offset = 501;
372-
$message->timestamp = 500;
427+
$message->partition = '9';
428+
$message->offset = '501';
429+
$message->timestamp = '500';
430+
$message->headers = 'header';
373431
$message->err = RD_KAFKA_RESP_ERR_NO_ERROR;
374432

375433
$topics = [new TopicSubscription('testTopic')];
@@ -395,6 +453,8 @@ function (KafkaConsumerMessageInterface $message) {
395453
self::assertEquals(9, $message->getPartition());
396454
self::assertEquals(501, $message->getOffset());
397455
self::assertEquals(500, $message->getTimestamp());
456+
self::assertEquals(['header'], $message->getHeaders());
457+
398458
return true;
399459
}
400460
)

tests/Unit/Exception/KafkaConsumerConsumeExceptionTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,21 @@ public function testGetAndConstructOfKafkaConsumerConsumeException()
2525

2626
public function testGetAndConstructOfKafkaConsumerConsumeExceptionWithNullAsMessage()
2727
{
28-
$exception = new KafkaConsumerConsumeException('', 0, null);
28+
$exception = new KafkaConsumerConsumeException('test', 100, null);
2929

3030
self::assertNull($exception->getKafkaMessage());
31+
self::assertEquals('test', $exception->getMessage());
32+
self::assertEquals(100, $exception->getCode());
33+
}
34+
35+
public function testGetDefaults()
36+
{
37+
$exception = new KafkaConsumerConsumeException();
38+
39+
self::assertNull($exception->getKafkaMessage());
40+
self::assertEquals('', $exception->getMessage());
41+
self::assertEquals(0, $exception->getCode());
42+
self::assertNull($exception->getPrevious());
43+
3144
}
3245
}

tests/Unit/Message/KafkaProducerMessageTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,28 @@ public function testMessageGettersAndConstructor()
3737
self::assertEquals($partition, $message->getPartition());
3838
self::assertEquals($expectedHeader, $message->getHeaders());
3939
}
40+
41+
public function testClone()
42+
{
43+
$key = '1234-1234-1234';
44+
$body = 'foo bar baz';
45+
$topic = 'test';
46+
$partition = 1;
47+
$headers = [ 'key' => 'value' ];
48+
49+
50+
$origMessage = KafkaProducerMessage::create($topic, $partition);
51+
52+
$message = $origMessage->withKey($key);
53+
self::assertNotSame($origMessage, $message);
54+
55+
$message = $origMessage->withBody($body);
56+
self::assertNotSame($origMessage, $message);
57+
58+
$message = $origMessage->withHeaders($headers);
59+
self::assertNotSame($origMessage, $message);
60+
61+
$message = $origMessage->withHeader('anotherKey', 1);
62+
self::assertNotSame($origMessage, $message);
63+
}
4064
}

tests/Unit/Producer/KafkaProducerTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ public function testPoll()
172172
$this->kafkaProducer->poll(1000);
173173
}
174174

175+
public function testPollDefault()
176+
{
177+
$this->rdKafkaProducerMock
178+
->expects(self::once())
179+
->method('poll')
180+
->with(0);
181+
182+
$this->kafkaProducer->poll();
183+
}
184+
175185
public function testPollUntilQueueSizeReached()
176186
{
177187
$message = KafkaProducerMessage::create('test-topic', 1)
@@ -283,4 +293,34 @@ public function testGetMetadataForTopic(): void
283293
->willReturn($metadataMock);
284294
$this->kafkaProducer->getMetadataForTopic('test-topic-name', 1000);
285295
}
296+
297+
/**
298+
* @return void
299+
*/
300+
public function testGetMetadataForTopicDefault(): void
301+
{
302+
$topicMock = $this->createMock(RdKafkaProducerTopic::class);
303+
$metadataMock = $this->createMock(RdKafkaMetadata::class);
304+
$metadataCollectionMock = $this->createMock(RdKafkaMetadataCollection::class);
305+
$metadataTopic = $this->createMock(RdKafkaMetadataTopic::class);
306+
$metadataMock
307+
->expects(self::once())
308+
->method('getTopics')
309+
->willReturn($metadataCollectionMock);
310+
$metadataCollectionMock
311+
->expects(self::once())
312+
->method('current')
313+
->willReturn($metadataTopic);
314+
$this->rdKafkaProducerMock
315+
->expects(self::once())
316+
->method('newTopic')
317+
->with('test-topic-name')
318+
->willReturn($topicMock);
319+
$this->rdKafkaProducerMock
320+
->expects(self::once())
321+
->method('getMetadata')
322+
->with(false, $topicMock, 10000)
323+
->willReturn($metadataMock);
324+
$this->kafkaProducer->getMetadataForTopic('test-topic-name');
325+
}
286326
}

0 commit comments

Comments
 (0)