| Q | A
| --------------------| ---------------
| PHPUnit version | 9.4.0
| PHP version | 8.0.0rc1
| Installation Method | Composer
The static return type isn't fully supported in mocks in PHP 8.0.
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"
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());
}
}
The above test should pass.
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.
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.