Skip to content

Commit e785e9e

Browse files
committed
upgrade to version 2
1 parent 66d646b commit e785e9e

File tree

5 files changed

+37
-37
lines changed

5 files changed

+37
-37
lines changed

.travis.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ cache:
77
- $HOME/.composer/cache/files
88

99
php:
10-
- 5.5
11-
- 5.6
12-
- 7.0
1310
- 7.1
11+
- 7.2
1412

1513
env:
1614
global:
@@ -23,7 +21,7 @@ branches:
2321
matrix:
2422
fast_finish: true
2523
include:
26-
- php: 5.5
24+
- php: 7.1
2725
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
2826

2927
before_install:

CHANGELOG.md

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

3+
## 2.0.0 - unreleased
4+
5+
### Changed
6+
7+
- Client::getLastRequest returns `null` instead of `false` when on requests have been recorded yet.
8+
39
## 1.1.0 - 2018-01-08
410

511
### Added

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^5.5 || ^7.0",
15-
"php-http/httplug": "^1.0",
16-
"php-http/client-common": "^1.1",
14+
"php": "^7.1",
15+
"php-http/httplug": "^2.0",
16+
"php-http/client-common": "^2.0@dev",
1717
"php-http/discovery": "^1.0",
1818
"php-http/message-factory": "^1.0"
1919
},
@@ -36,7 +36,7 @@
3636
},
3737
"extra": {
3838
"branch-alias": {
39-
"dev-master": "1.1-dev"
39+
"dev-master": "2.0-dev"
4040
}
4141
}
4242
}

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_null_when_there_is_no_last_request()
84+
{
85+
$this->getLastRequest()->shouldReturn(null);
86+
}
7687
}

src/Client.php

+10-25
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
use Psr\Http\Message\ResponseInterface;
1313

1414
/**
15-
* HTTP client mock.
15+
* An implementation of the HTTP client that is useful for automated tests.
1616
*
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.
17+
* This mock does not send requests but stores them for later retrieval.
18+
* You can configure the mock with responses to return and/or exceptions to throw.
2019
*
2120
* @author David de Boer <[email protected]>
2221
*/
@@ -54,9 +53,6 @@ class Client implements HttpClient, HttpAsyncClient
5453
*/
5554
private $defaultException;
5655

57-
/**
58-
* @param ResponseFactory|null $responseFactory
59-
*/
6056
public function __construct(ResponseFactory $responseFactory = null)
6157
{
6258
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
@@ -65,7 +61,7 @@ public function __construct(ResponseFactory $responseFactory = null)
6561
/**
6662
* {@inheritdoc}
6763
*/
68-
public function sendRequest(RequestInterface $request)
64+
public function sendRequest(RequestInterface $request): ResponseInterface
6965
{
7066
$this->requests[] = $request;
7167

@@ -91,8 +87,6 @@ public function sendRequest(RequestInterface $request)
9187

9288
/**
9389
* Adds an exception that will be thrown.
94-
*
95-
* @param \Exception $exception
9690
*/
9791
public function addException(\Exception $exception)
9892
{
@@ -103,18 +97,14 @@ public function addException(\Exception $exception)
10397
* Sets the default exception to throw when the list of added exceptions and responses is exhausted.
10498
*
10599
* If both a default exception and a default response are set, the exception will be thrown.
106-
*
107-
* @param \Exception|null $defaultException
108100
*/
109-
public function setDefaultException(\Exception $defaultException = null)
101+
public function setDefaultException(?\Exception $defaultException)
110102
{
111103
$this->defaultException = $defaultException;
112104
}
113105

114106
/**
115-
* Adds a response that will be returned.
116-
*
117-
* @param ResponseInterface $response
107+
* Adds a response that will be returned in first in first out order.
118108
*/
119109
public function addResponse(ResponseInterface $response)
120110
{
@@ -123,10 +113,8 @@ public function addResponse(ResponseInterface $response)
123113

124114
/**
125115
* Sets the default response to be returned when the list of added exceptions and responses is exhausted.
126-
*
127-
* @param ResponseInterface|null $defaultResponse
128116
*/
129-
public function setDefaultResponse(ResponseInterface $defaultResponse = null)
117+
public function setDefaultResponse(?ResponseInterface $defaultResponse)
130118
{
131119
$this->defaultResponse = $defaultResponse;
132120
}
@@ -136,16 +124,13 @@ public function setDefaultResponse(ResponseInterface $defaultResponse = null)
136124
*
137125
* @return RequestInterface[]
138126
*/
139-
public function getRequests()
127+
public function getRequests(): array
140128
{
141129
return $this->requests;
142130
}
143131

144-
/**
145-
* @return RequestInterface|false
146-
*/
147-
public function getLastRequest()
132+
public function getLastRequest(): ?RequestInterface
148133
{
149-
return end($this->requests);
134+
return end($this->requests) ?: null;
150135
}
151136
}

0 commit comments

Comments
 (0)