Skip to content

Test Double code generator does not handle new expressions inside parameter default values #4929

@maxm86545

Description

@maxm86545
Q A
PHPUnit version 9.5.18
PHP version 8.1.2
Installation Method Composer

Summary

createMock is not working with new in initializers

Current behavior

Tests fail with an incomprehensible error:

PHP Fatal error: Constant expression contains invalid operations in /var/www/html/app/vendor/phpunit/phpunit/src/Framework/MockObject/MockClass.php(51) : eval()'d code on line 9

How to reproduce

class MyClass {
    public static function create(Example $example = new Example()) {
    // ...
    }
}
    // ...

    public function testSome(): void
    {
        $this->createMock(MyClass::class); // <--- PHP Fatal error
    }

    / ...

Expected behavior

Mock is created successfully.

Metadata

Metadata

Labels

feature/test-doublesTest Stubs and Mock Objectstype/bugSomething is brokentype/change-in-php-requires-adaptationA change in PHP requires a change so that existing PHPUnit functionality continues to work

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions