Skip to content

Commit 8b279c5

Browse files
committed
Add streamFactory, require factory implementation, lazyload factories
1 parent 8476014 commit 8b279c5

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"psr/http-client": "^1",
4545
"psr/http-client-implementation": "^1",
4646
"psr/http-factory": "^1",
47+
"psr/http-factory-implementation": "^1",
4748
"symfony/http-foundation": "^2.1||^3||^4",
4849
"zendframework/zend-diactoros": "^2.1"
4950
},

src/Common/Http/Client.php

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,62 @@
88
use Omnipay\Common\Http\Exception\RequestException;
99
use Psr\Http\Message\RequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\StreamFactoryInterface;
1112
use Psr\Http\Message\StreamInterface;
12-
use Psr\Http\Client\ClientInterface as Psr18ClientInterface;
13-
use Psr\Http\Message\RequestFactoryInterface as Psr17RequestFactoryInterface;
13+
use Psr\Http\Client\ClientInterface as HttpClientInterface;
14+
use Psr\Http\Message\RequestFactoryInterface as RequestFactoryInterface;
15+
use Psr\Http\Message\UriFactoryInterface;
1416

1517
final class Client implements ClientInterface
1618
{
1719
/**
1820
* The Http Client which implements `public function sendRequest(RequestInterface $request)`
1921
*
20-
* @var Psr18ClientInterface
22+
* @var HttpClientInterface
2123
*/
2224
private $httpClient;
2325

2426
/**
25-
* @var Psr17RequestFactoryInterface
27+
* @var RequestFactoryInterface
2628
*/
2729
private $requestFactory;
2830

31+
/**
32+
* @var StreamFactoryInterface
33+
*/
34+
private $streamFactory;
35+
2936
public function __construct($httpClient = null, $requestFactory = null)
3037
{
31-
$this->httpClient = $httpClient ?: Psr18ClientDiscovery::find();
32-
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
38+
$this->httpClient = $httpClient;
39+
$this->requestFactory = $requestFactory;
40+
}
41+
42+
private function getHttpClient() : HttpClientInterface
43+
{
44+
if (!$this->httpClient) {
45+
$this->httpClient = Psr18ClientDiscovery::find();
46+
}
47+
48+
return $this->httpClient;
49+
}
50+
51+
private function getRequestFactory() : RequestFactoryInterface
52+
{
53+
if (!$this->requestFactory) {
54+
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
55+
}
56+
57+
return $this->requestFactory;
58+
}
59+
60+
private function getStreamFactory() : StreamFactoryInterface
61+
{
62+
if (!$this->streamFactory) {
63+
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
64+
}
65+
66+
return $this->streamFactory;
3367
}
3468

3569
/**
@@ -48,12 +82,13 @@ public function request(
4882
$body = null,
4983
$protocolVersion = '1.1'
5084
) {
51-
$request = $this->requestFactory
85+
$request = $this->getRequestFactory()
5286
->createRequest($method, $uri)
5387
->withProtocolVersion($protocolVersion);
5488

5589
if ($body) {
56-
$request = $request->withBody($body);
90+
$stream = $this->getStreamFactory()->createStream($body);
91+
$request = $request->withBody($stream);
5792
}
5893

5994
foreach ($headers as $name => $value) {
@@ -71,7 +106,7 @@ public function request(
71106
private function sendRequest(RequestInterface $request)
72107
{
73108
try {
74-
return $this->httpClient->sendRequest($request);
109+
return $this->getHttpClient()->sendRequest($request);
75110
} catch (\Http\Client\Exception\NetworkException $networkException) {
76111
throw new NetworkException($networkException->getMessage(), $request, $networkException);
77112
} catch (\Throwable $exception) {

tests/Omnipay/Common/Http/ClientTest.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,11 @@
1010
use Omnipay\Tests\TestCase;
1111
use Psr\Http\Client\ClientInterface;
1212
use Psr\Http\Message\RequestFactoryInterface;
13+
use Psr\Http\Message\RequestInterface;
1314

1415
class ClientTest extends TestCase
1516
{
16-
public function testEmptyConstruct()
17-
{
18-
$client = new Client();
19-
20-
$this->assertAttributeInstanceOf(ClientInterface::class, 'httpClient', $client);
21-
$this->assertAttributeInstanceOf(RequestFactoryInterface::class, 'requestFactory', $client);
22-
}
23-
24-
public function testSend()
17+
public function testSendGet()
2518
{
2619
$mockClient = m::mock(ClientInterface::class);
2720
$mockFactory = m::mock(RequestFactoryInterface::class);
@@ -41,7 +34,48 @@ public function testSend()
4134
->once();
4235

4336
$this->assertSame($response, $client->request('GET', '/path'));
37+
}
4438

39+
public function testSendPostJson()
40+
{
41+
$mockClient = m::mock(ClientInterface::class);
42+
$mockFactory = m::mock(RequestFactoryInterface::class);
43+
$client = new Client($mockClient, $mockFactory);
44+
45+
$request = new Request('POST', '/path');
46+
$response = new Response();
47+
48+
$mockFactory->shouldReceive('createRequest')->withArgs([
49+
'POST',
50+
'/path',
51+
])->andReturn($request);
52+
53+
$mockClient->shouldReceive('sendRequest')
54+
->with(m::on(function (RequestInterface $request) {
55+
56+
if ($request->getMethod() !== 'POST') {
57+
return false;
58+
}
59+
60+
if ($request->getHeader('Content-Type') !== ['application/json']) {
61+
return false;
62+
}
63+
64+
if ($request->getBody()->getContents() !== '{foo:bar}') {
65+
return false;
66+
}
67+
68+
return true;
69+
}))
70+
->andReturn($response)
71+
->once();
72+
73+
$this->assertSame($response, $client->request(
74+
'POST',
75+
'/path',
76+
['Content-Type' => 'application/json'],
77+
'{foo:bar}'
78+
));
4579
}
4680

4781
public function testSendException()

0 commit comments

Comments
 (0)