Skip to content

Allow the PSR-17 response factory and add union type checking #51

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 4 commits into from
Jul 2, 2020
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
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ enabled:

disabled:
- phpdoc_annotation_without_dot # This is still buggy: https://github.com/symfony/symfony/pull/19198
- single_line_throw
15 changes: 11 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Change Log

## 1.4.0 -
## 1.4.0 - 2020-07-02

### Added

- Support for the PSR-17 response factory

### Changed

- Drop support for PHP 5 and 7.0
- Consitent implementation of union type checking

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

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

## 1.3.1 - 2019-11-06

Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
"php-http/client-common": "^1.9 || ^2.0",
"php-http/discovery": "^1.0",
"php-http/httplug": "^1.0 || ^2.0",
"php-http/message-factory": "^1.0"
"php-http/message-factory": "^1.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"symfony/polyfill-php80": "^1.17"
},
"provide": {
"php-http/async-client-implementation": "1.0",
Expand Down
21 changes: 13 additions & 8 deletions spec/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Http\Message\RequestMatcher;
use Http\Message\ResponseFactory;
use Http\Mock\Client;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
Expand Down Expand Up @@ -49,16 +50,16 @@ function it_returns_the_default_response_for_a_request(RequestInterface $request

function it_throws_an_exception_for_a_request(RequestInterface $request)
{
$this->addException(new \Exception());
$this->addException(new Exception());

$this->shouldThrow('Exception')->duringSendRequest($request);
$this->shouldThrow(Exception::class)->duringSendRequest($request);
}

function it_throws_the_default_exception_for_a_request(RequestInterface $request)
{
$this->setDefaultException(new \Exception());
$this->setDefaultException(new Exception());

$this->shouldThrow('Exception')->duringSendRequest($request);
$this->shouldThrow(Exception::class)->duringSendRequest($request);
}

function it_creates_an_empty_response_when_none_is_added(
Expand Down Expand Up @@ -94,8 +95,8 @@ function it_reset(
) {
$this->addResponse($response);
$this->setDefaultResponse($response);
$this->addException(new \Exception());
$this->setDefaultException(new \Exception());
$this->addException(new Exception());
$this->setDefaultException(new Exception());

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

Expand All @@ -122,8 +123,8 @@ function it_throws_exception_if_request_matcher_matches(
RequestInterface $request
) {
$matcher->matches($request)->willReturn(true);
$this->on($matcher, new \Exception());
$this->shouldThrow('Exception')->duringSendRequest($request);
$this->on($matcher, new Exception());
$this->shouldThrow(Exception::class)->duringSendRequest($request);
}

function it_skips_conditional_response_if_matcher_returns_false(
Expand Down Expand Up @@ -155,3 +156,7 @@ function(RequestInterface $request) use ($response) {
$this->sendRequest($request)->shouldReturn($response);
}
}

class Exception extends \Exception implements ClientExceptionInterface
{
}
32 changes: 21 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Http\Message\ResponseFactory;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -28,7 +29,7 @@ class Client implements HttpClient, HttpAsyncClient
use VersionBridgeClient;

/**
* @var ResponseFactory
* @var ResponseFactory|ResponseFactoryInterface
*/
private $responseFactory;

Expand Down Expand Up @@ -62,8 +63,17 @@ class Client implements HttpClient, HttpAsyncClient
*/
private $defaultException;

public function __construct(ResponseFactory $responseFactory = null)
/**
* @param ResponseFactory|ResponseFactoryInterface|null
*/
public function __construct($responseFactory = null)
{
if (!$responseFactory instanceof ResponseFactory && !$responseFactory instanceof ResponseFactoryInterface && null !== $responseFactory) {
throw new \TypeError(
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))
);
}

$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
}

Expand Down Expand Up @@ -122,6 +132,12 @@ public function doSendRequest(RequestInterface $request)
*/
public function on(RequestMatcher $requestMatcher, $result)
{
if (!$result instanceof ResponseInterface && !$result instanceof Exception && !$result instanceof ClientExceptionInterface && !is_callable($result)) {
throw new \TypeError(
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))
);
}

$callable = self::makeCallable($result);

$this->conditionalResults[] = [
Expand All @@ -133,8 +149,6 @@ public function on(RequestMatcher $requestMatcher, $result)
/**
* @param ResponseInterface|Exception|ClientExceptionInterface|callable $result
*
* @throws \InvalidArgumentException
*
* @return callable
*/
private static function makeCallable($result)
Expand All @@ -149,13 +163,9 @@ private static function makeCallable($result)
};
}

if ($result instanceof \Exception) {
return function () use ($result) {
throw $result;
};
}

throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable');
return function () use ($result) {
throw $result;
};
}

/**
Expand Down