Skip to content

Commit 4bb6d66

Browse files
committed
Create interface for HttpClient
1 parent 46daacf commit 4bb6d66

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed

src/Common/Http/Client.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Omnipay\Common\Http;
44

5+
use function GuzzleHttp\Psr7\str;
56
use Http\Client\HttpClient;
67
use Http\Discovery\HttpClientDiscovery;
78
use Http\Discovery\MessageFactoryDiscovery;
@@ -11,10 +12,11 @@
1112
use Psr\Http\Message\StreamInterface;
1213
use Psr\Http\Message\UriInterface;
1314

14-
class Client implements RequestFactory
15+
class Client implements ClientInterface
1516
{
1617
/**
1718
* The Http Client which implements `public function sendRequest(RequestInterface $request)`
19+
* Note: Will be changed to PSR-18 when released
1820
*
1921
* @var HttpClient
2022
*/
@@ -39,32 +41,15 @@ public function __construct($httpClient = null, RequestFactory $requestFactory =
3941
* @param string $protocolVersion
4042
* @return ResponseInterface
4143
*/
42-
public function request($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
43-
{
44-
$request = $this->createRequest($method, $uri, $headers, $body, $protocolVersion);
45-
46-
return $this->sendRequest($request);
47-
}
48-
49-
/**
50-
* @param $method
51-
* @param $uri
52-
* @param array $headers
53-
* @param string|resource|StreamInterface|null $body
54-
* @param string $protocolVersion
55-
* @return RequestInterface
56-
*/
57-
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
58-
{
59-
return $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
60-
}
44+
public function request(
45+
string $method,
46+
$uri,
47+
array $headers = [],
48+
$body = null,
49+
string $protocolVersion = '1.1'
50+
) : ResponseInterface {
51+
$request = $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
6152

62-
/**
63-
* @param RequestInterface $request
64-
* @return ResponseInterface
65-
*/
66-
public function sendRequest(RequestInterface $request)
67-
{
6853
return $this->httpClient->sendRequest($request);
6954
}
7055
}

src/Common/Http/ClientInterface.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Http;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\StreamInterface;
7+
use Psr\Http\Message\UriInterface;
8+
9+
interface ClientInterface
10+
{
11+
/**
12+
* Creates a new PSR-7 request.
13+
*
14+
* @param string $method
15+
* @param string|UriInterface $uri
16+
* @param array $headers
17+
* @param resource|string|StreamInterface|null $body
18+
* @param string $protocolVersion
19+
*
20+
* @return ResponseInterface
21+
*/
22+
public function request(
23+
string $method,
24+
$uri,
25+
array $headers = [],
26+
$body = null,
27+
string $protocolVersion = '1.1'
28+
) : ResponseInterface;
29+
}

tests/Omnipay/Common/Http/ClientTest.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Omnipay\Common\Http;
44

5+
use GuzzleHttp\Psr7\Response;
56
use Mockery as m;
67
use GuzzleHttp\Psr7\Request;
78
use Http\Client\HttpClient;
@@ -18,25 +19,14 @@ public function testEmptyConstruct()
1819
$this->assertAttributeInstanceOf(RequestFactory::class, 'requestFactory', $client);
1920
}
2021

21-
public function testCreateRequest()
22-
{
23-
$client = $this->getHttpClient();
24-
25-
$request = $client->createRequest('GET', '/path', ['foo' => 'bar']);
26-
27-
$this->assertInstanceOf(Request::class, $request);
28-
$this->assertEquals('/path', $request->getUri());
29-
$this->assertEquals(['bar'], $request->getHeader('foo'));
30-
}
31-
32-
3322
public function testSend()
3423
{
3524
$mockClient = m::mock(HttpClient::class);
3625
$mockFactory = m::mock(RequestFactory::class);
3726
$client = new Client($mockClient, $mockFactory);
3827

3928
$request = new Request('GET', '/path');
29+
$response = new Response();
4030

4131
$mockFactory->shouldReceive('createRequest')->withArgs([
4232
'GET',
@@ -46,8 +36,12 @@ public function testSend()
4636
'1.1',
4737
])->andReturn($request);
4838

49-
$mockClient->shouldReceive('sendRequest')->with($request)->once();
39+
$mockClient->shouldReceive('sendRequest')
40+
->with($request)
41+
->andReturn($response)
42+
->once();
43+
44+
$this->assertSame($response, $client->request('GET', '/path'));
5045

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

0 commit comments

Comments
 (0)