|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Http\Adapter\Tests; |
| 4 | + |
| 5 | +use GuzzleHttp\Exception as GuzzleExceptions; |
| 6 | +use GuzzleHttp\Message\Request as GuzzleRequest; |
| 7 | +use GuzzleHttp\Message\Response as GuzzleResponse; |
| 8 | +use GuzzleHttp\Psr7\Request as Psr7Request; |
| 9 | +use GuzzleHttp\Stream\Stream; |
| 10 | +use Http\Adapter\Guzzle5HttpAdapter; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Tobias Nyholm <[email protected]> |
| 14 | + */ |
| 15 | +class Guzzle5ExceptionTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + private $guzzleRequest; |
| 18 | + private $guzzleResponse; |
| 19 | + |
| 20 | + public function setUp() |
| 21 | + { |
| 22 | + $this->guzzleRequest = new GuzzleRequest('GET', 'http://foo.com'); |
| 23 | + $this->guzzleResponse = new GuzzleResponse('400', [], Stream::factory('message body'), ['protocol_version'=>'1,1']); |
| 24 | + } |
| 25 | + |
| 26 | + protected function makeRequest(GuzzleExceptions\TransferException $exception) |
| 27 | + { |
| 28 | + $client = $this->getMock('GuzzleHttp\ClientInterface'); |
| 29 | + $client->expects($this->any())->method('send')->willThrowException($exception); |
| 30 | + $client->expects($this->any())->method('createRequest')->willReturn($this->guzzleRequest); |
| 31 | + |
| 32 | + $request = new Psr7Request('GET', 'http://foo.com'); |
| 33 | + (new Guzzle5HttpAdapter($client))->sendRequest($request); |
| 34 | + } |
| 35 | + |
| 36 | + public function testConnectException() |
| 37 | + { |
| 38 | + // Guzzle's ConnectException should be converted to a NetworkException |
| 39 | + $this->setExpectedException('Http\Client\Exception\NetworkException'); |
| 40 | + $this->makeRequest(new GuzzleExceptions\ConnectException('foo', $this->guzzleRequest)); |
| 41 | + } |
| 42 | + |
| 43 | + public function testTooManyRedirectsException() |
| 44 | + { |
| 45 | + // Guzzle's TooManyRedirectsException should be converted to a RequestException |
| 46 | + $this->setExpectedException('Http\Client\Exception\RequestException'); |
| 47 | + $this->makeRequest(new GuzzleExceptions\TooManyRedirectsException('foo', $this->guzzleRequest)); |
| 48 | + } |
| 49 | + |
| 50 | + public function testRequestException() |
| 51 | + { |
| 52 | + // Guzzle's RequestException should be converted to a HttpException |
| 53 | + $this->setExpectedException('Http\Client\Exception\HttpException'); |
| 54 | + $this->makeRequest(new GuzzleExceptions\RequestException('foo', $this->guzzleRequest, $this->guzzleResponse)); |
| 55 | + } |
| 56 | + |
| 57 | + public function testRequestExceptionWithoutResponse() |
| 58 | + { |
| 59 | + // Guzzle's RequestException with no response should be converted to a RequestException |
| 60 | + $this->setExpectedException('Http\Client\Exception\RequestException'); |
| 61 | + $this->makeRequest(new GuzzleExceptions\RequestException('foo', $this->guzzleRequest)); |
| 62 | + } |
| 63 | + |
| 64 | + public function testBadResponseException() |
| 65 | + { |
| 66 | + // Guzzle's BadResponseException should be converted to a HttpException |
| 67 | + $this->setExpectedException('Http\Client\Exception\HttpException'); |
| 68 | + $this->makeRequest(new GuzzleExceptions\BadResponseException('foo', $this->guzzleRequest, $this->guzzleResponse)); |
| 69 | + } |
| 70 | + |
| 71 | + public function testBadResponseExceptionWithoutResponse() |
| 72 | + { |
| 73 | + // Guzzle's BadResponseException with no response should be converted to a RequestException |
| 74 | + $this->setExpectedException('Http\Client\Exception\RequestException'); |
| 75 | + $this->makeRequest(new GuzzleExceptions\BadResponseException('foo', $this->guzzleRequest)); |
| 76 | + } |
| 77 | + |
| 78 | + public function testClientException() |
| 79 | + { |
| 80 | + // Guzzle's ClientException should be converted to a HttpException |
| 81 | + $this->setExpectedException('Http\Client\Exception\HttpException'); |
| 82 | + $this->makeRequest(new GuzzleExceptions\ClientException('foo', $this->guzzleRequest, $this->guzzleResponse)); |
| 83 | + } |
| 84 | + |
| 85 | + public function testClientExceptionWithoutResponse() |
| 86 | + { |
| 87 | + // Guzzle's ClientException with no response should be converted to a RequestException |
| 88 | + $this->setExpectedException('Http\Client\Exception\RequestException'); |
| 89 | + $this->makeRequest(new GuzzleExceptions\ClientException('foo', $this->guzzleRequest)); |
| 90 | + } |
| 91 | + |
| 92 | + public function testServerException() |
| 93 | + { |
| 94 | + // Guzzle's ServerException should be converted to a HttpException |
| 95 | + $this->setExpectedException('Http\Client\Exception\HttpException'); |
| 96 | + $this->makeRequest(new GuzzleExceptions\ServerException('foo', $this->guzzleRequest, $this->guzzleResponse)); |
| 97 | + } |
| 98 | + |
| 99 | + public function testServerExceptionWithoutResponse() |
| 100 | + { |
| 101 | + // Guzzle's ServerException with no response should be converted to a RequestException |
| 102 | + $this->setExpectedException('Http\Client\Exception\RequestException'); |
| 103 | + $this->makeRequest(new GuzzleExceptions\BadResponseException('foo', $this->guzzleRequest)); |
| 104 | + } |
| 105 | + |
| 106 | + public function testTransferException() |
| 107 | + { |
| 108 | + // Guzzle's TransferException should be converted to a TransferException |
| 109 | + $this->setExpectedException('Http\Client\Exception\TransferException'); |
| 110 | + $this->makeRequest(new GuzzleExceptions\TransferException('foo')); |
| 111 | + } |
| 112 | + |
| 113 | + public function testParseException() |
| 114 | + { |
| 115 | + // Guzzle's ParseException should be converted to a TransferException |
| 116 | + $this->setExpectedException('Http\Client\Exception\TransferException'); |
| 117 | + $this->makeRequest(new GuzzleExceptions\ParseException('foo', $this->guzzleResponse)); |
| 118 | + } |
| 119 | +} |
0 commit comments