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 mocking methods with a "static" return type #621

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions spec/Prophecy/Prophecy/MethodProphecySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,33 @@ function it_returns_object_prophecy_for_object_return_type(ObjectProphecy $objec
$return->shouldImplement(ProphecySubjectInterface::class);
}

function it_returns_double_for_static_return_type(ObjectProphecy $objectProphecy, ArgumentsWildcard $argumentsWildcard)
{
if (\PHP_VERSION_ID < 80000) {
return;
}

$objectProphecy->reveal()->willReturn(
eval(
<<<CODE
class ClassNameWithStaticReturnMethod {
public function foo() : static
{
}
}

return new ClassNameWithStaticReturnMethod();
CODE
)
);
$objectProphecy->addMethodProphecy(Argument::any())->willReturn();

$this->beConstructedWith($objectProphecy, 'foo', $argumentsWildcard);

$return = $this->getPromise()->execute([], $objectProphecy, $this);
$return->shouldImplement(ProphecySubjectInterface::class);
}

function it_throws_for_non_existent_class_return_type(ObjectProphecy $objectProphecy, ArgumentsWildcard $argumentsWildcard)
{
$this->generateMethodProphecyWithReturnValue($objectProphecy, 'foo', 'NonExistentClass');
Expand Down
4 changes: 4 additions & 0 deletions src/Prophecy/Prophecy/MethodProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ static function(string $type1, string $type2) {
$this->voidReturnType = true;
}

if ('static' === $defaultType) {
$defaultType = get_class($double);
}

$this->will(function ($args, ObjectProphecy $object, MethodProphecy $method) use ($defaultType) {
switch ($defaultType) {
case 'void': return;
Expand Down
Loading