Skip to content

Commit 04d4829

Browse files
Nyholmdbu
authored andcommitted
Support HTTPlug 2.0 (#33)
1 parent 5d00afe commit 04d4829

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

.travis.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ env:
2020

2121
branches:
2222
except:
23-
- /^analysis-.*$/
23+
- /^patch-.*$/
2424

2525
matrix:
2626
fast_finish: true
2727
include:
28-
- php: 5.5
28+
- php: 7.1
2929
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
30+
- php: 5.5
31+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
3032

3133
before_install:
3234
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## 1.2.0 - unreleased
4+
5+
### Added
6+
7+
- Support for HTTPlug 2.0.
8+
- Support for php-http/client-common 2.0.
9+
310
## 1.1.0 - 2018-01-08
411

512
### Added

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
],
1313
"require": {
1414
"php": "^5.5 || ^7.0",
15-
"php-http/httplug": "^1.0",
16-
"php-http/client-common": "^1.1 || ^2.0",
15+
"php-http/httplug": "^1.0 || ^2.0",
16+
"php-http/client-common": "^1.9 || ^2.0",
1717
"php-http/discovery": "^1.0",
1818
"php-http/message-factory": "^1.0"
1919
},

spec/ClientSpec.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace spec\Http\Mock;
44

5+
use Http\Client\HttpAsyncClient;
6+
use Http\Client\HttpClient;
57
use Http\Message\ResponseFactory;
8+
use Http\Mock\Client;
69
use Psr\Http\Message\RequestInterface;
710
use Psr\Http\Message\ResponseInterface;
811
use PhpSpec\ObjectBehavior;
@@ -16,17 +19,17 @@ function let(ResponseFactory $responseFactory)
1619

1720
function it_is_initializable()
1821
{
19-
$this->shouldHaveType('Http\Mock\Client');
22+
$this->shouldHaveType(Client::class);
2023
}
2124

2225
function it_is_an_http_client()
2326
{
24-
$this->shouldImplement('Http\Client\HttpClient');
27+
$this->shouldImplement(HttpClient::class);
2528
}
2629

2730
function it_is_an_async_http_client()
2831
{
29-
$this->shouldImplement('Http\Client\HttpAsyncClient');
32+
$this->shouldImplement(HttpAsyncClient::class);
3033
}
3134

3235
function it_returns_a_response_for_a_request(RequestInterface $request, ResponseInterface $response)
@@ -67,10 +70,18 @@ function it_creates_an_empty_response_when_none_is_added(
6770
$this->sendRequest($request)->shouldReturn($response);
6871
}
6972

70-
function it_returns_the_last_request(RequestInterface $request)
73+
function it_returns_the_last_request(RequestInterface $request, ResponseInterface $response)
7174
{
75+
// we need to set something that sendRequest can return.
76+
$this->addResponse($response);
77+
7278
$this->sendRequest($request);
7379

7480
$this->getLastRequest()->shouldReturn($request);
7581
}
82+
83+
function it_returns_false_when_there_is_no_last_request()
84+
{
85+
$this->getLastRequest()->shouldReturn(false);
86+
}
7687
}

src/Client.php

+7-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\Mock;
44

55
use Http\Client\Common\HttpAsyncClientEmulator;
6+
use Http\Client\Common\VersionBridgeClient;
67
use Http\Client\Exception;
78
use Http\Client\HttpAsyncClient;
89
use Http\Client\HttpClient;
@@ -12,17 +13,17 @@
1213
use Psr\Http\Message\ResponseInterface;
1314

1415
/**
15-
* HTTP client mock.
16+
* An implementation of the HTTP client that is useful for automated tests.
1617
*
17-
* This mock is most useful in tests. It does not send requests but stores them
18-
* for later retrieval. Additionally, you can set an exception to test
19-
* exception handling.
18+
* This mock does not send requests but stores them for later retrieval.
19+
* You can configure the mock with responses to return and/or exceptions to throw.
2020
*
2121
* @author David de Boer <[email protected]>
2222
*/
2323
class Client implements HttpClient, HttpAsyncClient
2424
{
2525
use HttpAsyncClientEmulator;
26+
use VersionBridgeClient;
2627

2728
/**
2829
* @var ResponseFactory
@@ -54,9 +55,6 @@ class Client implements HttpClient, HttpAsyncClient
5455
*/
5556
private $defaultException;
5657

57-
/**
58-
* @param ResponseFactory|null $responseFactory
59-
*/
6058
public function __construct(ResponseFactory $responseFactory = null)
6159
{
6260
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
@@ -65,7 +63,7 @@ public function __construct(ResponseFactory $responseFactory = null)
6563
/**
6664
* {@inheritdoc}
6765
*/
68-
public function sendRequest(RequestInterface $request)
66+
public function doSendRequest(RequestInterface $request)
6967
{
7068
$this->requests[] = $request;
7169

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

9290
/**
9391
* Adds an exception that will be thrown.
94-
*
95-
* @param \Exception $exception
9692
*/
9793
public function addException(\Exception $exception)
9894
{
@@ -103,18 +99,14 @@ public function addException(\Exception $exception)
10399
* Sets the default exception to throw when the list of added exceptions and responses is exhausted.
104100
*
105101
* If both a default exception and a default response are set, the exception will be thrown.
106-
*
107-
* @param \Exception|null $defaultException
108102
*/
109103
public function setDefaultException(\Exception $defaultException = null)
110104
{
111105
$this->defaultException = $defaultException;
112106
}
113107

114108
/**
115-
* Adds a response that will be returned.
116-
*
117-
* @param ResponseInterface $response
109+
* Adds a response that will be returned in first in first out order.
118110
*/
119111
public function addResponse(ResponseInterface $response)
120112
{
@@ -123,8 +115,6 @@ public function addResponse(ResponseInterface $response)
123115

124116
/**
125117
* Sets the default response to be returned when the list of added exceptions and responses is exhausted.
126-
*
127-
* @param ResponseInterface|null $defaultResponse
128118
*/
129119
public function setDefaultResponse(ResponseInterface $defaultResponse = null)
130120
{
@@ -141,9 +131,6 @@ public function getRequests()
141131
return $this->requests;
142132
}
143133

144-
/**
145-
* @return RequestInterface|false
146-
*/
147134
public function getLastRequest()
148135
{
149136
return end($this->requests);

0 commit comments

Comments
 (0)