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

Deprecate support for doubling interfaces (or classes) that have a method named method #5415

Closed
sebastianbergmann opened this issue Jun 20, 2023 · 0 comments
Assignees
Labels
feature/test-doubles Test Stubs and Mock Objects type/deprecation Something will be/is deprecated
Milestone

Comments

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Jun 20, 2023

Right now, the Stub interface looks like this:

/**
 * @method InvocationStubber method($constraint)
 *
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
 */
interface Stub
{
}

The test double code generator checks whether the interface(s) (or class) to be doubled have a method named method.

If there is no such method then the generated class will have a method named method that allows the configuration of methods on the test double object:

$stub = $this->createStub(AnInterface::class);

$stub->method('doSomething')->willReturn('value');

If the interface(s) (or class) to be doubled have a method named method then the test double code generator will leave that method as-is. In this case, the example shown above has to be written like so:

$stub = $this->createStub(AnInterface::class);

$stub->expects($this->any())->method('doSomething')->willReturn('value');

This implementation, which only exists because of an unlikely edge case (when do you call a method method?), results in unnecessary complexity which I would like to get rid of. Furthermore, the expects() method should only exist on mock objects (e.g. created with createMock()), but not on test stubs (e.g. created with createStub()).

Once the support for doubling interfaces (or classes) that have a method named method has been removed, the Stub interface can be changed to look like so:

/**
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
 */
interface Stub
{
    public function method($constraint): InvocationStubber;
}

At that point in time we can also change createStub() et. al. to return an object that does not have expects().

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/deprecation Something will be/is deprecated
Projects
None yet
Development

No branches or pull requests

1 participant