Skip to content

PHPUnit 11 final changes result in inability to disable return value generation #5688

@ben-challis

Description

@ben-challis

For our uses of PHPUnit, we want to disable return value generation always. We want all mocked methods to need to be explicitly configured, rather than false-y values stubbed in if omitted. We find this approach is safer when code is refactored and mocked methods being called may change. Enforcing this has found bugs in our test suite(s).

We achieve this via https://github.com/Lendable/phpunit-extensions/blob/main/src/StrictMocking.php#L27 and a PHPStan rule to ensure either this trait is used or an abstract test case is extended from which uses this trait.

In PHPUnit 11 TestCase::createMock has been made final, which entirely breaks this. I realise the approach of overriding the method as such is a bit of a cludge as it requires the implementation to stay in sync with the upstream implementation, however, there is no ability currently to hook into the creation of the mock via MockGenerator, as all the code is just inline. Therefore, limited options to achieve this 😅

Can you consider having a hook point for creating the "default" double?

I.e. enable configurability to the options passed here:

        $mock = (new MockGenerator)->testDouble(
            $originalClassName,
            true,
            callOriginalConstructor: false,
            callOriginalClone: false,
            cloneArguments: false,
            allowMockingUnknownTypes: false,
        );

Or simply a means to opt-out of return value auto-generation at the TestCase level?

        $mock = (new MockGenerator)->testDouble(
            $originalClassName,
            true,
            callOriginalConstructor: false,
            callOriginalClone: false,
            cloneArguments: false,
            allowMockingUnknownTypes: false,
            returnValueGeneration: $this->returnValueGenerationEnabled(),
        );

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/enhancementA new idea that should be implemented

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions