Phpunit: Regression in PHPUnit 7.5.10 with regard to Exception stubbing/mocking

Created on 9 May 2019  路  7Comments  路  Source: sebastianbergmann/phpunit

| Q | A
| --------------------| ---------------
| PHPUnit version | 7.5.10
| PHP version | 7.3.5
| Installation Method | Composer

Mocking the driver exception like this worked up to 7.5.9, the fix of the issue #3596 seems to break it:

/** @var DriverException | MockObject $driverException */
$driverException = $this->getMockBuilder(DriverException::class)->getMockForAbstractClass();

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

With 7.5.10 its not working anymore:

Fatal error: Cannot override final method Exception::getMessage() in vendor/phpunit/phpunit/src/Framework/MockObject/Generator.php(642) : eval()'d code on line 1
typbug

Most helpful comment

Thank you all for the information. I should be able to provide a fix within a few days.

All 7 comments

3596

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

I have similar problem with exception mock.

<?php

namespace Tests;

use PHPUnit\Framework\TestCase;

class MockTest extends TestCase
{
    public function test(): void
    {
        $this->createMock(\Throwable::class);
    }
}

Output:

[docker://xxx:5000/xxx/xxx:latest/]:php /opt/project/vendor/phpunit/phpunit/phpunit --configuration /opt/project/phpunit.xml Tests\MockTest /opt/project/tests/MockTest.php --teamcity
PHPUnit 8.1.4 by Sebastian Bergmann and contributors.

PHP Fatal error:  Cannot override final method Exception::getMessage() in /opt/project/vendor/phpunit/phpunit/src/Framework/MockObject/Generator.php(627) : eval()'d code on line 1
PHP Stack trace:
PHP   1. {main}() /opt/project/vendor/phpunit/phpunit/phpunit:0
PHP   2. PHPUnit\TextUI\Command::main() /opt/project/vendor/phpunit/phpunit/phpunit:61
PHP   3. PHPUnit\TextUI\Command->run() /opt/project/vendor/phpunit/phpunit/src/TextUI/Command.php:163
PHP   4. PHPUnit\TextUI\TestRunner->doRun() /opt/project/vendor/phpunit/phpunit/src/TextUI/Command.php:207
PHP   5. PHPUnit\Framework\TestSuite->run() /opt/project/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:613
PHP   6. Tests\MockTest->run() /opt/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:761
PHP   7. PHPUnit\Framework\TestResult->run() /opt/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:808
PHP   8. Tests\MockTest->runBare() /opt/project/vendor/phpunit/phpunit/src/Framework/TestResult.php:685
PHP   9. Tests\MockTest->runTest() /opt/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:854
PHP  10. Tests\MockTest->test() /opt/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:1172
PHP  11. Tests\MockTest->createMock() /opt/project/tests/MockTest.php:11
PHP  12. PHPUnit\Framework\MockObject\MockBuilder->getMock() /opt/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:1335
PHP  13. PHPUnit\Framework\MockObject\Generator->getMock() /opt/project/vendor/phpunit/phpunit/src/Framework/MockObject/MockBuilder.php:117
PHP  14. PHPUnit\Framework\MockObject\Generator->getObject() /opt/project/vendor/phpunit/phpunit/src/Framework/MockObject/Generator.php:196
PHP  15. PHPUnit\Framework\MockObject\Generator->evalClass() /opt/project/vendor/phpunit/phpunit/src/Framework/MockObject/Generator.php:581
PHP  16. eval() /opt/project/vendor/phpunit/phpunit/src/Framework/MockObject/Generator.php:627

CC @morozov

Having the same problem here. It seems to be related to the changes introduced with #3604 when trying to mock the final function getMessage from Exception class.

I can reproduce this with

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

class Test extends TestCase
{
    public function testOne(): void
    {
        $this->createMock(\Throwable::class);
    }
}

Thank you all for the information. I should be able to provide a fix within a few days.

Was this page helpful?
0 / 5 - 0 ratings