Skip to content

Methods with "static" return type (introduced in PHP 8) are not handled correctly by test double code generator #4480

@davidbyoung

Description

@davidbyoung
Q A
PHPUnit version 9.4.0
PHP version 8.0.0rc1
Installation Method Composer

Summary

The static return type isn't fully supported in mocks in PHP 8.0.

Current behavior

I get the following error when running a test:

Method getInstance may not return value of type Mock_IFoo_54e9358a, its return declaration is "static"

How to reproduce

interface IFoo
{
    public function getInstance(): static;
}

class FooTest extends TestCase
{
    public function testFoo(): void
    {
        $mockFoo = $this->createMock(Foo::class);
        $mockFoo->method('getInstance')
            ->willReturn($mockFoo);
        $this->assertSame($mockFoo, $mockFoo->getInstance());
    }
}

Expected behavior

The above test should pass.

Additional notes

On a related note, although I cannot create a simple repro for the following error, here's a link to the broken unit test and output for a possibly-related issue with supporting static return types. When dumping the eval'd code here, I noticed that some of my tests are resulting in code like the following being generated:

class static
{
}

class Mock_static_5832468d extends static implements PHPUnit\Framework\MockObject\MockObject
{
    use \PHPUnit\Framework\MockObject\Api;
    use \PHPUnit\Framework\MockObject\Method;
    use \PHPUnit\Framework\MockObject\MockedCloneMethod;
}

It looks like static is being treated like the actual name of a return type rather than a special type.

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions