Skip to content

Commit

Permalink
Merge pull request #65 from php-http/fix-factory
Browse files Browse the repository at this point in the history
test that mock client can discover a message factory
  • Loading branch information
dbu authored May 21, 2023
2 parents 86cc715 + b6a326a commit ae5d717
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Change Log

## 1.5.2 - 2023-05-23
## 1.6.0 - 2023-05-21

### Fixed

- We actually did fallback to the legacy message factory discovery so 1.5.2 is broken.
Changed to use PSR 17 factory discovery.
If you allow the composer plugin of `php-http/discovery`, things will work out of the box.
When disabled and you do not have a PSR-17 factory installed, you will need to explicitly require one, e.g. `nyholm/psr7`.

## 1.5.2 - 2023-05-17

**Broken, use 1.6.0 instead**

### Removed

Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"require": {
"php": "^7.1 || ^8.0",
"php-http/client-common": "^2.0",
"php-http/discovery": "^1.0",
"php-http/discovery": "^1.16",
"php-http/httplug": "^2.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-factory-implementation": "^1.0",
"psr/http-message": "^1.0 || ^2.0",
"symfony/polyfill-php80": "^1.17"
},
Expand All @@ -34,7 +34,10 @@
"phpspec/phpspec": "^5.1 || ^6.1 || ^7.3"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
}
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 3 additions & 0 deletions spec/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function let(ResponseFactoryInterface $responseFactory)
function it_is_initializable()
{
$this->shouldHaveType(Client::class);

// make sure the client is also instantiable without arguments
new Client();
}

function it_is_an_http_client()
Expand Down
18 changes: 17 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Http\Client\Exception;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Discovery\Exception\NotFoundException;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Message\RequestMatcher;
use Http\Message\ResponseFactory;
use Psr\Http\Client\ClientExceptionInterface;
Expand Down Expand Up @@ -72,7 +74,21 @@ public function __construct($responseFactory = null)
);
}

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

return;
}
try {
$this->responseFactory = Psr17FactoryDiscovery::findResponseFactory();
} catch (NotFoundException $notFoundException) {
try {
$this->responseFactory = MessageFactoryDiscovery::find();
} catch (NotFoundException $e) {
// throw the psr-17 exception to make people install the new way and not the old
throw $notFoundException;
}
}
}

/**
Expand Down

0 comments on commit ae5d717

Please sign in to comment.