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

Created on 5 Oct 2020  路  2Comments  路  Source: sebastianbergmann/phpunit

| 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.

featurtest-doubles typbug

All 2 comments

Here is a corrected and cleaned up version of the example:

<?php
use PHPUnit\Framework\TestCase;

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

class FooTest extends TestCase
{
    public function testFoo(): void
    {
        $foo = $this->createMock(Foo::class);

        $foo->method('getInstance')
            ->willReturn($foo);

        $this->assertSame($foo, $foo->getInstance());
    }
}

I am happy to report that I am already able to run it:

PHPUnit 9.4.0-1-g7646ea0ce by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 00:00.024, Memory: 6.00 MB

OK (1 test, 1 assertion)

I hope to have these changes ready for commit/push soon.

This requires the fix for PHP bug 80190.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastianbergmann picture sebastianbergmann  路  4Comments

keradus picture keradus  路  4Comments

keradus picture keradus  路  3Comments

dciancu picture dciancu  路  3Comments

rentalhost picture rentalhost  路  4Comments