Skip to content

Commit 9947c7f

Browse files
Allow the PSR-17 response factory and add union type checking (#51)
1 parent f718936 commit 9947c7f

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

Diff for: .styleci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ enabled:
1212

1313
disabled:
1414
- phpdoc_annotation_without_dot # This is still buggy: https://github.com/symfony/symfony/pull/19198
15+
- single_line_throw

Diff for: CHANGELOG.md

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

3-
## 1.4.0 -
3+
## 1.4.0 - 2020-07-02
4+
5+
### Added
6+
7+
- Support for the PSR-17 response factory
8+
9+
### Changed
10+
11+
- Drop support for PHP 5 and 7.0
12+
- Consitent implementation of union type checking
413

514
### Fixed
6-
- `reset()` should not trigger `setDefaultException` error condition
715

8-
### Breaking
9-
- drop support for PHP 5 and 7.0
16+
- `reset()` should not trigger `setDefaultException` error condition
1017

1118
## 1.3.1 - 2019-11-06
1219

Diff for: composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
"php-http/client-common": "^1.9 || ^2.0",
2121
"php-http/discovery": "^1.0",
2222
"php-http/httplug": "^1.0 || ^2.0",
23-
"php-http/message-factory": "^1.0"
23+
"php-http/message-factory": "^1.0",
24+
"psr/http-client": "^1.0",
25+
"psr/http-factory": "^1.0",
26+
"psr/http-message": "^1.0",
27+
"symfony/polyfill-php80": "^1.17"
2428
},
2529
"provide": {
2630
"php-http/async-client-implementation": "1.0",

Diff for: spec/ClientSpec.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Http\Message\RequestMatcher;
88
use Http\Message\ResponseFactory;
99
use Http\Mock\Client;
10+
use Psr\Http\Client\ClientExceptionInterface;
1011
use Psr\Http\Message\RequestInterface;
1112
use Psr\Http\Message\ResponseInterface;
1213
use PhpSpec\ObjectBehavior;
@@ -49,16 +50,16 @@ function it_returns_the_default_response_for_a_request(RequestInterface $request
4950

5051
function it_throws_an_exception_for_a_request(RequestInterface $request)
5152
{
52-
$this->addException(new \Exception());
53+
$this->addException(new Exception());
5354

54-
$this->shouldThrow('Exception')->duringSendRequest($request);
55+
$this->shouldThrow(Exception::class)->duringSendRequest($request);
5556
}
5657

5758
function it_throws_the_default_exception_for_a_request(RequestInterface $request)
5859
{
59-
$this->setDefaultException(new \Exception());
60+
$this->setDefaultException(new Exception());
6061

61-
$this->shouldThrow('Exception')->duringSendRequest($request);
62+
$this->shouldThrow(Exception::class)->duringSendRequest($request);
6263
}
6364

6465
function it_creates_an_empty_response_when_none_is_added(
@@ -94,8 +95,8 @@ function it_reset(
9495
) {
9596
$this->addResponse($response);
9697
$this->setDefaultResponse($response);
97-
$this->addException(new \Exception());
98-
$this->setDefaultException(new \Exception());
98+
$this->addException(new Exception());
99+
$this->setDefaultException(new Exception());
99100

100101
$responseFactory->createResponse()->willReturn($newResponse);
101102

@@ -122,8 +123,8 @@ function it_throws_exception_if_request_matcher_matches(
122123
RequestInterface $request
123124
) {
124125
$matcher->matches($request)->willReturn(true);
125-
$this->on($matcher, new \Exception());
126-
$this->shouldThrow('Exception')->duringSendRequest($request);
126+
$this->on($matcher, new Exception());
127+
$this->shouldThrow(Exception::class)->duringSendRequest($request);
127128
}
128129

129130
function it_skips_conditional_response_if_matcher_returns_false(
@@ -155,3 +156,7 @@ function(RequestInterface $request) use ($response) {
155156
$this->sendRequest($request)->shouldReturn($response);
156157
}
157158
}
159+
160+
class Exception extends \Exception implements ClientExceptionInterface
161+
{
162+
}

Diff for: src/Client.php

+21-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Http\Message\ResponseFactory;
1313
use Psr\Http\Client\ClientExceptionInterface;
1414
use Psr\Http\Message\RequestInterface;
15+
use Psr\Http\Message\ResponseFactoryInterface;
1516
use Psr\Http\Message\ResponseInterface;
1617

1718
/**
@@ -28,7 +29,7 @@ class Client implements HttpClient, HttpAsyncClient
2829
use VersionBridgeClient;
2930

3031
/**
31-
* @var ResponseFactory
32+
* @var ResponseFactory|ResponseFactoryInterface
3233
*/
3334
private $responseFactory;
3435

@@ -62,8 +63,17 @@ class Client implements HttpClient, HttpAsyncClient
6263
*/
6364
private $defaultException;
6465

65-
public function __construct(ResponseFactory $responseFactory = null)
66+
/**
67+
* @param ResponseFactory|ResponseFactoryInterface|null
68+
*/
69+
public function __construct($responseFactory = null)
6670
{
71+
if (!$responseFactory instanceof ResponseFactory && !$responseFactory instanceof ResponseFactoryInterface && null !== $responseFactory) {
72+
throw new \TypeError(
73+
sprintf('%s::__construct(): Argument #1 ($responseFactory) must be of type %s|%s|null, %s given', self::class, ResponseFactory::class, ResponseFactoryInterface::class, get_debug_type($responseFactory))
74+
);
75+
}
76+
6777
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
6878
}
6979

@@ -122,6 +132,12 @@ public function doSendRequest(RequestInterface $request)
122132
*/
123133
public function on(RequestMatcher $requestMatcher, $result)
124134
{
135+
if (!$result instanceof ResponseInterface && !$result instanceof Exception && !$result instanceof ClientExceptionInterface && !is_callable($result)) {
136+
throw new \TypeError(
137+
sprintf('%s::on(): Argument #2 ($result) must be of type %s|%s|%s|callable, %s given', self::class, ResponseInterface::class, Exception::class, ClientExceptionInterface::class, get_debug_type($result))
138+
);
139+
}
140+
125141
$callable = self::makeCallable($result);
126142

127143
$this->conditionalResults[] = [
@@ -133,8 +149,6 @@ public function on(RequestMatcher $requestMatcher, $result)
133149
/**
134150
* @param ResponseInterface|Exception|ClientExceptionInterface|callable $result
135151
*
136-
* @throws \InvalidArgumentException
137-
*
138152
* @return callable
139153
*/
140154
private static function makeCallable($result)
@@ -149,13 +163,9 @@ private static function makeCallable($result)
149163
};
150164
}
151165

152-
if ($result instanceof \Exception) {
153-
return function () use ($result) {
154-
throw $result;
155-
};
156-
}
157-
158-
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable');
166+
return function () use ($result) {
167+
throw $result;
168+
};
159169
}
160170

161171
/**

0 commit comments

Comments
 (0)