Skip to content

Commit 6dc544b

Browse files
committed
Add tests for exceptions, fix some mistakes
1 parent e341e16 commit 6dc544b

9 files changed

+259
-3
lines changed

spec/Exception/BatchExceptionSpec.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Http\Client\Exception\TransferException;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class BatchExceptionSpec extends ObjectBehavior
10+
{
11+
function let(TransferException $e, ResponseInterface $response)
12+
{
13+
$this->beConstructedWith([$e], [$response]);
14+
}
15+
16+
function it_is_initializable()
17+
{
18+
$this->shouldHaveType('Http\Client\Exception\BatchException');
19+
}
20+
21+
function it_is_a_transfer_exception()
22+
{
23+
$this->shouldHaveType('Http\Client\Exception\TransferException');
24+
}
25+
26+
function it_has_exceptions(TransferException $e, TransferException $e2)
27+
{
28+
$this->getExceptions()->shouldReturn([$e]);
29+
$this->hasException($e)->shouldReturn(true);
30+
$this->hasException($e2)->shouldReturn(false);
31+
$this->hasExceptions()->shouldReturn(true);
32+
}
33+
34+
function it_has_responses(ResponseInterface $response, ResponseInterface $response2)
35+
{
36+
$this->getResponses()->shouldReturn([$response]);
37+
$this->hasResponse($response)->shouldReturn(true);
38+
$this->hasResponse($response2)->shouldReturn(false);
39+
$this->hasResponses()->shouldReturn(true);
40+
}
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class ClientExceptionSpec extends ObjectBehavior
10+
{
11+
function let(RequestInterface $request, ResponseInterface $response)
12+
{
13+
$response->getStatusCode()->willReturn(400);
14+
15+
$this->beConstructedWith('message', $request, $response);
16+
}
17+
18+
function it_is_initializable()
19+
{
20+
$this->shouldHaveType('Http\Client\Exception\ClientException');
21+
}
22+
23+
function it_is_http_exception()
24+
{
25+
$this->shouldHaveType('Http\Client\Exception\HttpException');
26+
}
27+
}

spec/Exception/HttpExceptionSpec.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class HttpExceptionSpec extends ObjectBehavior
10+
{
11+
function let(RequestInterface $request, ResponseInterface $response)
12+
{
13+
$response->getStatusCode()->willReturn(400);
14+
15+
$this->beConstructedWith('message', $request, $response);
16+
}
17+
18+
function it_is_initializable()
19+
{
20+
$this->shouldHaveType('Http\Client\Exception\HttpException');
21+
}
22+
23+
function it_is_request_exception()
24+
{
25+
$this->shouldHaveType('Http\Client\Exception\RequestException');
26+
}
27+
28+
function it_has_a_response(ResponseInterface $response)
29+
{
30+
$this->getResponse()->shouldReturn($response);
31+
}
32+
33+
function it_creates_a_client_exception(RequestInterface $request, ResponseInterface $response)
34+
{
35+
$request->getRequestTarget()->willReturn('/uri');
36+
$request->getMethod()->willReturn('GET');
37+
$response->getStatusCode()->willReturn(404);
38+
$response->getReasonPhrase()->willReturn('Not Found');
39+
40+
$e = $this->create($request, $response);
41+
42+
$e->shouldHaveType('Http\Client\Exception\ClientException');
43+
$e->getMessage()->shouldReturn('Client error [url] /uri [http method] GET [status code] 404 [reason phrase] Not Found');
44+
}
45+
46+
function it_creates_a_server_exception(RequestInterface $request, ResponseInterface $response)
47+
{
48+
$request->getRequestTarget()->willReturn('/uri');
49+
$request->getMethod()->willReturn('GET');
50+
$response->getStatusCode()->willReturn(500);
51+
$response->getReasonPhrase()->willReturn('Internal Server Error');
52+
53+
$e = $this->create($request, $response);
54+
55+
$e->shouldHaveType('Http\Client\Exception\ServerException');
56+
$e->getMessage()->shouldReturn('Server error [url] /uri [http method] GET [status code] 500 [reason phrase] Internal Server Error');
57+
}
58+
59+
function it_creates_an_http_exception(RequestInterface $request, ResponseInterface $response)
60+
{
61+
$request->getRequestTarget()->willReturn('/uri');
62+
$request->getMethod()->willReturn('GET');
63+
$response->getStatusCode()->willReturn(100);
64+
$response->getReasonPhrase()->willReturn('Continue');
65+
66+
$e = $this->create($request, $response);
67+
68+
$e->shouldHaveType('Http\Client\Exception\HttpException');
69+
$e->getMessage()->shouldReturn('Unsuccessful response [url] /uri [http method] GET [status code] 100 [reason phrase] Continue');
70+
}
71+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class NetworkExceptionSpec extends ObjectBehavior
9+
{
10+
function let(RequestInterface $request)
11+
{
12+
$this->beConstructedWith('message', $request);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Client\Exception\NetworkException');
18+
}
19+
20+
function it_is_request_exception()
21+
{
22+
$this->shouldHaveType('Http\Client\Exception\RequestException');
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Http\Client\Exception\RequestException;
6+
use Psr\Http\Message\RequestInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class RequestExceptionSpec extends ObjectBehavior
10+
{
11+
function let(RequestInterface $request)
12+
{
13+
$this->beConstructedWith('message', $request);
14+
}
15+
16+
function it_is_initializable()
17+
{
18+
$this->shouldHaveType('Http\Client\Exception\RequestException');
19+
}
20+
21+
function it_has_a_request(RequestInterface $request)
22+
{
23+
$this->getRequest()->shouldReturn($request);
24+
}
25+
26+
function it_wraps_an_exception(RequestInterface $request)
27+
{
28+
$e = new \Exception('message');
29+
30+
$requestException = $this->wrapException($request, $e);
31+
32+
$requestException->getMessage()->shouldReturn('message');
33+
}
34+
35+
function it_does_not_wrap_if_request_exception(RequestInterface $request, RequestException $requestException)
36+
{
37+
$this->wrapException($request, $requestException)->shouldReturn($requestException);
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class ServerExceptionSpec extends ObjectBehavior
10+
{
11+
function let(RequestInterface $request, ResponseInterface $response)
12+
{
13+
$response->getStatusCode()->willReturn(500);
14+
15+
$this->beConstructedWith('message', $request, $response);
16+
}
17+
18+
function it_is_initializable()
19+
{
20+
$this->shouldHaveType('Http\Client\Exception\ServerException');
21+
}
22+
23+
function it_is_http_exception()
24+
{
25+
$this->shouldHaveType('Http\Client\Exception\HttpException');
26+
}
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class TransferExceptionSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('Http\Client\Exception\TransferException');
13+
}
14+
}

src/Exception/BatchException.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,21 @@ public function __construct(array $exceptions = [], array $responses = [])
2727
{
2828
parent::__construct('An error occurred when sending multiple requests.');
2929

30-
$this->setExceptions($exceptions);
31-
$this->setResponses($responses);
30+
foreach ($exceptions as $e) {
31+
if (!$e instanceof TransferException) {
32+
throw new InvalidArgumentException('Exception is not an instanceof Http\Client\Exception\TransferException');
33+
}
34+
}
35+
36+
foreach ($responses as $response) {
37+
if (!$response instanceof ResponseInterface) {
38+
throw new InvalidArgumentException('Response is not an instanceof Psr\Http\Message\ResponseInterface');
39+
}
40+
}
41+
42+
43+
$this->exceptions = $exceptions;
44+
$this->responses = $responses;
3245
}
3346

3447
/**

src/Exception/HttpException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function create(RequestInterface $request, ResponseInterface $resp
7171
}
7272

7373
$message = sprintf(
74-
'%s [url] %s [http method] %s [status code] %s [reason phrase] %',
74+
'%s [url] %s [http method] %s [status code] %s [reason phrase] %s',
7575
$message,
7676
$request->getRequestTarget(),
7777
$request->getMethod(),

0 commit comments

Comments
 (0)