Skip to content
This repository was archived by the owner on Jan 16, 2018. It is now read-only.

adding utility HttpMethodsClient class #2

Merged
merged 1 commit into from
Oct 20, 2015
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
],
"require": {
"php": ">=5.4",
"php-http/client": "dev-utility_separation"
"php-http/client": "dev-utility_separation",
"php-http/message-factory": "dev-master@dev"
},
"require-dev": {
"phpspec/phpspec": "^2.2",
Expand Down
27 changes: 12 additions & 15 deletions spec/HttpMethodsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,75 @@

namespace spec\Http\Client\Util;

use Http\Client\Util\HttpMethods;
use Http\Client\HttpMethodsClient;
use Http\Client\Util\HttpMethodsClient;
use PhpSpec\ObjectBehavior;

class HttpMethodsSpec extends ObjectBehavior
{
function let()
{
$this->beAnInstanceOf('spec\Http\Client\HttpMethodsStub');
$this->beAnInstanceOf('spec\Http\Client\Util\HttpMethodsClientStub');
}

function it_sends_a_get_request()
{
$data = HttpMethodsStub::$requestData;
$data = HttpMethodsClientStub::$requestData;

$this->get($data['uri'], $data['headers'], $data['options'])->shouldReturn(true);
}

function it_sends_a_head_request()
{
$data = HttpMethodsStub::$requestData;
$data = HttpMethodsClientStub::$requestData;

$this->head($data['uri'], $data['headers'], $data['options'])->shouldReturn(true);
}

function it_sends_a_trace_request()
{
$data = HttpMethodsStub::$requestData;
$data = HttpMethodsClientStub::$requestData;

$this->trace($data['uri'], $data['headers'], $data['options'])->shouldReturn(true);
}

function it_sends_a_post_request()
{
$data = HttpMethodsStub::$requestData;
$data = HttpMethodsClientStub::$requestData;

$this->post($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
}

function it_sends_a_put_request()
{
$data = HttpMethodsStub::$requestData;
$data = HttpMethodsClientStub::$requestData;

$this->put($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
}

function it_sends_a_patch_request()
{
$data = HttpMethodsStub::$requestData;
$data = HttpMethodsClientStub::$requestData;

$this->patch($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
}

function it_sends_a_delete_request()
{
$data = HttpMethodsStub::$requestData;
$data = HttpMethodsClientStub::$requestData;

$this->delete($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
}

function it_sends_a_options_request()
{
$data = HttpMethodsStub::$requestData;
$data = HttpMethodsClientStub::$requestData;

$this->options($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
}
}

class HttpMethodsStub implements HttpMethodsClient
class HttpMethodsClientStub extends HttpMethodsClient
{
use HttpMethods;

public static $requestData = [
'uri' => '/uri',
'headers' => [
Expand All @@ -88,7 +85,7 @@ class HttpMethodsStub implements HttpMethodsClient
/**
* {@inheritdoc}
*/
public function send($method, $uri, array $headers = [], $body = null, array $options = [])
protected function send($method, $uri, array $headers = [], $body = null, array $options = [])
{
if (in_array($method, ['GET', 'HEAD', 'TRACE'])) {
return $uri === self::$requestData['uri'] &&
Expand Down
82 changes: 0 additions & 82 deletions src/HttpMethods.php

This file was deleted.

195 changes: 195 additions & 0 deletions src/HttpMethodsClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace Http\Client\Util;

use Http\Client\Exception;
use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;

/**
* Convenience HTTP client that integrates the MessageFactory in order to send
* requests in the form of
*
* $client
* ->get('/foo')
* ->post('/bar')
* ;
*
* @author Márk Sági-Kazár <[email protected]>
* @author David Buchmann <[email protected]>
*/
class HttpMethodsClient
{
/**
* @var HttpClient
*/
private $httpClient;

/**
* @var MessageFactory
*/
private $messageFactory;

/**
* Instantiate the message client with a client and a message factory.
*
* @param HttpClient $httpClient The client to send requests with.
* @param MessageFactory $messageFactory The message factory to create requests.
*/
public function __construct(HttpClient $httpClient, MessageFactory $messageFactory)
{
$this->httpClient = $httpClient;
$this->messageFactory = $messageFactory;
}

/**
* Sends a GET request
*
* @param string|UriInterface $uri
* @param array $headers
*
* @throws Exception
*
* @return ResponseInterface
*/
public function get($uri, array $headers = [])
{
return $this->send('GET', $uri, $headers, null);
}

/**
* Sends an HEAD request
*
* @param string|UriInterface $uri
* @param array $headers
*
* @throws Exception
*
* @return ResponseInterface
*/
public function head($uri, array $headers = [])
{
return $this->send('HEAD', $uri, $headers, null);
}

/**
* Sends a TRACE request
*
* @param string|UriInterface $uri
* @param array $headers
*
* @throws Exception
*
* @return ResponseInterface
*/
public function trace($uri, array $headers = [])
{
return $this->send('TRACE', $uri, $headers, null);
}

/**
* Sends a POST request
*
* @param string|UriInterface $uri
* @param array $headers
* @param string|StreamInterface|null $body
*
* @throws Exception
*
* @return ResponseInterface
*/
public function post($uri, array $headers = [], $body = null)
{
return $this->send('POST', $uri, $headers, $body);
}

/**
* Sends a PUT request
*
* @param string|UriInterface $uri
* @param array $headers
* @param string|StreamInterface|null $body
*
* @throws Exception
*
* @return ResponseInterface
*/
public function put($uri, array $headers = [], $body = null)
{
return $this->send('PUT', $uri, $headers, $body);
}

/**
* Sends a PATCH request
*
* @param string|UriInterface $uri
* @param array $headers
* @param string|StreamInterface|null $body
*
* @throws Exception
*
* @return ResponseInterface
*/
public function patch($uri, array $headers = [], $body = null)
{
return $this->send('PATCH', $uri, $headers, $body);
}

/**
* Sends a DELETE request
*
* @param string|UriInterface $uri
* @param array $headers
* @param string|StreamInterface|null $body
*
* @throws Exception
*
* @return ResponseInterface
*/
public function delete($uri, array $headers = [], $body = null)
{
return $this->send('DELETE', $uri, $headers, $body);
}

/**
* Sends an OPTIONS request
*
* @param string|UriInterface $uri
* @param array $headers
* @param string|StreamInterface|null $body
*
* @throws Exception
*
* @return ResponseInterface
*/
public function options($uri, array $headers = [], $body = null)
{
return $this->send('OPTIONS', $uri, $headers, $body);
}

/**
* Sends a request
*
* @param string $method
* @param string|UriInterface $uri
* @param array $headers
* @param string|StreamInterface|null $body
*
* @throws Exception
*
* @return ResponseInterface
*/
protected function send($method, $uri, array $headers = [], $body = null)
{
return $this->httpClient->sendRequest($this->messageFactory->createRequest(
$method,
$uri,
'1.1',
$headers,
$body
));
}
}