Skip to content

WIP: upgrade to HTTPlug 2.0 #29

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

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ cache:
- $HOME/.composer/cache/files

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
Expand All @@ -25,7 +22,7 @@ branches:
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"

before_install:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 2.0.0 - unreleased

### Changed

- Client::getLastRequest returns `null` instead of `false` when on requests have been recorded yet.

## 1.1.0 - 2018-01-08

### Added
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
}
],
"require": {
"php": "^5.5 || ^7.0",
"php-http/httplug": "^1.0",
"php-http/client-common": "^1.1 || ^2.0",
"php": "^7.1",
"php-http/httplug": "^2.0",
"php-http/client-common": "^2.0",
"php-http/discovery": "^1.0",
"php-http/message-factory": "^1.0"
},
Expand All @@ -36,7 +36,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.2-dev"
"dev-master": "2.0.x-dev"
}
},
"prefer-stable": true,
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_null_when_there_is_no_last_request()
{
$this->getLastRequest()->shouldReturn(null);
}
}
35 changes: 10 additions & 25 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
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 <[email protected]>
*/
Expand Down Expand Up @@ -54,9 +53,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 +61,7 @@ public function __construct(ResponseFactory $responseFactory = null)
/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request)
public function sendRequest(RequestInterface $request): ResponseInterface
{
$this->requests[] = $request;

Expand All @@ -91,8 +87,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 +97,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)
public function setDefaultException(?\Exception $defaultException)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

We should not allow someone doing $e->setDefaultException();

This change make sure you have to do $e->setDefaultException(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,10 +113,8 @@ 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)
public function setDefaultResponse(?ResponseInterface $defaultResponse)
{
$this->defaultResponse = $defaultResponse;
}
Expand All @@ -136,16 +124,13 @@ public function setDefaultResponse(ResponseInterface $defaultResponse = null)
*
* @return RequestInterface[]
*/
public function getRequests()
public function getRequests(): array
{
return $this->requests;
}

/**
* @return RequestInterface|false
*/
public function getLastRequest()
public function getLastRequest(): ?RequestInterface
{
return end($this->requests);
return end($this->requests) ?: null;
}
}