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

Missing mock type information for Mockery\Expectation::getMock() #55

Open
larsnystrom opened this issue Jun 10, 2022 · 1 comment
Open

Comments

@larsnystrom
Copy link

Mockery::mock() returns a Mockery\MockInterface. MockInterface::shouldReceive() et al returns an Mockery\Expectation, which in turn has a getMock() method to return the MockInterface the Expectation belongs to.

The problem is that phpstan says the return type of Expectation::getMock() is Mockery\MockInterface with no type information on what was originally mocked. So for example

interface A {
    foo(): void
}

function getMock(): A
{
    return Mockery::mock(A::class)
        ->shouldReceive('foo')
        ->getMock();
}

will cause the following phpstan error: Function getMock() should return A but returns Mockery\MockInterface.

Of course the code in getMock() can be rewritten to not be fluent so that the return value of Mockery::mock() is returned instead, however in many cases I mock objects inline in some other expression and when I do that it's really helpful to be able to use the fluent way of writing the expectations.

Is it possible to change the type of the return value of Mockery\Expectation::getMock() to include the type information of the original mock? With generics I guess it should be something like Mockery::mock(T): MockInterface<T>, MockInterface<T>::shouldReceive(): Expectation<T> and Expectation<T>::getMock(): MockInterface<T>.

@ondrejmirtes
Copy link
Member

The problem is that non of these classes/interfaces are currently generic. The mocks are intersection types. We could probably make Expectation generic with T, and we could write a dynamic return type extension for shouldReceive so that when called on MockInterface&Foo, it returns Expectation<Foo>.

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

No branches or pull requests

2 participants