Skip to content

Add client decorators and emulators #1

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 1 commit into from
Dec 24, 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 @@ -13,7 +13,8 @@
"require": {
"php": ">=5.4",
"php-http/httplug": "1.0.0-beta",
"php-http/message-factory": "^1.0"
"php-http/message-factory": "^1.0",
"php-http/promise": "^0.1.1"
},
"require-dev": {
"phpspec/phpspec": "^2.4",
Expand Down
2 changes: 1 addition & 1 deletion spec/BatchClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace spec\Http\Client\Common;

use Http\Client\HttpClient;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class BatchClientSpec extends ObjectBehavior
{
Expand Down
47 changes: 47 additions & 0 deletions spec/HttpAsyncClientDecoratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace spec\Http\Client\Common;

use Http\Client\HttpAsyncClient;
use Http\Client\Common\HttpAsyncClientDecorator;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use PhpSpec\ObjectBehavior;

class HttpAsyncClientDecoratorSpec extends ObjectBehavior
{
function let(HttpAsyncClient $httpAsyncClient)
{
$this->beAnInstanceOf('spec\Http\Client\Common\HttpAsyncClientDecoratorStub', [$httpAsyncClient]);
}

function it_is_initializable()
{
$this->shouldHaveType('spec\Http\Client\Common\HttpAsyncClientDecoratorStub');
}

function it_is_an_http_async_client()
{
$this->shouldImplement('Http\Client\HttpAsyncClient');
}

function it_decorates_the_underlying_client(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise)
{
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);

$this->sendAsyncRequest($request)->shouldReturn($promise);
}
}

class HttpAsyncClientDecoratorStub implements HttpAsyncClient
{
use HttpAsyncClientDecorator;

/**
* @param HttpAsyncClient $httpAsyncClient
*/
public function __construct(HttpAsyncClient $httpAsyncClient)
{
$this->httpAsyncClient = $httpAsyncClient;
}
}
52 changes: 52 additions & 0 deletions spec/HttpAsyncClientEmulatorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace spec\Http\Client\Common;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Client\Common\HttpAsyncClientEmulator;
use Http\Client\Common\HttpClientDecorator;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class HttpAsyncClientEmulatorSpec extends ObjectBehavior
{
function let(HttpClient $httpClient)
{
$this->beAnInstanceOf('spec\Http\Client\Common\HttpAsyncClientEmulatorStub', [$httpClient]);
}

function it_is_initializable()
{
$this->shouldHaveType('spec\Http\Client\Common\HttpAsyncClientEmulatorStub');
}

function it_emulates_a_successful_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response)
{
$httpClient->sendRequest($request)->willReturn($response);

$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise');
}

function it_emulates_a_failed_request(HttpClient $httpClient, RequestInterface $request)
{
$httpClient->sendRequest($request)->willThrow('Http\Client\Exception\TransferException');

$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
}
}

class HttpAsyncClientEmulatorStub implements HttpClient, HttpAsyncClient
{
use HttpClientDecorator;
use HttpAsyncClientEmulator;

/**
* @param HttpClient $httpClient
*/
public function __construct(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}
}
47 changes: 47 additions & 0 deletions spec/HttpClientDecoratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace spec\Http\Client\Common;

use Http\Client\HttpClient;
use Http\Client\Common\HttpClientDecorator;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class HttpClientDecoratorSpec extends ObjectBehavior
{
function let(HttpClient $httpClient)
{
$this->beAnInstanceOf('spec\Http\Client\Common\HttpClientDecoratorStub', [$httpClient]);
}

function it_is_initializable()
{
$this->shouldHaveType('spec\Http\Client\Common\HttpClientDecoratorStub');
}

function it_is_an_http_client()
{
$this->shouldImplement('Http\Client\HttpClient');
}

function it_decorates_the_underlying_client(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response)
{
$httpClient->sendRequest($request)->willReturn($response);

$this->sendRequest($request)->shouldReturn($response);
}
}

class HttpClientDecoratorStub implements HttpClient
{
use HttpClientDecorator;

/**
* @param HttpClient $httpClient
*/
public function __construct(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}
}
62 changes: 62 additions & 0 deletions spec/HttpClientEmulatorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace spec\Http\Client\Common;

