arnaud-lb / php-rdkafka

Production-ready, stable Kafka client for PHP
MIT License
2.08k stars 263 forks source link

TypeError KafkaConsumer::getMetadata #521

Closed maxm86545 closed 2 years ago

maxm86545 commented 2 years ago

Description

The following code:

<?php

class Example {
    public function __construct(private \RdKafka\KafkaConsumer $kafkaConsumer) {}

    public function getMetadata(): \RdKafka\Metadata
    {
        return $this->kafkaConsumer->getMetadata(true, null, 5000);
    }
}

$kafkaConsumer = new \RdKafka\KafkaConsumer(/** ... **/)
$example = new Example($kafkaConsumer);
$metadata = $example->getMetadata(); // <- Works successfully

$class = new \ReflectionClass($kafkaConsumer);
$method = $class->getMethod('getMetadata');
$onlyTopicParameter = $method->getParameters()[1];
$onlyTopicParameter->allowsNull(); // = false

PHPUnit 9.5.20:

<?php

// ...

    public function testGetMetadata(): void
    {
        $kafkaMetadataMock = $this->createMock(\RdKafka\Metadata::class);

        $kafkaConsumerMock = $this->createMock(\RdKafka\KafkaConsumer::class);
        $kafkaConsumerMock
            ->expects($this->once())
            ->method('getMetadata')
            ->with(
                $this->isTrue(),
                $this->isNull(),
                $this->identicalTo(5000)
            )
            ->willReturn($kafkaMetadataMock);

        $class = new \ReflectionClass($kafkaConsumerMock);
        $method = $class->getMethod('getMetadata');
        $onlyTopicParameter = $method->getParameters()[1];
        $onlyTopicParameter->allowsNull(); // = false

        $example = new \Example($kafkaConsumerMock);

        $metadata = $example->getMetadata(); // <- TypeError

        $this->assertSame($kafkaMetadataMock, $metadata);
    }

// ...

Resulted in this output:

TypeError: Mock_KafkaConsumer_edc85dc5::getMetadata(): Argument #2 ($only_topic) must be of type RdKafka\Topic, null given

But I expected this output instead:

Test succeeds

Null allowed: https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/rdkafka-kafkaconsumer.getmetadata.html

RdKafka\KafkaConsumerTopic|null $only_topic

php-rdkafka Version

6.0.1

librdkafka Version

1.8.2

PHP Version

8.1.6 and 8.0.19

Operating System

Official php alpine docker container

Kafka Version

2.8

arnaud-lb commented 2 years ago

Thank you! This should be fixed now, I will push a release soon