Skip to content

PHPUnit silently ignores the return value on a void method #3602

Closed
@morozov

Description

@morozov
Q A
PHPUnit version 8.1.0
PHP version 7.3.4
Installation Method Composer

Consider the following test:

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class Doer
{
    public function doStuff() : void
    {
    }
}

class PHPUnitTest extends TestCase
{
    public function test() : void
    {
        /** @var Doer|MockObject $doer */
        $doer = $this->createMock(Doer::class);
        $doer->expects($this->any())
            ->method('doStuff')
            ->willReturn(true);

        $this->assertNull($doer->doStuff());
    }
}

The test passes:

PHPUnit 8.1.0 by Sebastian Bergmann and contributors.

Testing PHPUnitTest


Time: 69 ms, Memory: 8.00 MB

OK (1 test, 1 assertion)

The test shouldn't pass since the willReturn(true) behavior cannot be achieved.

Fixing this issue could help keep existing tests up to date when the return value of a method is changed from something to void.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions