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

Improve error handling for unsupported return types #604

Merged
merged 1 commit into from
Jun 20, 2023
Merged
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
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ parameters:
count: 1
path: src/Prophecy/Doubler/Doubler.php

-
message: "#^Parameter \\#1 \\$classOrInterface of method Prophecy\\\\Prophet\\:\\:prophesize\\(\\) expects class\\-string\\<object\\>\\|null, string given\\.$#"
count: 1
path: src/Prophecy/Prophecy/MethodProphecy.php

-
message: "#^Unable to resolve the template type T in call to method Prophecy\\\\Prophet\\:\\:prophesize\\(\\)$#"
count: 1
Expand Down
54 changes: 54 additions & 0 deletions spec/Prophecy/Prophecy/MethodProphecySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Prophecy\Argument;
use Prophecy\Argument\ArgumentsWildcard;
use Prophecy\Call\Call;
use Prophecy\Exception\Prophecy\MethodProphecyException;
use Prophecy\Prediction\PredictionInterface;
use Prophecy\Promise\CallbackPromise;
use Prophecy\Promise\PromiseInterface;
Expand Down Expand Up @@ -557,6 +558,59 @@ function it_returns_object_prophecy_for_object_scalar_union(ObjectProphecy $obje
$return->shouldImplement(ProphecySubjectInterface::class);
}

function it_returns_object_prophecy_for_object_return_type(ObjectProphecy $objectProphecy, ArgumentsWildcard $argumentsWildcard)
{
$this->generateMethodProphecyWithReturnValue($objectProphecy, 'foo', 'object');
$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');
$this->beConstructedWith($objectProphecy, 'foo', $argumentsWildcard);

$return = $this->getPromise()->shouldThrow(MethodProphecyException::class)->during('execute', [[], $objectProphecy, $this]);
}

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

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

$this->getPromise()->execute([], $objectProphecy, $this)->shouldBe(true);
}

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

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

$this->getPromise()->execute([], $objectProphecy, $this)->shouldBe(false);
}

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

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

$this->getPromise()->execute([], $objectProphecy, $this)->shouldBe(null);
}

private function generateMethodProphecyWithReturnValue($objectProphecy, string $methodName, string $returnType): void
{
$objectProphecy->reveal()->willReturn(
Expand Down
13 changes: 12 additions & 1 deletion src/Prophecy/Prophecy/MethodProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,17 @@ static function(string $type1, string $type2) {
$this->voidReturnType = true;
}

$this->will(function () use ($defaultType) {
$this->will(function ($args, ObjectProphecy $object, MethodProphecy $method) use ($defaultType) {
switch ($defaultType) {
case 'void': return;
case 'string': return '';
case 'float': return 0.0;
case 'int': return 0;
case 'bool': return false;
case 'array': return array();
case 'true': return true;
case 'false': return false;
case 'null': return null;

case 'callable':
case 'Closure':
Expand All @@ -172,7 +175,15 @@ static function(string $type1, string $type2) {
case 'Generator':
return (function () { yield; })();

case 'object':
$prophet = new Prophet;
return $prophet->prophesize()->reveal();

default:
if (!class_exists($defaultType) && !interface_exists($defaultType)) {
throw new MethodProphecyException(sprintf('Cannot create a return value for the method as the type "%s" is not supported. Configure an explicit return value instead.', $defaultType), $method);
}

$prophet = new Prophet;
return $prophet->prophesize($defaultType)->reveal();
}
Expand Down