Skip to content

Add emulated clients #2

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 26, 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log


## Unreleased

### Added

- Emulated clients


## 0.1.0 - 2015-12-25

### Added
Expand Down
58 changes: 58 additions & 0 deletions spec/EmulatedHttpAsyncClientSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace spec\Http\Client\Common;

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

class EmulatedHttpAsyncClientSpec extends ObjectBehavior
{
function let(HttpClient $httpClient)
{
$this->beConstructedWith($httpClient);
}

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

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

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

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');
}

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

$this->sendRequest($request)->shouldReturn($response);
}
}
42 changes: 25 additions & 17 deletions spec/HttpClientEmulatorSpec.php → spec/EmulatedHttpClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,39 @@
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
class EmulatedHttpClientSpec extends ObjectBehavior
{
function let(HttpAsyncClient $httpAsyncClient)
{
$this->beAnInstanceOf('spec\Http\Client\Common\HttpClientEmulatorStub', [$httpAsyncClient]);
$this->beConstructedWith($httpAsyncClient);
}

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

function it_emulates_a_successful_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise, ResponseInterface $response)
function it_is_an_http_client()
{
$this->shouldImplement('Http\Client\HttpClient');
}

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

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);
Expand All @@ -45,18 +57,14 @@ function it_emulates_a_failed_request(HttpAsyncClient $httpAsyncClient, RequestI

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

class HttpClientEmulatorStub implements HttpAsyncClient, HttpClient
{
use HttpAsyncClientDecorator;
use HttpClientEmulator;
function it_decorates_the_underlying_client(
HttpAsyncClient $httpAsyncClient,
RequestInterface $request,
Promise $promise
) {
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);

/**
* @param HttpAsyncClient $httpAsyncClient
*/
public function __construct(HttpAsyncClient $httpAsyncClient)
{
$this->httpAsyncClient = $httpAsyncClient;
$this->sendAsyncRequest($request)->shouldReturn($promise);
}
}
47 changes: 0 additions & 47 deletions spec/HttpAsyncClientDecoratorSpec.php

This file was deleted.

52 changes: 0 additions & 52 deletions spec/HttpAsyncClientEmulatorSpec.php

This file was deleted.

47 changes: 0 additions & 47 deletions spec/HttpClientDecoratorSpec.php

This file was deleted.

Loading