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

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

Closed
maxm86545 opened this issue Mar 12, 2022 · 1 comment
Assignees
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken type/change-in-php-requires-adaptation A change in PHP requires a change so that existing PHPUnit functionality continues to work

Comments

@maxm86545
Copy link

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.

@maxm86545 maxm86545 added the type/bug Something is broken label Mar 12, 2022
@sebastianbergmann sebastianbergmann added feature/test-doubles Test Stubs and Mock Objects type/change-in-php-requires-adaptation A change in PHP requires a change so that existing PHPUnit functionality continues to work labels Mar 12, 2022
@sebastianbergmann sebastianbergmann changed the title createMock: PHP Fatal error with new in initializers Test Doubles code generator does not handle new expressions inside parameter default values Mar 12, 2022
@sebastianbergmann sebastianbergmann self-assigned this Mar 12, 2022
@sebastianbergmann sebastianbergmann changed the title Test Doubles code generator does not handle new expressions inside parameter default values Test Double code generator does not handle new expressions inside parameter default values Mar 12, 2022
@sebastianbergmann
Copy link
Owner

@nikic Am I missing something or are string operations on the return value of ReflectionParameter::__toString() really the only way to access the information we need here through the Reflection API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken type/change-in-php-requires-adaptation A change in PHP requires a change so that existing PHPUnit functionality continues to work
Projects
None yet
Development

No branches or pull requests

2 participants