Skip to content

Add ClientTrait #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,30 @@ public function sendRequest(RequestInterface $request)
curl_setopt($this->handle, CURLOPT_URL, (string) $request->getUri());
curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, [$this, 'headerFunction']);

$method = $request->getMethod();
foreach ($request->getHeaders() as $name => $values) {
$headers[] = $name . ': ' . implode(', ', $values);
}
if (isset($headers)) {
curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
}

if ($method == HttpMethod::HEAD) {
curl_setopt($this->handle, CURLOPT_NOBODY, true);
} elseif ($method == HttpMethod::POST) {
curl_setopt($this->handle, CURLOPT_POST, true);
$body = $request->getBody();
if (!empty($body)) {
curl_setopt($this->handle, CURLOPT_POSTFIELDS, $body);
}
} elseif (in_array($method, [HttpMethod::PUT, HttpMethod::PATCH, HttpMethod::DELETE])) {
curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
$method = $request->getMethod();
switch($method) {
case HttpMethod::HEAD:
curl_setopt($this->handle, CURLOPT_NOBODY, true);
break;
case HttpMethod::POST:
curl_setopt($this->handle, CURLOPT_POST, true);
$body = $request->getBody();
if (!empty($body)) {
curl_setopt($this->handle, CURLOPT_POSTFIELDS, $body);
}
break;
case HttpMethod::PUT:
case HttpMethod::PATCH:
case HttpMethod::DELETE:
curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
break;
}

$ret = curl_exec($this->handle);
Expand Down
49 changes: 49 additions & 0 deletions src/ClientAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/*
Copyright (c) 2017 Jorge Matricali <jorgematricali@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

namespace Matricali\Http;

use Matricali\Http\ClientInterface as HttpClientInterface;

/**
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*/
trait ClientAwareTrait
{
/**
* The client instance.
*
* @var HttpClientInterface
*/
protected $httpClient;

/**
* setHttpClient.
*
* @param HttpClientInterface $httpClient
*/
public function setHttpClient(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
}
2 changes: 1 addition & 1 deletion src/HttpMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Matricali\Http;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*/
abstract class HttpMethod extends Enum
{
Expand Down
2 changes: 1 addition & 1 deletion src/HttpStatusCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Matricali\Http;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*/
abstract class HttpStatusCode extends Enum
{
Expand Down
8 changes: 6 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ class Request extends AbstractMessage implements RequestInterface
* @param string $body Body to send in the request.-
* @throws \InvalidArgumentException for invalid HTTP methods.
*/
public function __construct($method = HttpMethod::GET, $uri = '', array $headers = array(), $body = null)
{
public function __construct(
$method = HttpMethod::GET,
$uri = '',
array $headers = array(),
$body = null
) {
$this->validateMethod($method);
parent::__construct('1.1', $headers, $body);
$this->method = $method;
Expand Down
8 changes: 6 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ class Response extends AbstractMessage implements ResponseInterface
* @param string $protocolVersion HTTP protocol version
* @throws \InvalidArgumentException for invalid HTTP methods.
*/
public function __construct($body = '', $statusCode = 200, array $headers = array(), $protocolVersion = '1.1')
{
public function __construct(
$body = '',
$statusCode = HttpStatusCode::HTTP_OK,
array $headers = array(),
$protocolVersion = '1.1'
) {
$this->validateStatusCode($statusCode);
parent::__construct($protocolVersion, $headers, $body);
$this->statusCode = $statusCode;
Expand Down
2 changes: 1 addition & 1 deletion src/Scheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Matricali\Http;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*/
abstract class Scheme extends Enum
{
Expand Down
55 changes: 55 additions & 0 deletions tests/ClientAwareTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
Copyright (c) 2017 Jorge Matricali <jorgematricali@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

namespace Matricali\Http;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;

/**
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*
* @group Traits
*/
class ClientAwareTraitTest extends TestCase
{
protected $trait;

public function setUp()
{
$this->trait = $this->getMockForTrait('Matricali\Http\ClientAwareTrait');
}

/**
* @test
*/
public function testSetHttpClient()
{
$httpClient = new Client();
$this->assertNull($this->trait->setHttpClient($httpClient));
$reflection = new \ReflectionProperty(get_class($this->trait), 'httpClient');
$reflection->setAccessible(true);

$this->assertInstanceOf('Matricali\Http\ClientInterface', $reflection->getValue($this->trait));
}
}
19 changes: 18 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Prophecy\Argument;

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

Expand Down Expand Up @@ -156,4 +157,20 @@ public function testParseHeaderNotArray()

$this->assertFalse($method->invoke($client, 123));
}

/**
* @test
*/
public function testBasicPostWithHeaders()
{
$client = new Client();
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'text/xml;charset=UTF-8',
];
$response = $client->post('http://www.google.com/', 'test=test&a=b', $headers);
$this->assertEquals(405, $response->getStatusCode());
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertNotEmpty($response->getBody());
}
}
2 changes: 1 addition & 1 deletion tests/HttpMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use PHPUnit\Framework\TestCase;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*
* @group Enums
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/HttpStatusCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use PHPUnit\Framework\TestCase;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*
* @group Enums
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Prophecy\Argument;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*
* @group Request
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use PHPUnit\Framework\TestCase;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*
* @group Response
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/SchemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use PHPUnit\Framework\TestCase;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*
* @group Enums
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use PHPUnit\Framework\TestCase;

/**
* @author Gabriel Polverini <gpolverini_ext@amco.mx>
* @author Gabriel Polverini <polverini.gabriel@gmail.com>
*
* @group Uri
*/
Expand Down