Skip to content

Commit d12c15d

Browse files
Nyholmfbourigault
authored andcommitted
Use flex client wrapper for profiling if needed (#209)
* Adding failing tests for #206 * Use FlexibleClient * Minor fixes * Updated test name
1 parent c88d5d6 commit d12c15d

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

Collector/ProfileClient.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,18 @@ class ProfileClient implements HttpClient, HttpAsyncClient
4747
private $eventNames = [];
4848

4949
/**
50-
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and
51-
* HttpAsyncClient interfaces.
50+
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement HttpClient or
51+
* HttpAsyncClient interface.
5252
* @param Collector $collector
5353
* @param Formatter $formatter
5454
* @param Stopwatch $stopwatch
5555
*/
5656
public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
5757
{
5858
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
59-
throw new \RuntimeException(sprintf(
60-
'%s first argument must implement %s and %s. Consider using %s.',
61-
__METHOD__,
62-
HttpClient::class,
63-
HttpAsyncClient::class,
64-
FlexibleHttpClient::class
65-
));
59+
$client = new FlexibleHttpClient($client);
6660
}
61+
6762
$this->client = $client;
6863
$this->collector = $collector;
6964
$this->formatter = $formatter;

Tests/Functional/Issue206.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Http\HttplugBundle\Tests\Functional;
6+
7+
use Http\Client\Common\HttpMethodsClient;
8+
use Http\Client\Common\PluginClient;
9+
use Http\Client\Common\PluginClientFactory;
10+
use Http\Discovery\HttpClientDiscovery;
11+
use Http\Discovery\MessageFactoryDiscovery;
12+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
13+
14+
class Issue206 extends WebTestCase
15+
{
16+
public function testCustomClientDoesNotCauseException()
17+
{
18+
static::bootKernel();
19+
$container = static::$kernel->getContainer();
20+
PluginClientFactory::setFactory([$container->get('Http\Client\Common\PluginClientFactory'), 'createClient']);
21+
22+
// Create a client
23+
$myCustomClient = new HttpMethodsClient(HttpClientDiscovery::find(), MessageFactoryDiscovery::find());
24+
$pluginClient = (new PluginClientFactory())->createClient($myCustomClient, []);
25+
26+
// If we get to this line, no exceptions has been thrown.
27+
$this->assertInstanceOf(PluginClient::class, $pluginClient);
28+
}
29+
}

0 commit comments

Comments
 (0)