From 9db690fbc675f0c5d1874010dc14fc7773a4bcd7 Mon Sep 17 00:00:00 2001 From: ranpafin Date: Sun, 22 Feb 2026 00:42:38 +0100 Subject: [PATCH] Catch Throwable instead of Exception in CallCenter to record all thrown errors When a promise throws a Throwable that is not an Exception (e.g. TypeError, Error), the CallCenter catch block missed it, so the call was never recorded. This aligns CallCenter and Call with ThrowPromise which already supports Throwable. Fixes #595 --- spec/Prophecy/Call/CallCenterSpec.php | 26 ++++++++++++++++++++++++++ spec/Prophecy/Call/CallSpec.php | 8 ++++++++ src/Prophecy/Call/Call.php | 8 ++++---- src/Prophecy/Call/CallCenter.php | 2 +- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/spec/Prophecy/Call/CallCenterSpec.php b/spec/Prophecy/Call/CallCenterSpec.php index 1e3b5dd88..2390fbd66 100644 --- a/spec/Prophecy/Call/CallCenterSpec.php +++ b/spec/Prophecy/Call/CallCenterSpec.php @@ -159,6 +159,32 @@ function it_finds_recorded_calls_by_a_method_name_and_arguments_wildcard( $calls[0]->getArguments()->shouldReturn(array('everything')); } + function it_records_calls_when_promise_throws_a_throwable( + $objectProphecy, + MethodProphecy $method, + ArgumentsWildcard $arguments, + PromiseInterface $promise + ) { + $method->hasReturnVoid()->willReturn(false); + $method->getMethodName()->willReturn('getName'); + $method->getArgumentsWildcard()->willReturn($arguments); + $method->getPromise()->willReturn($promise); + $arguments->scoreArguments(array('world'))->willReturn(100); + + $objectProphecy->getMethodProphecies()->willReturn(array('getName' => array($method))); + $objectProphecy->getMethodProphecies('getName')->willReturn(array($method)); + $objectProphecy->reveal()->willReturn(new \stdClass()); + + $error = new \TypeError('type error'); + $promise->execute(array('world'), $objectProphecy->getWrappedObject(), $method)->willThrow($error); + + $this->shouldThrow($error)->duringMakeCall($objectProphecy, 'getName', array('world')); + + $calls = $this->findCalls('getName', $arguments); + $calls->shouldHaveCount(1); + $calls[0]->getException()->shouldBeAnInstanceOf('TypeError'); + } + function it_records_the_error_when_stub_has_got_unexpected_method_calls( $objectProphecy, MethodProphecy $method, diff --git a/spec/Prophecy/Call/CallSpec.php b/spec/Prophecy/Call/CallSpec.php index 8322ca843..80cbb3079 100644 --- a/spec/Prophecy/Call/CallSpec.php +++ b/spec/Prophecy/Call/CallSpec.php @@ -32,6 +32,14 @@ function it_exposes_exception_through_getter($exception) $this->getException()->shouldReturn($exception); } + function it_accepts_throwable_instances() + { + $error = new \Error('some error'); + $this->beConstructedWith('setValues', array(5, 2), null, $error, 'some_file.php', 23); + + $this->getException()->shouldReturn($error); + } + function it_exposes_file_and_line_through_getter() { $this->getFile()->shouldReturn('some_file.php'); diff --git a/src/Prophecy/Call/Call.php b/src/Prophecy/Call/Call.php index 99b3eab68..852de66c1 100644 --- a/src/Prophecy/Call/Call.php +++ b/src/Prophecy/Call/Call.php @@ -11,7 +11,7 @@ namespace Prophecy\Call; -use Exception; +use Throwable; use Prophecy\Argument\ArgumentsWildcard; /** @@ -44,12 +44,12 @@ class Call * @param string $methodName * @param array $arguments * @param mixed $returnValue - * @param Exception|null $exception + * @param Throwable|null $exception * @param null|string $file * @param null|int $line */ public function __construct($methodName, array $arguments, $returnValue, - ?Exception $exception, $file, $line) + ?Throwable $exception, $file, $line) { $this->methodName = $methodName; $this->arguments = $arguments; @@ -96,7 +96,7 @@ public function getReturnValue() /** * Returns exception that call thrown. * - * @return null|Exception + * @return null|Throwable */ public function getException() { diff --git a/src/Prophecy/Call/CallCenter.php b/src/Prophecy/Call/CallCenter.php index 306dc3d64..686b53452 100644 --- a/src/Prophecy/Call/CallCenter.php +++ b/src/Prophecy/Call/CallCenter.php @@ -101,7 +101,7 @@ public function makeCall(ObjectProphecy $prophecy, $methodName, array $arguments if ($promise = $methodProphecy->getPromise()) { try { $returnValue = $promise->execute($arguments, $prophecy, $methodProphecy); - } catch (\Exception $e) { + } catch (\Throwable $e) { $exception = $e; } }