use Http\Client\Exception\TransferException;
use Http\Client\HttpClient;
use Http\Client\HttpAsyncClient;
use Http\Client\Common\HttpClientEmulator;
use Http\Client\Common\HttpAsyncClientDecorator;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class HttpClientEmulatorSpec extends ObjectBehavior
{
function let(HttpAsyncClient $httpAsyncClient)
{
$this->beAnInstanceOf('spec\Http\Client\Common\HttpClientEmulatorStub', [$httpAsyncClient]);
}

function it_is_initializable()
{
$this->shouldHaveType('spec\Http\Client\Common\HttpClientEmulatorStub');
}

function it_emulates_a_successful_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise, ResponseInterface $response)
{
$promise->wait()->shouldBeCalled();
$promise->getState()->willReturn(Promise::FULFILLED);
$promise->wait()->willReturn($response);

$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);

$this->sendRequest($request)->shouldReturn($response);
}

function it_emulates_a_failed_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise)
{
$promise->wait()->shouldBeCalled();
$promise->getState()->willReturn(Promise::REJECTED);
$promise->wait()->willThrow(new TransferException());

$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);

$this->shouldThrow('Http\Client\Exception')->duringSendRequest($request);
}
}

class HttpClientEmulatorStub implements HttpAsyncClient, HttpClient
{
use HttpAsyncClientDecorator;
use HttpClientEmulator;

/**
* @param HttpAsyncClient $httpAsyncClient
*/
public function __construct(HttpAsyncClient $httpAsyncClient)
{
$this->httpAsyncClient = $httpAsyncClient;
}
}
2 changes: 1 addition & 1 deletion spec/HttpMethodsClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Http\Client\HttpClient;
use Http\Client\Common\HttpMethodsClient;
use Http\Message\MessageFactory;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class HttpMethodsClientSpec extends ObjectBehavior
{
Expand Down
29 changes: 29 additions & 0 deletions src/HttpAsyncClientDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Http\Client\Common;

use Http\Client\HttpAsyncClient;
use Psr\Http\Message\RequestInterface;

/**
* Decorates an HTTP Async Client.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
trait HttpAsyncClientDecorator
{
/**
* @var HttpAsyncClient
*/
protected $httpAsyncClient;

/**
* {@inheritdoc}
*
* @see HttpAsyncClient::sendAsyncRequest
*/
public function sendAsyncRequest(RequestInterface $request)
{
return $this->httpAsyncClient->sendAsyncRequest($request);
}
}
36 changes: 36 additions & 0 deletions src/HttpAsyncClientEmulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Http\Client\Common;

use Http\Client\Exception;
use Http\Promise;
use Psr\Http\Message\RequestInterface;

/**
* Emulates an HTTP Async Client in an HTTP Client.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
trait HttpAsyncClientEmulator
{
/**
* {@inheritdoc}
*
* @see HttpClient::sendRequest
*/
abstract public function sendRequest(RequestInterface $request);

/**
* {@inheritdoc}
*
* @see HttpAsyncClient::sendAsyncRequest
*/
public function sendAsyncRequest(RequestInterface $request)
{
try {
return new Promise\FulfilledPromise($this->sendRequest($request));
} catch (Exception $e) {
return new Promise\RejectedPromise($e);
}
}
}
29 changes: 29 additions & 0 deletions src/HttpClientDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Http\Client\Common;

use Http\Client\HttpClient;
use Psr\Http\Message\RequestInterface;

/**
* Decorates an HTTP Client.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
trait HttpClientDecorator
{
/**
* @var HttpClient
*/
protected $httpClient;

/**
* {@inheritdoc}
*
* @see HttpClient::sendRequest
*/
public function sendRequest(RequestInterface $request)
{
return $this->httpClient->sendRequest($request);
}
}
32 changes: 32 additions & 0 deletions src/HttpClientEmulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Http\Client\Common;

use Psr\Http\Message\RequestInterface;

/**
* Emulates an HTTP Client in an HTTP Async Client.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
trait HttpClientEmulator
{
/**
* {@inheritdoc}
*
* @see HttpClient::sendRequest
*/
public function sendRequest(RequestInterface $request)
{
$promise = $this->sendAsyncRequest($request);

return $promise->wait();
}

/**
* {@inheritdoc}
*
* @see HttpAsyncClient::sendAsyncRequest
*/
abstract public function sendAsyncRequest(RequestInterface $request);
}