From 81c63ff993abccd44fa8aeda9589ebe92bf1b315 Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Tue, 30 Jul 2019 15:23:10 +0300 Subject: [PATCH 1/6] Added test that reveal error --- tests/Unit/Collector/ProfileClientTest.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index f5d39064..c01b1b8b 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -14,6 +14,7 @@ use Http\Promise\FulfilledPromise; use Http\Promise\Promise; use Http\Promise\RejectedPromise; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -34,7 +35,7 @@ class ProfileClientTest extends TestCase private $activeStack; /** - * @var HttpClient + * @var HttpClient|MockObject */ private $client; @@ -145,6 +146,16 @@ public function testSendRequest(): void $this->assertEquals('https', $this->activeStack->getRequestScheme()); } + public function testSendRequestTypeError() + { + $this->client + ->expects($this->once()) + ->method('sendRequest') + ->willThrowException(new \TypeError('You set string to int prop')); + + $response = $this->subject->sendRequest($this->request); + } + public function testSendAsyncRequest(): void { $this->client From b7640360d988b1202af5e80f4ad64eeee9bb4dc6 Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Tue, 30 Jul 2019 15:44:15 +0300 Subject: [PATCH 2/6] Try to fix --- src/Collector/Formatter.php | 4 ++-- src/Collector/ProfileClient.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Collector/Formatter.php b/src/Collector/Formatter.php index 7732600f..e2eb9c12 100644 --- a/src/Collector/Formatter.php +++ b/src/Collector/Formatter.php @@ -44,11 +44,11 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c /** * Formats an exception. * - * @param Exception $exception + * @param \Throwable $exception * * @return string */ - public function formatException(Exception $exception) + public function formatException(\Throwable $exception) { if ($exception instanceof HttpException) { return $this->formatter->formatResponse($exception->getResponse()); diff --git a/src/Collector/ProfileClient.php b/src/Collector/ProfileClient.php index 58d7b1ac..318120ea 100644 --- a/src/Collector/ProfileClient.php +++ b/src/Collector/ProfileClient.php @@ -176,11 +176,11 @@ private function collectResponseInformations(ResponseInterface $response, Stopwa } /** - * @param \Exception $exception + * @param \Throwable $exception * @param StopwatchEvent $event * @param Stack $stack */ - private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) + private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack) { if ($exception instanceof HttpException) { $this->collectResponseInformations($exception->getResponse(), $event, $stack); From 24e7c856fb4ecd5c1f98d2ffab8bba563c3cfb3c Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Tue, 30 Jul 2019 15:52:04 +0300 Subject: [PATCH 3/6] Try error --- tests/Unit/Collector/ProfileClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index c01b1b8b..ab1cc844 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -151,7 +151,7 @@ public function testSendRequestTypeError() $this->client ->expects($this->once()) ->method('sendRequest') - ->willThrowException(new \TypeError('You set string to int prop')); + ->willThrowException(new \Error('You set string to int prop')); $response = $this->subject->sendRequest($this->request); } From 49a7817e34cc92c14480aa6c74fd75a7abcece3b Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Tue, 30 Jul 2019 15:58:09 +0300 Subject: [PATCH 4/6] Lets have another try --- tests/Unit/Collector/ProfileClientTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index ab1cc844..7c695d99 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -151,7 +151,9 @@ public function testSendRequestTypeError() $this->client ->expects($this->once()) ->method('sendRequest') - ->willThrowException(new \Error('You set string to int prop')); + ->willReturnCallback(function () { + throw new \Error('You set string to int prop'); + }); $response = $this->subject->sendRequest($this->request); } From 879fca27d618ae724c6a4356786fc316e8560b7a Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Tue, 30 Jul 2019 16:52:34 +0300 Subject: [PATCH 5/6] Finish tests --- tests/Unit/Collector/ProfileClientTest.php | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index 7c695d99..2cc7a607 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -45,7 +45,7 @@ class ProfileClientTest extends TestCase private $request; /** - * @var Formatter + * @var Formatter|MockObject */ private $formatter; @@ -111,11 +111,6 @@ public function setUp(): void ->with($this->response) ->willReturn('FormattedResponse') ; - $this->formatter - ->method('formatException') - ->with($this->exception) - ->willReturn('FormattedException') - ; $this->stopwatch ->method('start') @@ -146,6 +141,10 @@ public function testSendRequest(): void $this->assertEquals('https', $this->activeStack->getRequestScheme()); } + /** + * @expectedException \Error + * @expectedException "You set string to int prop" + */ public function testSendRequestTypeError() { $this->client @@ -154,8 +153,12 @@ public function testSendRequestTypeError() ->willReturnCallback(function () { throw new \Error('You set string to int prop'); }); + $this->formatter + ->expects($this->once()) + ->method('formatException') + ->with($this->isInstanceOf(\Error::class)); - $response = $this->subject->sendRequest($this->request); + $this->subject->sendRequest($this->request); } public function testSendAsyncRequest(): void @@ -224,6 +227,12 @@ public function testOnRejected(): void ->willReturn($this->rejectedPromise) ; + $this->formatter + ->method('formatException') + ->with($this->exception) + ->willReturn('FormattedException') + ; + $this->subject->sendAsyncRequest($this->request); $this->assertEquals(42, $this->activeStack->getDuration()); From bd1c2ac6847516485bc335b98be5aecad344b6de Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Tue, 30 Jul 2019 17:11:39 +0300 Subject: [PATCH 6/6] Added changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ece91f0e..2ea82242 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee - Configured clients are now tagged with `'httplug.client'` +### Changed + +- Fixed error handling. Now TypeErrors and other \Throwable are correctly handled by ProfileClient + ## 1.16.0 - 2019-06-05 ### Changed