Skip to content

Commit

Permalink
Require psr/http-client, implement exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Mar 27, 2018
1 parent 0757afa commit 2896cb2
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -43,7 +43,8 @@
"php-http/message": "^1.5",
"php-http/discovery": "^1.2.1",
"symfony/http-foundation": "^2.1|^3|^4",
"moneyphp/money": "^3.1"
"moneyphp/money": "^3.1",
"psr/http-client": "^0.1.0"
},
"require-dev": {
"omnipay/tests": "^3",
Expand Down
25 changes: 24 additions & 1 deletion src/Common/Http/Client.php
Expand Up @@ -7,6 +7,8 @@
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestFactory;
use Omnipay\Common\Http\Exception\NetworkException;
use Omnipay\Common\Http\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand Down Expand Up @@ -39,6 +41,10 @@ public function __construct($httpClient = null, RequestFactory $requestFactory =
* @param array $headers
* @param string|array|resource|StreamInterface|null $body
* @param string $protocolVersion
*
* @throws \Psr\Http\Client\Exception\NetworkException
* @throws \Psr\Http\Client\Exception\RequestException
*
* @return ResponseInterface
*/
public function request(
Expand All @@ -50,6 +56,23 @@ public function request(
) : ResponseInterface {
$request = $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);

return $this->httpClient->sendRequest($request);
return $this->sendRequest($request);
}

/**
* @param RequestInterface $request
* @return ResponseInterface
* @throws \Psr\Http\Client\Exception\NetworkException
* @throws \Psr\Http\Client\Exception\RequestException
*/
private function sendRequest(RequestInterface $request)
{
try {
return $this->httpClient->sendRequest($request);
} catch (\Http\Client\Exception\NetworkException $networkException) {
throw new NetworkException($networkException->getMessage(), $request, $networkException);
} catch (\Exception $exception) {
throw new RequestException($exception->getMessage(), $request, $exception);
}
}
}
5 changes: 5 additions & 0 deletions src/Common/Http/ClientInterface.php
Expand Up @@ -2,6 +2,8 @@

namespace Omnipay\Common\Http;

use Psr\Http\Client\Exception\NetworkException;
use Psr\Http\Client\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
Expand All @@ -17,6 +19,9 @@ interface ClientInterface
* @param resource|string|StreamInterface|null $body
* @param string $protocolVersion
*
* @throws RequestException when the HTTP client is passed a request that is invalid and cannot be sent.
* @throws NetworkException if there is an error with the network or the remote server cannot be reached.
*
* @return ResponseInterface
*/
public function request(
Expand Down
31 changes: 31 additions & 0 deletions src/Common/Http/Exception.php
@@ -0,0 +1,31 @@
<?php

namespace Omnipay\Common\Http;

use Psr\Http\Message\RequestInterface;
use Throwable;

abstract class Exception extends \RuntimeException
{
/** @var RequestInterface */
protected $request;

public function __construct(string $message, RequestInterface $request, Throwable $previous = null)
{
$this->request = $request;

parent::__construct($message, 0, $previous);
}

/**
* Returns the request.
*
* The request object MAY be a different object from the one passed to ClientInterface::sendRequest()
*
* @return RequestInterface
*/
public function getRequest(): RequestInterface
{
return $this->getRequest();
}
}
10 changes: 10 additions & 0 deletions src/Common/Http/Exception/NetworkException.php
@@ -0,0 +1,10 @@
<?php

namespace Omnipay\Common\Http\Exception;

use Omnipay\Common\Http\Exception;
use Psr\Http\Client\Exception\NetworkException as PsrNetworkException;

class NetworkException extends Exception implements PsrNetworkException
{
}
10 changes: 10 additions & 0 deletions src/Common/Http/Exception/RequestException.php
@@ -0,0 +1,10 @@
<?php

namespace Omnipay\Common\Http\Exception;

use Omnipay\Common\Http\Exception;
use Psr\Http\Client\Exception\RequestException as PsrRequestException;

class RequestException extends Exception implements PsrRequestException
{
}
56 changes: 56 additions & 0 deletions tests/Omnipay/Common/Http/ClientTest.php
Expand Up @@ -3,11 +3,13 @@
namespace Omnipay\Common\Http;

use GuzzleHttp\Psr7\Response;
use Http\Client\Exception\NetworkException;
use Mockery as m;
use GuzzleHttp\Psr7\Request;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Omnipay\Tests\TestCase;
use Psr\Http\Client\Exception\RequestException;

class ClientTest extends TestCase
{
Expand Down Expand Up @@ -44,4 +46,58 @@ public function testSend()
$this->assertSame($response, $client->request('GET', '/path'));

}

public function testSendException()
{
$mockClient = m::mock(HttpClient::class);
$mockFactory = m::mock(RequestFactory::class);
$client = new Client($mockClient, $mockFactory);

$request = new Request('GET', '/path');
$response = new Response();

$mockFactory->shouldReceive('createRequest')->withArgs([
'GET',
'/path',
[],
null,
'1.1',
])->andReturn($request);

$mockClient->shouldReceive('sendRequest')
->with($request)
->andThrow(new \Exception('Something went wrong'));

$this->expectException(RequestException::class);
$this->expectExceptionMessage('Something went wrong');

$client->request('GET', '/path');
}

public function testSendNetworkException()
{
$mockClient = m::mock(HttpClient::class);
$mockFactory = m::mock(RequestFactory::class);
$client = new Client($mockClient, $mockFactory);

$request = new Request('GET', '/path');
$response = new Response();

$mockFactory->shouldReceive('createRequest')->withArgs([
'GET',
'/path',
[],
null,
'1.1',
])->andReturn($request);

$mockClient->shouldReceive('sendRequest')
->with($request)
->andThrow(new NetworkException('Something went wrong', $request));

$this->expectException(\Psr\Http\Client\Exception\NetworkException::class);
$this->expectExceptionMessage('Something went wrong');

$client->request('GET', '/path');
}
}

0 comments on commit 2896cb2

Please sign in to comment.