Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPUnit fails to generate an exception mock #3596

Closed
morozov opened this issue Apr 6, 2019 · 2 comments
Closed

PHPUnit fails to generate an exception mock #3596

morozov opened this issue Apr 6, 2019 · 2 comments
Labels
feature/test-doubles Stubs and Mock Objects type/bug Something is broken

Comments

@morozov
Copy link
Contributor

morozov commented Apr 6, 2019

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;
        }
    }
}
@dominikzogg
Copy link

Since the release of this issue we get the following issue:

Fatal error: Cannot override final method Exception::getMessage() in vendor/phpunit/phpunit/src/Framework/MockObject/Generator.php(642) : eval()'d code on line 1
/** @var DriverException | MockObject $driverException */
$driverException = $this->getMockBuilder(DriverException::class)->getMockForAbstractClass();

$connectionException = new ConnectionException('connection lost', $driverException);

@sebastianbergmann
Copy link
Owner

Please open a new ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Stubs and Mock Objects type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

3 participants