Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Mocking of Interface method with type hint causes fatal error #319

Closed
@sawmurai

Description

@sawmurai

I need to create a mock of a class that implements two interfaces. This is what I do:

$contextMock = $this->createMock([QueryContextInterface::class, RepresentAggsAsFacets::class]);
        $contextMock
            ->expects($this->once())
            ->method('getAggregationKeys')
            ->willReturn(['category_ids']);

QueryContextInterface:

interface QueryContextInterface
{

    /**
     * Get the query tree builder.
     *
     * @return TreeBuilder
     */
    public function getTreeBuilder();
}

RepresentAggsAsFacets

interface RepresentAggsAsFacets
{

    /**
     * @return string[]
     */
    public function getAggregationKeys(): array;
}

Now, when I execute the test, I get the following error message:

PHP Fatal error: Declaration of Mock_RepresentAggsAsFacets_2fec4bca::getAggregationKeys() must be compatible with Jobcloud\PlatformBundle\Elasticsearch\RepresentAggsAsFacets::getAggregationKeys(): array in /srv/httpd/unity-backend-core/PlatformBundle/vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Generator.php(340) : eval()'d code on line 11

The error does not show if I remove the typehint of RepresentAggsAsFacets::getAggregationKeys.

PHP-Version: 7.0.9
PHPUnit: 5.5
phpunit-mock-objects: 3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions