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

Support for mocking methods with "static" return type #620

Closed
aik099 opened this issue Mar 30, 2024 · 5 comments
Closed

Support for mocking methods with "static" return type #620

aik099 opened this issue Mar 30, 2024 · 5 comments

Comments

@aik099
Copy link
Member

aik099 commented Mar 30, 2024

When attempting to mock a method with a static return type (see https://php.watch/versions/8.0/static-return-type), then you'll get this error:

Prophecy\Exception\Prophecy\MethodProphecyException: Cannot create a return value for the method as the type "static" is not supported. Configure an explicit return value instead.

Code:

class Process implements \IteratorAggregate
{
    public function mustRun(?callable $callback = null, array $env = []): static
    {
        return $this;
    }
}
@stof
Copy link
Member

stof commented Aug 14, 2024

The error message does not say that mocking such methods is not supported. It says that guessing a return value is not supported and you need to configure an explicit one (for instance by using ->willReturnSelf())

@aik099
Copy link
Member Author

aik099 commented Aug 14, 2024

The error message does not say that mocking such methods is not supported. It says that guessing a return value is not supported and you need to configure an explicit one (for instance by using ->willReturnSelf())

That helped. Thanks.

What about the associated PR ( #621 ) to improve guessing code?

@aik099
Copy link
Member Author

aik099 commented Aug 14, 2024

I'm getting an error, that willReturnSelf method isn't defined in the used Prophecy version. I've used this code instead:

$mock = $this->prophesize('Process');
$mock->mustRun()->willReturn($mock)->shouldBeCalled();

is this correct way or I should be doing

$mock = $this->prophesize('Process');
$mock->mustRun()->willReturn($mock->reveal())->shouldBeCalled();

instead?

@stof
Copy link
Member

stof commented Aug 14, 2024

ah indeed. We don't have willReturnSelf in Prophecy (maybe I confused with PHPUnit mock or with a past discussion on Prophecy that has not been implemented).

The first code snippet is fine. ->willReturn does not require revealing explicitly.

@aik099
Copy link
Member Author

aik099 commented Aug 14, 2024

Great. Closing.

@aik099 aik099 closed this as completed Aug 14, 2024
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

Successfully merging a pull request may close this issue.

2 participants