Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Allow ad hoc mock method implementations to specify whether the function signature from an overridden method should be used when generating the mock class #267

Open
ezzatron opened this issue Feb 28, 2023 · 1 comment

Comments

@ezzatron
Copy link
Contributor

ezzatron commented Feb 28, 2023

See #253 for context.

Given some type with a method:

interface SomeType
{
    public function someMethod(string $arg): string|int;
}

A "standard" ad hoc mock like the following one would result in a mock class with someMethod having the signature (string|int): string (matching the signature of the ad hoc implementation):

partialMock([
    SomeType::class,
    [
        'someMethod' => function (string|int $arg): string {
        },
    ]
]);

Whereas a special token like {useOriginalSignature} would change the behaviour and result in a mock class with someMethod having the signature (string): string|int (matching the signature of the original method):

partialMock([
    SomeType::class,
    [
        'someMethod {useOriginalSignature}' => function (string|int $arg): string {
        },
    ]
]);

Alternatively, perhaps the behaviour could be flipped, defaulting to using the overridden method's signature, and a keyword like override (inspired by languages that require explicit override declaration) could indicate that the signature of the ad hoc implementation should be used. This would be a BC break, however.

@ezzatron
Copy link
Contributor Author

Actually, it occurs to me now that any time you want this behaviour, all you need to do is not provide an override in the initial mock definition, and then change the behaviour after definition with methods like ->does(). Perhaps a better solution is just to warn against overriding methods with ad hoc definitions, or even forbid it outright?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant