Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit 9509df0

Browse files
gmponosdbu
authored andcommitted
Fix handling of invalid $reason and rewrote PromiseExceptionTests (#63)
* Reason callback will not use a reason that is not a Throwable as root exception * Use data provider for tests of PromiseExceptionTest
1 parent 89fdf26 commit 9509df0

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

src/Promise.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ public function __construct(PromiseInterface $promise, RequestInterface $request
6161
$this->exception = $reason;
6262
} elseif ($reason instanceof GuzzleExceptions\GuzzleException) {
6363
$this->exception = $this->handleException($reason, $request);
64-
} elseif ($reason instanceof \Exception) {
64+
} elseif ($reason instanceof \Throwable) {
6565
$this->exception = new \RuntimeException('Invalid exception returned from Guzzle6', 0, $reason);
6666
} else {
67-
$this->exception = new \UnexpectedValueException('Reason returned from Guzzle6 must be an Exception', 0, $reason);
67+
$this->exception = new \UnexpectedValueException('Reason returned from Guzzle6 must be an Exception');
6868
}
6969

7070
throw $this->exception;

tests/PromiseExceptionTest.php

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,62 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Http\Adapter\Guzzle6\Tests;
46

57
use GuzzleHttp\Exception as GuzzleExceptions;
68
use Http\Adapter\Guzzle6\Promise;
9+
use Http\Client\Exception\HttpException;
10+
use Http\Client\Exception\NetworkException;
11+
use Http\Client\Exception\RequestException;
12+
use Http\Client\Exception\TransferException;
713
use PHPUnit\Framework\TestCase;
14+
use Psr\Http\Message\RequestInterface;
15+
use Psr\Http\Message\ResponseInterface;
816

917
/**
1018
* @author Tobias Nyholm <[email protected]>
19+
* @author George Mponos <[email protected]>
1120
*/
12-
class PromiseExceptionTest extends TestCase
21+
final class PromiseExceptionTest extends TestCase
1322
{
14-
public function testGetException()
15-
{
16-
$request = $this->getMockBuilder('Psr\Http\Message\RequestInterface')->getMock();
17-
$response = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();
18-
$promise = $this->getMockBuilder('GuzzleHttp\Promise\PromiseInterface')->getMock();
19-
20-
$adapter = new Promise($promise, $request);
21-
$method = new \ReflectionMethod('Http\Adapter\Guzzle6\Promise', 'handleException');
22-
$method->setAccessible(true);
23-
24-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ConnectException('foo', $request), $request);
25-
$this->assertInstanceOf('Http\Client\Exception\NetworkException', $outputException, "Guzzle's ConnectException should be converted to a NetworkException");
26-
27-
$outputException = $method->invoke($adapter, new GuzzleExceptions\TooManyRedirectsException('foo', $request), $request);
28-
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's TooManyRedirectsException should be converted to a RequestException");
29-
30-
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request, $response), $request);
31-
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's RequestException should be converted to a HttpException");
32-
33-
$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request, $response), $request);
34-
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's BadResponseException should be converted to a HttpException");
35-
36-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request, $response), $request);
37-
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ClientException should be converted to a HttpException");
38-
39-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request, $response), $request);
40-
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ServerException should be converted to a HttpException");
41-
42-
$outputException = $method->invoke($adapter, new GuzzleExceptions\TransferException('foo'), $request);
43-
$this->assertInstanceOf('Http\Client\Exception\TransferException', $outputException, "Guzzle's TransferException should be converted to a TransferException");
44-
45-
/*
46-
* Test RequestException without response
47-
*/
48-
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request), $request);
49-
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's RequestException with no response should be converted to a RequestException");
50-
51-
$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request), $request);
52-
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's BadResponseException with no response should be converted to a RequestException");
53-
54-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request), $request);
55-
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's ClientException with no response should be converted to a RequestException");
23+
/**
24+
* @dataProvider exceptionThatIsThrownForGuzzleExceptionProvider
25+
*/
26+
public function testExceptionThatIsThrownForGuzzleException(
27+
RequestInterface $request,
28+
$reason,
29+
string $adapterExceptionClass
30+
) {
31+
$guzzlePromise = new \GuzzleHttp\Promise\Promise();
32+
$guzzlePromise->reject($reason);
33+
$promise = new Promise($guzzlePromise, $request);
34+
$this->expectException($adapterExceptionClass);
35+
$promise->wait();
36+
}
5637

57-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request), $request);
58-
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's ServerException with no response should be converted to a RequestException");
38+
public function exceptionThatIsThrownForGuzzleExceptionProvider(): array
39+
{
40+
$request = $this->getMockBuilder(RequestInterface::class)->getMock();
41+
$response = $this->getMockBuilder(ResponseInterface::class)->getMock();
42+
43+
return [
44+
[$request, new GuzzleExceptions\ConnectException('foo', $request), NetworkException::class],
45+
[$request, new GuzzleExceptions\TooManyRedirectsException('foo', $request), RequestException::class],
46+
[$request, new GuzzleExceptions\RequestException('foo', $request, $response), HttpException::class],
47+
[$request, new GuzzleExceptions\BadResponseException('foo', $request, $response), HttpException::class],
48+
[$request, new GuzzleExceptions\ClientException('foo', $request, $response), HttpException::class],
49+
[$request, new GuzzleExceptions\ServerException('foo', $request, $response), HttpException::class],
50+
[$request, new GuzzleExceptions\TransferException('foo'), TransferException::class],
51+
// check cases without response
52+
[$request, new GuzzleExceptions\RequestException('foo', $request), RequestException::class],
53+
[$request, new GuzzleExceptions\BadResponseException('foo', $request), RequestException::class],
54+
[$request, new GuzzleExceptions\ClientException('foo', $request), RequestException::class],
55+
[$request, new GuzzleExceptions\ServerException('foo', $request), RequestException::class],
56+
// Non PSR-18 Exceptions thrown
57+
[$request, new \Exception('foo'), \RuntimeException::class],
58+
[$request, new \Error('foo'), \RuntimeException::class],
59+
[$request, 'whatever', \UnexpectedValueException::class],
60+
];
5961
}
6062
}

0 commit comments

Comments
 (0)