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

Commit feb344d

Browse files
committed
Merge pull request #7 from Nyholm/patch-3
Added tests for exceptions
2 parents 4d81848 + 9cdaf70 commit feb344d

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
],
1717
"require": {
1818
"php": ">=5.4",
19-
"php-http/httplug": "^1.0.0-beta",
19+
"php-http/httplug": "1.0.0-beta",
2020
"php-http/discovery": "~0.5",
2121
"guzzlehttp/guzzle": "^5.1"
2222
},
2323
"require-dev": {
2424
"ext-curl": "*",
2525
"guzzlehttp/ringphp": "^1.1",
2626
"php-http/adapter-integration-tests": "dev-master",
27-
"php-http/client-common": "^0.1"
27+
"php-http/client-common": "^0.1",
28+
"guzzlehttp/psr7": "^1.2"
2829
},
2930
"provide": {
3031
"php-http/client-implementation": "1.0"

src/Guzzle5HttpAdapter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/**
1818
* @author GeLo <[email protected]>
19+
* @author Tobias Nyholm <[email protected]>
1920
*/
2021
class Guzzle5HttpAdapter implements HttpClient
2122
{

tests/Guzzle5ExceptionTest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)