Skip to content

PHPUnit fails to generate an exception mock #3596

@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;

interface DriverException extends Throwable
{
    public function getSQLState() : ?string;
}

class PHPUnitTest extends TestCase
{
    public function test() : void
    {
        $this->createMock(DriverException::class);
    }
}

Running this test produces the following:

Fatal error: Class Mock_Exception_17140bfa contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (DriverException::getSQLState) in vendor/phpunit/phpunit/src/Framework/MockObject/Generator.php(608) : eval()'d code on line 1

The generated code looks like the following:

class Mock_Exception_bc8583eb extends Exception implements DriverException, PHPUnit\Framework\MockObject\MockObject
{
    private $__phpunit_invocationMocker;
    private $__phpunit_originalObject;
    private $__phpunit_configurable = [];
    private $__phpunit_returnValueGeneration = true;


    public function expects(\PHPUnit\Framework\MockObject\Matcher\Invocation $matcher): \PHPUnit\Framework\MockObject\Builder\InvocationMocker
    {
        return $this->__phpunit_getInvocationMocker()->expects($matcher);
    }

    public function method()
    {
        $any     = new \PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount;
        $expects = $this->expects($any);

        return call_user_func_array([$expects, 'method'], func_get_args());
    }

    public function __phpunit_setOriginalObject($originalObject): void
    {
        $this->__phpunit_originalObject = $originalObject;
    }

    public function __phpunit_setReturnValueGeneration(bool $returnValueGeneration): void
    {
        $this->__phpunit_returnValueGeneration = $returnValueGeneration;
    }

    public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker
    {
        if ($this->__phpunit_invocationMocker === null) {
            $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration);
        }

        return $this->__phpunit_invocationMocker;
    }

    public function __phpunit_hasMatchers(): bool
    {
        return $this->__phpunit_getInvocationMocker()->hasMatchers();
    }

    public function __phpunit_verify(bool $unsetInvocationMocker = true): void
    {
        $this->__phpunit_getInvocationMocker()->verify();

        if ($unsetInvocationMocker) {
            $this->__phpunit_invocationMocker = null;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions