Skip to content

Support HTTPlug 2.0 #33

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 6 commits into from
Dec 30, 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
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ env:

branches:
except:
- /^analysis-.*$/
- /^patch-.*$/

matrix:
fast_finish: true
include:
- php: 5.5
- php: 7.1
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
- php: 5.5
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"

before_install:
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## 1.2.0 - unreleased

### Added

- Support for HTTPlug 2.0.
- Support for php-http/client-common 2.0.

## 1.1.0 - 2018-01-08

### Added
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
],
"require": {
"php": "^5.5 || ^7.0",
"php-http/httplug": "^1.0",
"php-http/client-common": "^1.1 || ^2.0",
"php-http/httplug": "^1.0 || ^2.0",
"php-http/client-common": "^1.9 || ^2.0",
"php-http/discovery": "^1.0",
"php-http/message-factory": "^1.0"
},
Expand Down
19 changes: 15 additions & 4 deletions spec/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace spec\Http\Mock;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\ResponseFactory;
use Http\Mock\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
Expand All @@ -16,17 +19,17 @@ function let(ResponseFactory $responseFactory)

function it_is_initializable()
{
$this->shouldHaveType('Http\Mock\Client');
$this->shouldHaveType(Client::class);
}

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

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

function it_returns_a_response_for_a_request(RequestInterface $request, ResponseInterface $response)
Expand Down Expand Up @@ -67,10 +70,18 @@ function it_creates_an_empty_response_when_none_is_added(
$this->sendRequest($request)->shouldReturn($response);
}

function it_returns_the_last_request(RequestInterface $request)
function it_returns_the_last_request(RequestInterface $request, ResponseInterface $response)
{
// we need to set something that sendRequest can return.
$this->addResponse($response);

$this->sendRequest($request);

$this->getLastRequest()->shouldReturn($request);
}

function it_returns_false_when_there_is_no_last_request()
{
$this->getLastRequest()->shouldReturn(false);
}
}
27 changes: 7 additions & 20 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Http\Mock;

use Http\Client\Common\HttpAsyncClientEmulator;
use Http\Client\Common\VersionBridgeClient;
use Http\Client\Exception;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
Expand All @@ -12,17 +13,17 @@
use Psr\Http\Message\ResponseInterface;

/**
* HTTP client mock.
* An implementation of the HTTP client that is useful for automated tests.
*
* This mock is most useful in tests. It does not send requests but stores them
* for later retrieval. Additionally, you can set an exception to test
* exception handling.
* This mock does not send requests but stores them for later retrieval.
* You can configure the mock with responses to return and/or exceptions to throw.
*
* @author David de Boer <david@ddeboer.nl>
*/
class Client implements HttpClient, HttpAsyncClient
{
use HttpAsyncClientEmulator;
use VersionBridgeClient;

/**
* @var ResponseFactory
Expand Down Expand Up @@ -54,9 +55,6 @@ class Client implements HttpClient, HttpAsyncClient
*/
private $defaultException;

/**
* @param ResponseFactory|null $responseFactory
*/
public function __construct(ResponseFactory $responseFactory = null)
{
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
Expand All @@ -65,7 +63,7 @@ public function __construct(ResponseFactory $responseFactory = null)
/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request)
public function doSendRequest(RequestInterface $request)
{
$this->requests[] = $request;

Expand All @@ -91,8 +89,6 @@ public function sendRequest(RequestInterface $request)

/**
* Adds an exception that will be thrown.
*
* @param \Exception $exception
*/
public function addException(\Exception $exception)
{
Expand All @@ -103,18 +99,14 @@ public function addException(\Exception $exception)
* Sets the default exception to throw when the list of added exceptions and responses is exhausted.
*
* If both a default exception and a default response are set, the exception will be thrown.
*
* @param \Exception|null $defaultException
*/
public function setDefaultException(\Exception $defaultException = null)
{
$this->defaultException = $defaultException;
}

/**
* Adds a response that will be returned.
*
* @param ResponseInterface $response
* Adds a response that will be returned in first in first out order.
*/
public function addResponse(ResponseInterface $response)
{
Expand All @@ -123,8 +115,6 @@ public function addResponse(ResponseInterface $response)

/**
* Sets the default response to be returned when the list of added exceptions and responses is exhausted.
*
* @param ResponseInterface|null $defaultResponse
*/
public function setDefaultResponse(ResponseInterface $defaultResponse = null)
{
Expand All @@ -141,9 +131,6 @@ public function getRequests()
return $this->requests;
}

/**
* @return RequestInterface|false
*/
public function getLastRequest()
{
return end($this->requests);
Expand Down