|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Http\Adapter\Guzzle6\Tests;
|
4 | 6 |
|
5 | 7 | use GuzzleHttp\Exception as GuzzleExceptions;
|
6 | 8 | 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; |
7 | 13 | use PHPUnit\Framework\TestCase;
|
| 14 | +use Psr\Http\Message\RequestInterface; |
| 15 | +use Psr\Http\Message\ResponseInterface; |
8 | 16 |
|
9 | 17 | /**
|
10 | 18 | * @author Tobias Nyholm <[email protected]>
|
| 19 | + * @author George Mponos <[email protected]> |
11 | 20 | */
|
12 |
| -class PromiseExceptionTest extends TestCase |
| 21 | +final class PromiseExceptionTest extends TestCase |
13 | 22 | {
|
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 | + } |
56 | 37 |
|
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 | + ]; |
59 | 61 | }
|
60 | 62 | }
|
0 commit comments