Skip to content
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 opened this issue Aug 16, 2016 · 2 comments
Closed

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

sawmurai opened this issue Aug 16, 2016 · 2 comments
Labels

Comments

@sawmurai
Copy link
Contributor

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

@sebastianbergmann
Copy link
Owner

<?php
interface A
{
    public function foo(): A;
}

interface B
{
}

class Test extends PHPUnit\Framework\TestCase
{
    public function testOne()
    {
        $this->createMock([A::class, B::class]);
    }
}

@sawmurai
Copy link
Contributor Author

I created a PR to fix this:

#320

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants