Skip to content

Commit 721e5e0

Browse files
spolischookNyholm
authored andcommitted
TypeError: Argument 1 passed to ProfileClient::collectExceptionInformations() must be an instance of Exception (#353)
* Added test that reveal error * Try to fix * Try error * Lets have another try * Finish tests * Added changelog
1 parent 1f28ecc commit 721e5e0

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
88

99
- Configured clients are now tagged with `'httplug.client'`
1010

11+
### Changed
12+
13+
- Fixed error handling. Now TypeErrors and other \Throwable are correctly handled by ProfileClient
14+
1115
## 1.16.0 - 2019-06-05
1216

1317
### Changed

src/Collector/Formatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c
4444
/**
4545
* Formats an exception.
4646
*
47-
* @param Exception $exception
47+
* @param \Throwable $exception
4848
*
4949
* @return string
5050
*/
51-
public function formatException(Exception $exception)
51+
public function formatException(\Throwable $exception)
5252
{
5353
if ($exception instanceof HttpException) {
5454
return $this->formatter->formatResponse($exception->getResponse());

src/Collector/ProfileClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ private function collectResponseInformations(ResponseInterface $response, Stopwa
176176
}
177177

178178
/**
179-
* @param \Exception $exception
179+
* @param \Throwable $exception
180180
* @param StopwatchEvent $event
181181
* @param Stack $stack
182182
*/
183-
private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack)
183+
private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack)
184184
{
185185
if ($exception instanceof HttpException) {
186186
$this->collectResponseInformations($exception->getResponse(), $event, $stack);

tests/Unit/Collector/ProfileClientTest.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Http\Promise\FulfilledPromise;
1515
use Http\Promise\Promise;
1616
use Http\Promise\RejectedPromise;
17+
use PHPUnit\Framework\MockObject\MockObject;
1718
use PHPUnit\Framework\TestCase;
1819
use Psr\Http\Message\RequestInterface;
1920
use Psr\Http\Message\ResponseInterface;
@@ -34,7 +35,7 @@ class ProfileClientTest extends TestCase
3435
private $activeStack;
3536

3637
/**
37-
* @var HttpClient
38+
* @var HttpClient|MockObject
3839
*/
3940
private $client;
4041

@@ -44,7 +45,7 @@ class ProfileClientTest extends TestCase
4445
private $request;
4546

4647
/**
47-
* @var Formatter
48+
* @var Formatter|MockObject
4849
*/
4950
private $formatter;
5051

@@ -110,11 +111,6 @@ public function setUp(): void
110111
->with($this->response)
111112
->willReturn('FormattedResponse')
112113
;
113-
$this->formatter
114-
->method('formatException')
115-
->with($this->exception)
116-
->willReturn('FormattedException')
117-
;
118114

119115
$this->stopwatch
120116
->method('start')
@@ -145,6 +141,26 @@ public function testSendRequest(): void
145141
$this->assertEquals('https', $this->activeStack->getRequestScheme());
146142
}
147143

144+
/**
145+
* @expectedException \Error
146+
* @expectedException "You set string to int prop"
147+
*/
148+
public function testSendRequestTypeError()
149+
{
150+
$this->client
151+
->expects($this->once())
152+
->method('sendRequest')
153+
->willReturnCallback(function () {
154+
throw new \Error('You set string to int prop');
155+
});
156+
$this->formatter
157+
->expects($this->once())
158+
->method('formatException')
159+
->with($this->isInstanceOf(\Error::class));
160+
161+
$this->subject->sendRequest($this->request);
162+
}
163+
148164
public function testSendAsyncRequest(): void
149165
{
150166
$this->client
@@ -211,6 +227,12 @@ public function testOnRejected(): void
211227
->willReturn($this->rejectedPromise)
212228
;
213229

230+
$this->formatter
231+
->method('formatException')
232+
->with($this->exception)
233+
->willReturn('FormattedException')
234+
;
235+
214236
$this->subject->sendAsyncRequest($this->request);
215237

216238
$this->assertEquals(42, $this->activeStack->getDuration());

0 commit comments

Comments
 (0)