Skip to content

Commit 853b2c4

Browse files
authored
Make sure we support any psr-18 client (#295)
* Make sure we support any psr-18 client * wring use statement * Updated exceptions * use use statements * Added tests with a PSR18 client
1 parent 9d208e2 commit 853b2c4

9 files changed

+88
-10
lines changed

Collector/Formatter.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Http\Client\Exception\TransferException;
88
use Http\Message\Formatter as MessageFormatter;
99
use Http\Message\Formatter\CurlCommandFormatter;
10+
use Psr\Http\Client\NetworkExceptionInterface;
11+
use Psr\Http\Client\RequestExceptionInterface;
1012
use Psr\Http\Message\RequestInterface;
1113
use Psr\Http\Message\ResponseInterface;
1214

@@ -49,11 +51,11 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c
4951
*/
5052
public function formatException(Exception $exception)
5153
{
52-
if ($exception instanceof HttpException) {
54+
if ($exception instanceof HttpException || $exception instanceof RequestExceptionInterface) {
5355
return $this->formatter->formatResponse($exception->getResponse());
5456
}
5557

56-
if ($exception instanceof TransferException) {
58+
if ($exception instanceof TransferException || $exception instanceof NetworkExceptionInterface) {
5759
return sprintf('Transfer error: %s', $exception->getMessage());
5860
}
5961

Collector/PluginClientFactory.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Http\Client\Common\PluginClient;
77
use Http\Client\HttpAsyncClient;
88
use Http\Client\HttpClient;
9+
use Psr\Http\Client\ClientInterface;
910
use Symfony\Component\Stopwatch\Stopwatch;
1011

1112
/**
@@ -46,9 +47,9 @@ public function __construct(Collector $collector, Formatter $formatter, Stopwatc
4647
}
4748

4849
/**
49-
* @param HttpClient|HttpAsyncClient $client
50-
* @param Plugin[] $plugins
51-
* @param array $options {
50+
* @param HttpClient|ClientInterface|HttpAsyncClient $client
51+
* @param Plugin[] $plugins
52+
* @param array $options {
5253
*
5354
* @var string $client_name to give client a name which may be used when displaying client information like in
5455
* the HTTPlugBundle profiler.

Collector/ProfileClient.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Http\Client\Exception\HttpException;
88
use Http\Client\HttpAsyncClient;
99
use Http\Client\HttpClient;
10+
use Psr\Http\Client\ClientInterface;
11+
use Psr\Http\Client\RequestExceptionInterface;
1012
use Psr\Http\Message\RequestInterface;
1113
use Psr\Http\Message\ResponseInterface;
1214
use Symfony\Component\Stopwatch\Stopwatch;
@@ -58,7 +60,7 @@ class ProfileClient implements HttpClient, HttpAsyncClient
5860
*/
5961
public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
6062
{
61-
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
63+
if (!(($client instanceof ClientInterface || $client instanceof HttpClient) && $client instanceof HttpAsyncClient)) {
6264
$client = new FlexibleHttpClient($client);
6365
}
6466

@@ -181,7 +183,7 @@ private function collectResponseInformations(ResponseInterface $response, Stopwa
181183
*/
182184
private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack)
183185
{
184-
if ($exception instanceof HttpException) {
186+
if ($exception instanceof HttpException || $exception instanceof RequestExceptionInterface) {
185187
$this->collectResponseInformations($exception->getResponse(), $event, $stack);
186188
}
187189

Collector/ProfileClientFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
88
use Http\HttplugBundle\ClientFactory\ClientFactory;
9+
use Psr\Http\Client\ClientInterface;
910
use Symfony\Component\Stopwatch\Stopwatch;
1011

1112
/**
@@ -61,7 +62,7 @@ public function createClient(array $config = [])
6162
{
6263
$client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config);
6364

64-
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
65+
if (!(($client instanceof HttpClient || $client instanceof ClientInterface) && $client instanceof HttpAsyncClient)) {
6566
$client = new FlexibleHttpClient($client);
6667
}
6768

Tests/Functional/ServiceInstantiationTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Http\HttplugBundle\Collector\ProfilePlugin;
1111
use Http\HttplugBundle\Collector\StackPlugin;
1212
use Nyholm\NSA;
13+
use Psr\Http\Client\ClientInterface;
14+
use Psr\Http\Message\ResponseInterface;
1315
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1416
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1517
use Symfony\Component\HttpFoundation\Request;
@@ -77,6 +79,28 @@ public function testProfilingDecoration()
7779
$this->assertInstanceOf(ProfilePlugin::class, $plugins[4]);
7880
}
7981

82+
public function testProfilingPsr18Decoration()
83+
{
84+
if (!interface_exists(ClientInterface::class)) {
85+
$this->markTestSkipped('PSR-18 is not installed');
86+
}
87+
88+
static::bootKernel(['debug' => true, 'environment' => 'psr18']);
89+
$container = static::$kernel->getContainer();
90+
91+
$client = $container->get('httplug.client.my_psr18');
92+
$this->assertInstanceOf(PluginClient::class, $client);
93+
$profileClient = NSA::getProperty($client, 'client');
94+
$this->assertInstanceOf(ProfileClient::class, $profileClient);
95+
96+
$flexibleClient = NSA::getProperty($profileClient, 'client');
97+
$psr18Client = NSA::getProperty($flexibleClient, 'httpClient');
98+
$this->assertInstanceOf(ClientInterface::class, $psr18Client);
99+
100+
$response = $client->sendRequest(new \GuzzleHttp\Psr7\Request('GET', 'https://example.com'));
101+
$this->assertInstanceOf(ResponseInterface::class, $response);
102+
}
103+
80104
/**
81105
* {@inheritdoc}
82106
*/

Tests/Resources/MyPsr18TestClient.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Tests\Resources;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use Psr\Http\Client\ClientInterface;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class MyPsr18TestClient implements ClientInterface
11+
{
12+
public function sendRequest(RequestInterface $request): ResponseInterface
13+
{
14+
return new Response();
15+
}
16+
17+
}

Tests/Resources/app/AppKernel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function registerBundles()
2828
new \Http\HttplugBundle\HttplugBundle(),
2929
];
3030

31-
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
31+
if (in_array($this->getEnvironment(), array('dev', 'test', 'psr18'))) {
3232
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
3333
}
3434

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
imports:
2+
- { resource: config_test.yml }
3+
4+
httplug:
5+
discovery:
6+
async_client: auto
7+
clients:
8+
my_psr18:
9+
service: 'my_psr18_client'
10+
public: true
11+
plugins:
12+
-
13+
decoder:
14+
use_content_encoding: false
15+
- app.http.plugin.custom
16+
-
17+
add_host:
18+
host: "http://localhost:8000"
19+
-
20+
authentication:
21+
my_basic:
22+
type: basic
23+
username: foo
24+
password: bar
25+
26+
services:
27+
my_psr18_client:
28+
class: Http\HttplugBundle\Tests\Resources\MyPsr18TestClient

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@
6161
"autoload": {
6262
"psr-4": {
6363
"Http\\HttplugBundle\\": ""
64-
}
64+
},
65+
"exclude-from-classmap": [
66+
"/Tests/Resources/MyPsr18TestClient.php"
67+
]
6568
},
6669
"autoload-dev": {
6770
"classmap": [

0 commit comments

Comments
 (0)