Anonymous classes are not rejected with a clear error when creating a test double · Issue #6601 · sebastianbergmann/phpunit · GitHub
Skip to content

Anonymous classes are not rejected with a clear error when creating a test double #6601

Closed

Description

@LastDragon-ru
Q A
PHPUnit version 13.1.5, 13.1.7
PHP version 8.5.3
Installation Method Composer

Summary

The ParseError will be thrown when mocking anonymous class.

Current behavior

ParseError: syntax error, unexpected token "@", expecting "{"

How to reproduce

<?php declare(strict_types = 1);

use PHPUnit\Framework\TestCase;

final class AnonymousMockTest extends TestCase {
    public function testMock(): void {
        $object = new class () implements Customizable {
            #[Override]
            public function customize(?stdClass $custom = null): void {
                // empty
            }
        };

        self::createMock($object::class);
    }
}

interface Customizable {
    public function customize(): void;
}

Expected behavior

No error.

Activity

  1. 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
  2. sebastianbergmann commented on Apr 29, 2026

    @sebastianbergmann
    Owner

    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 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.

  3. added a commit that references this issue on Apr 29, 2026
    489abdd
  4. added 2 commits that reference this issue on Apr 29, 2026
    cccdd46
    716c5f2
  5. locked as resolved and limited conversation to collaborators on May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions