Skip to content

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

@sebastianbergmann

Description

@sebastianbergmann

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

Metadata

Metadata

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions