Skip to content

Commit a28a37b

Browse files
authored
[3.0] Add convenience helpers, rename decoder, add tests (#147)
* Add convenience helpers, rename decoder, add testt
1 parent 3178cd3 commit a28a37b

File tree

5 files changed

+206
-112
lines changed

5 files changed

+206
-112
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
"require-dev": {
3737
"omnipay/tests": "^3",
38+
"php-http/mock-client": "^1",
3839
"squizlabs/php_codesniffer": "^2.8.1"
3940
},
4041
"extra": {

src/Omnipay/Common/Http/Client.php

Lines changed: 23 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -33,153 +33,65 @@ public function __construct(HttpClient $httpClient = null, RequestFactory $reque
3333
* @param $method
3434
* @param $uri
3535
* @param array $headers
36-
* @param null $body
36+
* @param string|array|resource|StreamInterface|null $body
3737
* @param string $protocolVersion
38-
* @return RequestInterface
39-
*/
40-
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
41-
{
42-
return $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
43-
}
44-
45-
/**
46-
* @param RequestInterface $request
47-
* @return ResponseInterface
48-
*/
49-
public function sendRequest(RequestInterface $request)
50-
{
51-
return $this->httpClient->sendRequest($request);
52-
}
53-
54-
/**
55-
* Send a GET request.
56-
*
57-
* @param UriInterface|string $uri
58-
* @param array $headers
5938
* @return ResponseInterface
6039
*/
61-
public function get($uri, array $headers = [])
40+
public function send($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
6241
{
63-
$request = $this->createRequest(
64-
'GET',
65-
$uri,
66-
$headers
67-
);
42+
if (is_array($body)) {
43+
$body = http_build_query($body, '', '&');
44+
}
6845

69-
return $this->sendRequest($request);
70-
}
71-
72-
/**
73-
* Send a POST request.
74-
*
75-
* @param UriInterface|string $uri
76-
* @param array $headers
77-
* @param string|null|resource|StreamInterface $body
78-
* @return ResponseInterface
79-
*/
80-
public function post($uri, array $headers = [], $body = null)
81-
{
82-
$request = $this->createRequest(
83-
'POST',
84-
$uri,
85-
$headers,
86-
$body
87-
);
46+
$request = $this->createRequest($method, $uri, $headers, $body, $protocolVersion);
8847

8948
return $this->sendRequest($request);
9049
}
9150

9251
/**
93-
* Send a PUT request.
94-
*
95-
* @param UriInterface|string $uri
52+
* @param $method
53+
* @param $uri
9654
* @param array $headers
97-
* @param string|null|resource|StreamInterface $body
98-
* @return ResponseInterface
55+
* @param string|resource|StreamInterface|null $body
56+
* @param string $protocolVersion
57+
* @return RequestInterface
9958
*/
100-
public function put($uri, array $headers = [], $body = null)
59+
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
10160
{
102-
$request = $this->createRequest(
103-
'PUT',
104-
$uri,
105-
$headers,
106-
$body
107-
);
108-
109-
return $this->sendRequest($request);
61+
return $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
11062
}
11163

11264
/**
113-
* Send a PATCH request.
114-
*
115-
* @param UriInterface|string $uri
116-
* @param array $headers
117-
* @param string|null|resource|StreamInterface $body
65+
* @param RequestInterface $request
11866
* @return ResponseInterface
11967
*/
120-
public function patch($uri, array $headers = [], $body = null)
68+
public function sendRequest(RequestInterface $request)
12169
{
122-
$request = $this->createRequest(
123-
'PATCH',
124-
$uri,
125-
$headers,
126-
$body
127-
);
128-
129-
return $this->sendRequest($request);
70+
return $this->httpClient->sendRequest($request);
13071
}
13172

13273
/**
133-
* Send a DELETE request.
74+
* Send a GET request.
13475
*
13576
* @param UriInterface|string $uri
13677
* @param array $headers
137-
* @param string|null|resource|StreamInterface $body
13878
* @return ResponseInterface
13979
*/
140-
public function delete($uri, array $headers = [], $body = null)
80+
public function get($uri, array $headers = [])
14181
{
142-
$request = $this->createRequest(
143-
'DELETE',
144-
$uri,
145-
$headers,
146-
$body
147-
);
148-
149-
return $this->sendRequest($request);
82+
return $this->send('GET', $uri, $headers);
15083
}
15184

15285
/**
153-
* Send a HEAD request.
86+
* Send a POST request.
15487
*
15588
* @param UriInterface|string $uri
15689
* @param array $headers
90+
* @param string|array|null|resource|StreamInterface $body
15791
* @return ResponseInterface
15892
*/
159-
public function head($uri, array $headers = [])
160-
{
161-
$request = $this->createRequest(
162-
'HEAD',
163-
$uri,
164-
$headers
165-
);
166-
167-
return $this->sendRequest($request);
168-
}
169-
170-
/**
171-
* Send a OPTIONS request.
172-
*
173-
* @param UriInterface|string $uri
174-
* @return ResponseInterface
175-
*/
176-
public function options($uri)
93+
public function post($uri, array $headers = [], $body = null)
17794
{
178-
$request = $this->createRequest(
179-
'OPTIONS',
180-
$uri
181-
);
182-
183-
return $this->sendRequest($request);
95+
return $this->send('POST', $uri, $headers, $body);
18496
}
18597
}

src/Omnipay/Common/Http/Decoder.php renamed to src/Omnipay/Common/Http/ResponseParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Omnipay\Common\Exception\RuntimeException;
66
use Psr\Http\Message\ResponseInterface;
77

8-
class Decoder
8+
class ResponseParser
99
{
1010
/**
1111
* @param string|ResponseInterface $response
@@ -39,6 +39,7 @@ public static function json($response)
3939
}
4040
return $data === null ? [] : $data;
4141
}
42+
4243
/**
4344
* Parse the XML response body and return a \SimpleXMLElement.
4445
*
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Http;
4+
5+
use Mockery as m;
6+
use GuzzleHttp\Psr7\Request;
7+
use Http\Client\HttpClient;
8+
use Http\Message\RequestFactory;
9+
use Omnipay\Tests\TestCase;
10+
11+
class ClientTest extends TestCase
12+
{
13+
public function testEmptyConstruct()
14+
{
15+
$client = new Client();
16+
17+
$this->assertAttributeInstanceOf(HttpClient::class, 'httpClient', $client);
18+
$this->assertAttributeInstanceOf(RequestFactory::class, 'requestFactory', $client);
19+
}
20+
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+
33+
public function testSend()
34+
{
35+
$mockClient = m::mock(HttpClient::class);
36+
$mockFactory = m::mock(RequestFactory::class);
37+
$client = new Client($mockClient, $mockFactory);
38+
39+
$request = new Request('GET', '/path');
40+
41+
$mockFactory->shouldReceive('createRequest')->withArgs([
42+
'GET',
43+
'/path',
44+
[],
45+
null,
46+
'1.1',
47+
])->andReturn($request);
48+
49+
$mockClient->shouldReceive('sendRequest')->with($request)->once();
50+
51+
$client->send('GET', '/path');
52+
}
53+
54+
public function testSendParsesArrayBody()
55+
{
56+
$mockClient = m::mock(HttpClient::class);
57+
$mockFactory = m::mock(RequestFactory::class);
58+
$client = new Client($mockClient, $mockFactory);
59+
60+
$request = new Request('POST', '/path', [], 'a=1&b=2');
61+
62+
$mockFactory->shouldReceive('createRequest')->withArgs([
63+
'POST',
64+
'/path',
65+
[],
66+
'a=1&b=2',
67+
'1.1',
68+
])->andReturn($request);
69+
70+
$mockClient->shouldReceive('sendRequest')->with($request)->once();
71+
72+
$client->send('POST', '/path', [], ['a'=>'1', 'b'=>2]);
73+
}
74+
75+
76+
public function testGet()
77+
{
78+
$mockClient = m::mock(HttpClient::class);
79+
$mockFactory = m::mock(RequestFactory::class);
80+
$client = new Client($mockClient, $mockFactory);
81+
82+
$request = new Request('GET', '/path');
83+
84+
$mockFactory->shouldReceive('createRequest')->withArgs([
85+
'GET',
86+
'/path',
87+
[],
88+
null,
89+
'1.1',
90+
])->andReturn($request);
91+
92+
$mockClient->shouldReceive('sendRequest')->with($request)->once();
93+
94+
$client->get('/path');
95+
}
96+
97+
public function testPost()
98+
{
99+
$mockClient = m::mock(HttpClient::class);
100+
$mockFactory = m::mock(RequestFactory::class);
101+
$client = new Client($mockClient, $mockFactory);
102+
103+
$request = new Request('POST', '/path', [], 'a=b');
104+
105+
$mockFactory->shouldReceive('createRequest')->withArgs([
106+
'POST',
107+
'/path',
108+
[],
109+
'a=b',
110+
'1.1',
111+
])->andReturn($request);
112+
113+
$mockClient->shouldReceive('sendRequest')->with($request)->once();
114+
115+
$client->post('/path', [], ['a' => 'b']);
116+
}
117+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Http;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use Omnipay\Common\Exception\RuntimeException;
7+
use Omnipay\Tests\TestCase;
8+
9+
class ResponseParserTest extends TestCase
10+
{
11+
public function testParsesXmlString()
12+
{
13+
$data = ResponseParser::xml('<Foo><Baz>Bar</Baz></Foo>');
14+
15+
$this->assertInstanceOf('SimpleXMLElement', $data);
16+
$this->assertEquals('Bar', (string) $data->Baz);
17+
}
18+
19+
public function testParsesXmlResponse()
20+
{
21+
$response = new Response(200, [], '<Foo><Baz>Bar</Baz></Foo>');
22+
23+
$data = ResponseParser::xml($response);
24+
25+
$this->assertInstanceOf('SimpleXMLElement', $data);
26+
$this->assertEquals('Bar', (string) $data->Baz);
27+
}
28+
29+
public function testParsesXmlResponseException()
30+
{
31+
$this->expectException(RuntimeException::class);
32+
33+
$response = new Response(200, [], 'FooBar');
34+
35+
ResponseParser::xml($response);
36+
}
37+
38+
public function testParsesJsonString()
39+
{
40+
$data = ResponseParser::json('{"Baz":"Bar"}');
41+
42+
$this->assertEquals(array('Baz' => 'Bar'), $data);
43+
}
44+
45+
public function testParsesJsonResponse()
46+
{
47+
$response = new Response(200, [], '{"Baz":"Bar"}');
48+
49+
$data = ResponseParser::json($response);
50+
51+
$this->assertEquals(array('Baz' => 'Bar'), $data);
52+
}
53+
54+
public function testParsesJsonResponseException()
55+
{
56+
$this->expectException(RuntimeException::class);
57+
58+
$response = new Response(200, [], 'FooBar');
59+
60+
ResponseParser::json($response);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)