Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c0c7d2

Browse files
Merge pull request #4 from gpolverini/master
Add ClientTrait
2 parents 4e71d1a + 48ff11a commit 9c0c7d2

15 files changed

+166
-25
lines changed

‎src/Client.php‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,30 @@ public function sendRequest(RequestInterface $request)
157157
curl_setopt($this->handle, CURLOPT_URL, (string) $request->getUri());
158158
curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, [$this, 'headerFunction']);
159159

160-
$method = $request->getMethod();
160+
foreach ($request->getHeaders() as $name => $values) {
161+
$headers[] = $name . ': ' . implode(', ', $values);
162+
}
163+
if (isset($headers)) {
164+
curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
165+
}
161166

162-
if ($method == HttpMethod::HEAD) {
163-
curl_setopt($this->handle, CURLOPT_NOBODY, true);
164-
} elseif ($method == HttpMethod::POST) {
165-
curl_setopt($this->handle, CURLOPT_POST, true);
166-
$body = $request->getBody();
167-
if (!empty($body)) {
168-
curl_setopt($this->handle, CURLOPT_POSTFIELDS, $body);
169-
}
170-
} elseif (in_array($method, [HttpMethod::PUT, HttpMethod::PATCH, HttpMethod::DELETE])) {
171-
curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
167+
$method = $request->getMethod();
168+
switch($method) {
169+
case HttpMethod::HEAD:
170+
curl_setopt($this->handle, CURLOPT_NOBODY, true);
171+
break;
172+
case HttpMethod::POST:
173+
curl_setopt($this->handle, CURLOPT_POST, true);
174+
$body = $request->getBody();
175+
if (!empty($body)) {
176+
curl_setopt($this->handle, CURLOPT_POSTFIELDS, $body);
177+
}
178+
break;
179+
case HttpMethod::PUT:
180+
case HttpMethod::PATCH:
181+
case HttpMethod::DELETE:
182+
curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
183+
break;
172184
}
173185

174186
$ret = curl_exec($this->handle);

‎src/ClientAwareTrait.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
Copyright (c) 2017 Jorge Matricali <jorgematricali@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
*/
23+
24+
namespace Matricali\Http;
25+
26+
use Matricali\Http\ClientInterface as HttpClientInterface;
27+
28+
/**
29+
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
30+
*/
31+
trait ClientAwareTrait
32+
{
33+
/**
34+
* The client instance.
35+
*
36+
* @var HttpClientInterface
37+
*/
38+
protected $httpClient;
39+
40+
/**
41+
* setHttpClient.
42+
*
43+
* @param HttpClientInterface $httpClient
44+
*/
45+
public function setHttpClient(HttpClientInterface $httpClient)
46+
{
47+
$this->httpClient = $httpClient;
48+
}
49+
}

‎src/HttpMethod.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace Matricali\Http;
2525

2626
/**
27-
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
27+
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
2828
*/
2929
abstract class HttpMethod extends Enum
3030
{

‎src/HttpStatusCode.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace Matricali\Http;
2525

2626
/**
27-
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
27+
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
2828
*/
2929
abstract class HttpStatusCode extends Enum
3030
{

‎src/Request.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ class Request extends AbstractMessage implements RequestInterface
4444
* @param string $body Body to send in the request.-
4545
* @throws \InvalidArgumentException for invalid HTTP methods.
4646
*/
47-
public function __construct($method = HttpMethod::GET, $uri = '', array $headers = array(), $body = null)
48-
{
47+
public function __construct(
48+
$method = HttpMethod::GET,
49+
$uri = '',
50+
array $headers = array(),
51+
$body = null
52+
) {
4953
$this->validateMethod($method);
5054
parent::__construct('1.1', $headers, $body);
5155
$this->method = $method;

‎src/Response.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ class Response extends AbstractMessage implements ResponseInterface
5454
* @param string $protocolVersion HTTP protocol version
5555
* @throws \InvalidArgumentException for invalid HTTP methods.
5656
*/
57-
public function __construct($body = '', $statusCode = 200, array $headers = array(), $protocolVersion = '1.1')
58-
{
57+
public function __construct(
58+
$body = '',
59+
$statusCode = HttpStatusCode::HTTP_OK,
60+
array $headers = array(),
61+
$protocolVersion = '1.1'
62+
) {
5963
$this->validateStatusCode($statusCode);
6064
parent::__construct($protocolVersion, $headers, $body);
6165
$this->statusCode = $statusCode;

‎src/Scheme.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace Matricali\Http;
2525

2626
/**
27-
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
27+
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
2828
*/
2929
abstract class Scheme extends Enum
3030
{

‎tests/ClientAwareTraitTest.php‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/*
3+
Copyright (c) 2017 Jorge Matricali <jorgematricali@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
*/
23+
24+
namespace Matricali\Http;
25+
26+
use PHPUnit\Framework\TestCase;
27+
use Prophecy\Argument;
28+
29+
/**
30+
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
31+
*
32+
* @group Traits
33+
*/
34+
class ClientAwareTraitTest extends TestCase
35+
{
36+
protected $trait;
37+
38+
public function setUp()
39+
{
40+
$this->trait = $this->getMockForTrait('Matricali\Http\ClientAwareTrait');
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function testSetHttpClient()
47+
{
48+
$httpClient = new Client();
49+
$this->assertNull($this->trait->setHttpClient($httpClient));
50+
$reflection = new \ReflectionProperty(get_class($this->trait), 'httpClient');
51+
$reflection->setAccessible(true);
52+
53+
$this->assertInstanceOf('Matricali\Http\ClientInterface', $reflection->getValue($this->trait));
54+
}
55+
}

‎tests/ClientTest.php‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
use Prophecy\Argument;
2828

2929
/**
30-
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
30+
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
3131
*
3232
* @group Client
3333
*/
@@ -44,6 +44,7 @@ public function testSendRequest()
4444
$request = $this->prophesize('Psr\Http\Message\RequestInterface');
4545
$request->getUri()->willReturn('http://404.php.net/');
4646
$request->getMethod()->willReturn(HttpMethod::GET);
47+
$request->getHeaders()->willReturn([]);
4748
$client->sendRequest($request->reveal());
4849
}
4950

@@ -156,4 +157,20 @@ public function testParseHeaderNotArray()
156157

157158
$this->assertFalse($method->invoke($client, 123));
158159
}
160+
161+
/**
162+
* @test
163+
*/
164+
public function testBasicPostWithHeaders()
165+
{
166+
$client = new Client();
167+
$headers = [
168+
'Accept' => 'application/json',
169+
'Content-Type' => 'text/xml;charset=UTF-8',
170+
];
171+
$response = $client->post('http://www.google.com/', 'test=test&a=b', $headers);
172+
$this->assertEquals(405, $response->getStatusCode());
173+
$this->assertEquals('1.1', $response->getProtocolVersion());
174+
$this->assertNotEmpty($response->getBody());
175+
}
159176
}

‎tests/HttpMethodTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use PHPUnit\Framework\TestCase;
2727

2828
/**
29-
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
29+
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
3030
*
3131
* @group Enums
3232
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /