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