From 2305dd0ed21286847e24e938d06c298b33ca112c Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 30 Mar 2024 20:48:21 +0200 Subject: [PATCH] Support mocking methods with a "static" return type --- spec/Prophecy/Prophecy/MethodProphecySpec.php | 27 +++++++++++++++++++ src/Prophecy/Prophecy/MethodProphecy.php | 4 +++ 2 files changed, 31 insertions(+) diff --git a/spec/Prophecy/Prophecy/MethodProphecySpec.php b/spec/Prophecy/Prophecy/MethodProphecySpec.php index 9b5bde6cd..b70d486d6 100644 --- a/spec/Prophecy/Prophecy/MethodProphecySpec.php +++ b/spec/Prophecy/Prophecy/MethodProphecySpec.php @@ -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( + <<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'); diff --git a/src/Prophecy/Prophecy/MethodProphecy.php b/src/Prophecy/Prophecy/MethodProphecy.php index 3dfd82de2..cdb80dfd6 100644 --- a/src/Prophecy/Prophecy/MethodProphecy.php +++ b/src/Prophecy/Prophecy/MethodProphecy.php @@ -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;