From 8bc38f9e94a2f78a1945cc6be6470eec3fa61c13 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 29 Dec 2018 16:18:20 +0100 Subject: [PATCH 1/5] Make sure we support any psr-18 client --- Collector/PluginClientFactory.php | 7 ++++--- Collector/ProfileClient.php | 3 ++- Collector/ProfileClientFactory.php | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Collector/PluginClientFactory.php b/Collector/PluginClientFactory.php index 25cd4896..a970b0bc 100644 --- a/Collector/PluginClientFactory.php +++ b/Collector/PluginClientFactory.php @@ -6,6 +6,7 @@ use Http\Client\Common\PluginClient; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; +use Http\HttplugBundle\Tests\Unit\Collector\ClientInterface; use Symfony\Component\Stopwatch\Stopwatch; /** @@ -46,9 +47,9 @@ public function __construct(Collector $collector, Formatter $formatter, Stopwatc } /** - * @param HttpClient|HttpAsyncClient $client - * @param Plugin[] $plugins - * @param array $options { + * @param HttpClient|ClientInterface|HttpAsyncClient $client + * @param Plugin[] $plugins + * @param array $options { * * @var string $client_name to give client a name which may be used when displaying client information like in * the HTTPlugBundle profiler. diff --git a/Collector/ProfileClient.php b/Collector/ProfileClient.php index b79f2e63..d932e22e 100644 --- a/Collector/ProfileClient.php +++ b/Collector/ProfileClient.php @@ -7,6 +7,7 @@ use Http\Client\Exception\HttpException; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; +use Http\HttplugBundle\Tests\Unit\Collector\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\Stopwatch\Stopwatch; @@ -58,7 +59,7 @@ class ProfileClient implements HttpClient, HttpAsyncClient */ public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) { - if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { + if (!(($client instanceof ClientInterface || $client instanceof HttpClient) && $client instanceof HttpAsyncClient)) { $client = new FlexibleHttpClient($client); } diff --git a/Collector/ProfileClientFactory.php b/Collector/ProfileClientFactory.php index 613243ea..e2adffa4 100644 --- a/Collector/ProfileClientFactory.php +++ b/Collector/ProfileClientFactory.php @@ -6,6 +6,7 @@ use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; use Http\HttplugBundle\ClientFactory\ClientFactory; +use Psr\Http\Client\ClientInterface; use Symfony\Component\Stopwatch\Stopwatch; /** @@ -61,7 +62,7 @@ public function createClient(array $config = []) { $client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config); - if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { + if (!(($client instanceof HttpClient || $client instanceof ClientInterface) && $client instanceof HttpAsyncClient)) { $client = new FlexibleHttpClient($client); } From 5e93b35d3d69d88d7fec179fcb87364c35bad495 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 30 Dec 2018 07:55:10 +0100 Subject: [PATCH 2/5] wring use statement --- Collector/PluginClientFactory.php | 2 +- Collector/ProfileClient.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Collector/PluginClientFactory.php b/Collector/PluginClientFactory.php index a970b0bc..91c22216 100644 --- a/Collector/PluginClientFactory.php +++ b/Collector/PluginClientFactory.php @@ -6,7 +6,7 @@ use Http\Client\Common\PluginClient; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; -use Http\HttplugBundle\Tests\Unit\Collector\ClientInterface; +use Psr\Http\Client\ClientInterface; use Symfony\Component\Stopwatch\Stopwatch; /** diff --git a/Collector/ProfileClient.php b/Collector/ProfileClient.php index d932e22e..58d7b1ac 100644 --- a/Collector/ProfileClient.php +++ b/Collector/ProfileClient.php @@ -7,7 +7,7 @@ use Http\Client\Exception\HttpException; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; -use Http\HttplugBundle\Tests\Unit\Collector\ClientInterface; +use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\Stopwatch\Stopwatch; From d0775d3cf9a2fe2e4cfffa015e57208af3cb28b2 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 30 Dec 2018 11:14:35 +0100 Subject: [PATCH 3/5] Updated exceptions --- Collector/Formatter.php | 4 ++-- Collector/ProfileClient.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Collector/Formatter.php b/Collector/Formatter.php index a77632fe..2a92e086 100644 --- a/Collector/Formatter.php +++ b/Collector/Formatter.php @@ -49,11 +49,11 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c */ public function formatException(Exception $exception) { - if ($exception instanceof HttpException) { + if ($exception instanceof HttpException || $exception instanceof \Psr\Http\Client\RequestExceptionInterface) { return $this->formatter->formatResponse($exception->getResponse()); } - if ($exception instanceof TransferException) { + if ($exception instanceof TransferException || $exception instanceof \Psr\Http\Client\NetworkExceptionInterface) { return sprintf('Transfer error: %s', $exception->getMessage()); } diff --git a/Collector/ProfileClient.php b/Collector/ProfileClient.php index 58d7b1ac..d18ed35c 100644 --- a/Collector/ProfileClient.php +++ b/Collector/ProfileClient.php @@ -182,7 +182,7 @@ private function collectResponseInformations(ResponseInterface $response, Stopwa */ private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) { - if ($exception instanceof HttpException) { + if ($exception instanceof HttpException || $exception instanceof \Psr\Http\Client\RequestExceptionInterface) { $this->collectResponseInformations($exception->getResponse(), $event, $stack); } From ac725800094400e41787da6c98570eddacf01227 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 30 Dec 2018 13:04:03 +0100 Subject: [PATCH 4/5] use use statements --- Collector/Formatter.php | 6 ++++-- Collector/ProfileClient.php | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Collector/Formatter.php b/Collector/Formatter.php index 2a92e086..de648c83 100644 --- a/Collector/Formatter.php +++ b/Collector/Formatter.php @@ -7,6 +7,8 @@ use Http\Client\Exception\TransferException; use Http\Message\Formatter as MessageFormatter; use Http\Message\Formatter\CurlCommandFormatter; +use Psr\Http\Client\NetworkExceptionInterface; +use Psr\Http\Client\RequestExceptionInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -49,11 +51,11 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c */ public function formatException(Exception $exception) { - if ($exception instanceof HttpException || $exception instanceof \Psr\Http\Client\RequestExceptionInterface) { + if ($exception instanceof HttpException || $exception instanceof RequestExceptionInterface) { return $this->formatter->formatResponse($exception->getResponse()); } - if ($exception instanceof TransferException || $exception instanceof \Psr\Http\Client\NetworkExceptionInterface) { + if ($exception instanceof TransferException || $exception instanceof NetworkExceptionInterface) { return sprintf('Transfer error: %s', $exception->getMessage()); } diff --git a/Collector/ProfileClient.php b/Collector/ProfileClient.php index d18ed35c..213d727f 100644 --- a/Collector/ProfileClient.php +++ b/Collector/ProfileClient.php @@ -8,6 +8,7 @@ use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; use Psr\Http\Client\ClientInterface; +use Psr\Http\Client\RequestExceptionInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\Stopwatch\Stopwatch; @@ -182,7 +183,7 @@ private function collectResponseInformations(ResponseInterface $response, Stopwa */ private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) { - if ($exception instanceof HttpException || $exception instanceof \Psr\Http\Client\RequestExceptionInterface) { + if ($exception instanceof HttpException || $exception instanceof RequestExceptionInterface) { $this->collectResponseInformations($exception->getResponse(), $event, $stack); } From 04f1ec3b8bfd79bb25f8a3ee4d819fe6517561b0 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 30 Dec 2018 14:01:44 +0100 Subject: [PATCH 5/5] Added tests with a PSR18 client --- Tests/Functional/ServiceInstantiationTest.php | 24 ++++++++++++++++ Tests/Resources/MyPsr18TestClient.php | 17 +++++++++++ Tests/Resources/app/AppKernel.php | 2 +- Tests/Resources/app/config/config_psr18.yml | 28 +++++++++++++++++++ composer.json | 5 +++- 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 Tests/Resources/MyPsr18TestClient.php create mode 100644 Tests/Resources/app/config/config_psr18.yml diff --git a/Tests/Functional/ServiceInstantiationTest.php b/Tests/Functional/ServiceInstantiationTest.php index 817d8aa4..71852e04 100644 --- a/Tests/Functional/ServiceInstantiationTest.php +++ b/Tests/Functional/ServiceInstantiationTest.php @@ -10,6 +10,8 @@ use Http\HttplugBundle\Collector\ProfilePlugin; use Http\HttplugBundle\Collector\StackPlugin; use Nyholm\NSA; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\ResponseInterface; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; @@ -77,6 +79,28 @@ public function testProfilingDecoration() $this->assertInstanceOf(ProfilePlugin::class, $plugins[4]); } + public function testProfilingPsr18Decoration() + { + if (!interface_exists(ClientInterface::class)) { + $this->markTestSkipped('PSR-18 is not installed'); + } + + static::bootKernel(['debug' => true, 'environment' => 'psr18']); + $container = static::$kernel->getContainer(); + + $client = $container->get('httplug.client.my_psr18'); + $this->assertInstanceOf(PluginClient::class, $client); + $profileClient = NSA::getProperty($client, 'client'); + $this->assertInstanceOf(ProfileClient::class, $profileClient); + + $flexibleClient = NSA::getProperty($profileClient, 'client'); + $psr18Client = NSA::getProperty($flexibleClient, 'httpClient'); + $this->assertInstanceOf(ClientInterface::class, $psr18Client); + + $response = $client->sendRequest(new \GuzzleHttp\Psr7\Request('GET', 'https://example.com')); + $this->assertInstanceOf(ResponseInterface::class, $response); + } + /** * {@inheritdoc} */ diff --git a/Tests/Resources/MyPsr18TestClient.php b/Tests/Resources/MyPsr18TestClient.php new file mode 100644 index 00000000..e4c0df00 --- /dev/null +++ b/Tests/Resources/MyPsr18TestClient.php @@ -0,0 +1,17 @@ +getEnvironment(), array('dev', 'test'))) { + if (in_array($this->getEnvironment(), array('dev', 'test', 'psr18'))) { $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); } diff --git a/Tests/Resources/app/config/config_psr18.yml b/Tests/Resources/app/config/config_psr18.yml new file mode 100644 index 00000000..f3aad063 --- /dev/null +++ b/Tests/Resources/app/config/config_psr18.yml @@ -0,0 +1,28 @@ +imports: + - { resource: config_test.yml } + +httplug: + discovery: + async_client: auto + clients: + my_psr18: + service: 'my_psr18_client' + public: true + plugins: + - + decoder: + use_content_encoding: false + - app.http.plugin.custom + - + add_host: + host: "http://localhost:8000" + - + authentication: + my_basic: + type: basic + username: foo + password: bar + +services: + my_psr18_client: + class: Http\HttplugBundle\Tests\Resources\MyPsr18TestClient diff --git a/composer.json b/composer.json index a0a4b834..469f59e5 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,10 @@ "autoload": { "psr-4": { "Http\\HttplugBundle\\": "" - } + }, + "exclude-from-classmap": [ + "/Tests/Resources/MyPsr18TestClient.php" + ] }, "autoload-dev": { "classmap": [