You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Anonymous classes are not rejected with a clear error when creating a test double #6601
changed the title [-]ParseError: syntax error, unexpected token "@", expecting "{" when mocking anonymous class[/-][+]Anonymous classes are not rejected with a clear error when creating a test double[/+]on Apr 29, 2026
Creating a test double for an anonymous class is not supported, and it never has been. PHPUnit's test double generator works by assembling a PHP source string of the form class MockObject_… extends <type> implements … and evaluating it via eval(). The runtime "name" of an anonymous class, for example Customizable@anonymous /path/to/file.php:11$27b, contains characters (@, space, /, ., :, $) that are not valid in a PHP class identifier and therefore cannot appear after extends. That is why you see the ParseError leaking out of eval().
Support for doubling anonymous classes will not be added. By design, an anonymous class is not referenceable by name outside the expression that creates it, so a generated double cannot meaningfully extend it, and an instance of such a double could not satisfy instanceof against the original anonymous type in any portable way. If you need a test double that matches the same set of interfaces the anonymous class implements, please use createMockForIntersectionOfInterfaces([...]) (or mock the individual interface, in your example Customizable).
What can and should be improved is the error message. PHPUnit should detect this case up front and raise a descriptive exception instead of letting a ParseError from eval() surface to the user. I will address that.
Activity
[-]ParseError: syntax error, unexpected token "@", expecting "{" when mocking anonymous class[/-][+]Anonymous classes are not rejected with a clear error when creating a test double[/+]sebastianbergmann commentedon Apr 29, 2026
Thank you for the report.
Creating a test double for an anonymous class is not supported, and it never has been. PHPUnit's test double generator works by assembling a PHP source string of the form
class MockObject_… extends <type> implements …and evaluating it viaeval(). The runtime "name" of an anonymous class, for exampleCustomizable@anonymous /path/to/file.php:11$27b, contains characters (@, space,/,.,:,$) that are not valid in a PHP class identifier and therefore cannot appear afterextends. That is why you see theParseErrorleaking out ofeval().Support for doubling anonymous classes will not be added. By design, an anonymous class is not referenceable by name outside the expression that creates it, so a generated double cannot meaningfully extend it, and an instance of such a double could not satisfy
instanceofagainst the original anonymous type in any portable way. If you need a test double that matches the same set of interfaces the anonymous class implements, please usecreateMockForIntersectionOfInterfaces([...])(or mock the individual interface, in your exampleCustomizable).What can and should be improved is the error message. PHPUnit should detect this case up front and raise a descriptive exception instead of letting a
ParseErrorfromeval()surface to the user. I will address that.Merge branch '13.1'
Closes sebastianbergmann#6601
Merge branch '12.5' into 13.1