10
10
use PHPUnit \Framework \TestCase ;
11
11
use RdKafka \KafkaConsumer ;
12
12
use RdKafka \Message ;
13
+ use RdKafka \TopicPartition ;
14
+ use RdKafka \Exception as RdKafkaException ;
13
15
14
16
class RdKafkaConsumerTest extends TestCase
15
17
{
@@ -258,6 +260,62 @@ public function testShouldAllowGetPreviouslySetSerializer()
258
260
$ this ->assertSame ($ expectedSerializer , $ consumer ->getSerializer ());
259
261
}
260
262
263
+ public function testShouldGetAssignmentWhenThereAreNoPartitions (): void
264
+ {
265
+ $ rdKafka = $ this ->createKafkaConsumerMock ();
266
+ $ rdKafka ->expects ($ this ->once ())
267
+ ->method ('getAssignment ' )
268
+ ->willReturn ([]);
269
+
270
+ $ consumer = new RdKafkaConsumer (
271
+ $ rdKafka ,
272
+ $ this ->createContextMock (),
273
+ new RdKafkaTopic ('' ),
274
+ $ this ->createSerializerMock ()
275
+ );
276
+
277
+ $ this ->assertEquals ([], $ consumer ->getAssignment ());
278
+ }
279
+
280
+ public function testShouldGetAssignmentWhenThereArePartitions (): void
281
+ {
282
+ $ partition = new TopicPartition ('' , 0 );
283
+
284
+ $ rdKafka = $ this ->createKafkaConsumerMock ();
285
+ $ rdKafka ->expects ($ this ->once ())
286
+ ->method ('getAssignment ' )
287
+ ->willReturn ([$ partition ]);
288
+
289
+ $ consumer = new RdKafkaConsumer (
290
+ $ rdKafka ,
291
+ $ this ->createContextMock (),
292
+ new RdKafkaTopic ('' ),
293
+ $ this ->createSerializerMock ()
294
+ );
295
+
296
+ $ expected = new RdKafkaTopic ('' );
297
+ $ expected ->setPartition (0 );
298
+
299
+ $ this ->assertEquals ([$ expected ], $ consumer ->getAssignment ());
300
+ }
301
+
302
+ public function testShouldGetAssignmentReturnEmptyArrayWhenThrowException (): void
303
+ {
304
+ $ rdKafka = $ this ->createKafkaConsumerMock ();
305
+ $ rdKafka ->expects ($ this ->once ())
306
+ ->method ('getAssignment ' )
307
+ ->willThrowException ($ this ->createExceptionMock ());
308
+
309
+ $ consumer = new RdKafkaConsumer (
310
+ $ rdKafka ,
311
+ $ this ->createContextMock (),
312
+ new RdKafkaTopic ('' ),
313
+ $ this ->createSerializerMock ()
314
+ );
315
+
316
+ $ this ->assertEquals ([], $ consumer ->getAssignment ());
317
+ }
318
+
261
319
/**
262
320
* @return \PHPUnit\Framework\MockObject\MockObject|KafkaConsumer
263
321
*/
@@ -281,4 +339,12 @@ private function createSerializerMock()
281
339
{
282
340
return $ this ->createMock (Serializer::class);
283
341
}
342
+
343
+ /**
344
+ * @return \PHPUnit\Framework\MockObject\MockObject|RdKafkaException
345
+ */
346
+ private function createExceptionMock ()
347
+ {
348
+ return $ this ->createMock (RdKafkaException::class);
349
+ }
284
350
}
0 commit comments