Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor exceptions #43

Merged
merged 11 commits into from
Sep 21, 2015
41 changes: 41 additions & 0 deletions spec/Exception/BatchExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace spec\Http\Client\Exception;

use Http\Client\Exception\TransferException;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class BatchExceptionSpec extends ObjectBehavior
{
function let(TransferException $e, ResponseInterface $response)
{
$this->beConstructedWith([$e], [$response]);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Exception\BatchException');
}

function it_is_a_transfer_exception()
{
$this->shouldHaveType('Http\Client\Exception\TransferException');
}

function it_has_exceptions(TransferException $e, TransferException $e2)
{
$this->getExceptions()->shouldReturn([$e]);
$this->hasException($e)->shouldReturn(true);
$this->hasException($e2)->shouldReturn(false);
$this->hasExceptions()->shouldReturn(true);
}

function it_has_responses(ResponseInterface $response, ResponseInterface $response2)
{
$this->getResponses()->shouldReturn([$response]);
$this->hasResponse($response)->shouldReturn(true);
$this->hasResponse($response2)->shouldReturn(false);
$this->hasResponses()->shouldReturn(true);
}
}
27 changes: 27 additions & 0 deletions spec/Exception/ClientExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace spec\Http\Client\Exception;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class ClientExceptionSpec extends ObjectBehavior
{
function let(RequestInterface $request, ResponseInterface $response)
{
$response->getStatusCode()->willReturn(400);

$this->beConstructedWith('message', $request, $response);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Exception\ClientException');
}

function it_is_http_exception()
{
$this->shouldHaveType('Http\Client\Exception\HttpException');
}
}
71 changes: 71 additions & 0 deletions spec/Exception/HttpExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace spec\Http\Client\Exception;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class HttpExceptionSpec extends ObjectBehavior
{
function let(RequestInterface $request, ResponseInterface $response)
{
$response->getStatusCode()->willReturn(400);

$this->beConstructedWith('message', $request, $response);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Exception\HttpException');
}

function it_is_request_exception()
{
$this->shouldHaveType('Http\Client\Exception\RequestException');
}

function it_has_a_response(ResponseInterface $response)
{
$this->getResponse()->shouldReturn($response);
}

function it_creates_a_client_exception(RequestInterface $request, ResponseInterface $response)
{
$request->getRequestTarget()->willReturn('/uri');
$request->getMethod()->willReturn('GET');
$response->getStatusCode()->willReturn(404);
$response->getReasonPhrase()->willReturn('Not Found');

$e = $this->create($request, $response);

$e->shouldHaveType('Http\Client\Exception\ClientException');
$e->getMessage()->shouldReturn('Client error [url] /uri [http method] GET [status code] 404 [reason phrase] Not Found');
}

function it_creates_a_server_exception(RequestInterface $request, ResponseInterface $response)
{
$request->getRequestTarget()->willReturn('/uri');
$request->getMethod()->willReturn('GET');
$response->getStatusCode()->willReturn(500);
$response->getReasonPhrase()->willReturn('Internal Server Error');

$e = $this->create($request, $response);

$e->shouldHaveType('Http\Client\Exception\ServerException');
$e->getMessage()->shouldReturn('Server error [url] /uri [http method] GET [status code] 500 [reason phrase] Internal Server Error');
}

function it_creates_an_http_exception(RequestInterface $request, ResponseInterface $response)
{
$request->getRequestTarget()->willReturn('/uri');
$request->getMethod()->willReturn('GET');
$response->getStatusCode()->willReturn(100);
$response->getReasonPhrase()->willReturn('Continue');

$e = $this->create($request, $response);

$e->shouldHaveType('Http\Client\Exception\HttpException');
$e->getMessage()->shouldReturn('Unsuccessful response [url] /uri [http method] GET [status code] 100 [reason phrase] Continue');
}
}
24 changes: 24 additions & 0 deletions spec/Exception/NetworkExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace spec\Http\Client\Exception;

use Psr\Http\Message\RequestInterface;
use PhpSpec\ObjectBehavior;

class NetworkExceptionSpec extends ObjectBehavior
{
function let(RequestInterface $request)
{
$this->beConstructedWith('message', $request);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Exception\NetworkException');
}

function it_is_request_exception()
{
$this->shouldHaveType('Http\Client\Exception\RequestException');
}
}
39 changes: 39 additions & 0 deletions spec/Exception/RequestExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace spec\Http\Client\Exception;

use Http\Client\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use PhpSpec\ObjectBehavior;

class RequestExceptionSpec extends ObjectBehavior
{
function let(RequestInterface $request)
{
$this->beConstructedWith('message', $request);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Exception\RequestException');
}

function it_has_a_request(RequestInterface $request)
{
$this->getRequest()->shouldReturn($request);
}

function it_wraps_an_exception(RequestInterface $request)
{
$e = new \Exception('message');

$requestException = $this->wrapException($request, $e);

$requestException->getMessage()->shouldReturn('message');
}

function it_does_not_wrap_if_request_exception(RequestInterface $request, RequestException $requestException)
{
$this->wrapException($request, $requestException)->shouldReturn($requestException);
}
}
27 changes: 27 additions & 0 deletions spec/Exception/ServerExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace spec\Http\Client\Exception;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class ServerExceptionSpec extends ObjectBehavior
{
function let(RequestInterface $request, ResponseInterface $response)
{
$response->getStatusCode()->willReturn(500);

$this->beConstructedWith('message', $request, $response);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Exception\ServerException');
}

function it_is_http_exception()
{
$this->shouldHaveType('Http\Client\Exception\HttpException');
}
}
14 changes: 14 additions & 0 deletions spec/Exception/TransferExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace spec\Http\Client\Exception;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class TransferExceptionSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Exception\TransferException');
}
}
62 changes: 62 additions & 0 deletions src/Exception/BatchException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Http\Client\Exception;

/**
* @author Márk Sági-Kazár <[email protected]>
*/
final class BatchException extends TransferException
{
/**
* @var TransferException[]
*/
private $exceptions;

/**
* @param TransferException[] $exceptions
*/
public function __construct(array $exceptions = [])
{
parent::__construct('An error occurred when sending multiple requests.');

foreach ($exceptions as $e) {
if (!$e instanceof TransferException) {
throw new InvalidArgumentException('Exception is not an instanceof Http\Client\Exception\TransferException');
}
}

$this->exceptions = $exceptions;
}

/**
* Returns all exceptions
*
* @return TransferException[]
*/
public function getExceptions()
{
return $this->exceptions;
}

/**
* Checks if a specific exception exists
*
* @param TransferException $exception
*
* @return boolean TRUE if there is the exception else FALSE.
*/
public function hasException(TransferException $exception)
{
return array_search($exception, $this->exceptions, true) !== false;
}

/**
* Checks if any exception exists
*
* @return boolean
*/
public function hasExceptions()
{
return !empty($this->exceptions);
}
}
12 changes: 12 additions & 0 deletions src/Exception/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Http\Client\Exception;

/**
* Thrown when a client error (4xx) is encountered
*
* @author Márk Sági-Kazár <[email protected]>
*/
final class ClientException extends HttpException
{
}
Loading