Skip to content

Use flex client wrapper for profiling if needed #209

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
Nov 29, 2017
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
13 changes: 4 additions & 9 deletions Collector/ProfileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,18 @@ class ProfileClient implements HttpClient, HttpAsyncClient
private $eventNames = [];

/**
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and
* HttpAsyncClient interfaces.
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement HttpClient or
* HttpAsyncClient interface.
* @param Collector $collector
* @param Formatter $formatter
* @param Stopwatch $stopwatch
*/
public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
{
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
throw new \RuntimeException(sprintf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the exception still be thrown if neither of the interfaces is implemented? I know that the FlexibleHttpClient throws anyway, but I am not sure if relying on that behaviour would be a good idea and also could the stack trace be confusing for users.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, an exception are thrown, But yes. It could be a good idea to throw en exception here as well.

'%s first argument must implement %s and %s. Consider using %s.',
__METHOD__,
HttpClient::class,
HttpAsyncClient::class,
FlexibleHttpClient::class
));
$client = new FlexibleHttpClient($client);
}

$this->client = $client;
$this->collector = $collector;
$this->formatter = $formatter;
Expand Down
29 changes: 29 additions & 0 deletions Tests/Functional/Issue206.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Http\HttplugBundle\Tests\Functional;

use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\PluginClient;
use Http\Client\Common\PluginClientFactory;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class Issue206 extends WebTestCase
{
public function testCustomClientDoesNotCauseException()
{
static::bootKernel();
$container = static::$kernel->getContainer();
PluginClientFactory::setFactory([$container->get('Http\Client\Common\PluginClientFactory'), 'createClient']);

// Create a client
$myCustomClient = new HttpMethodsClient(HttpClientDiscovery::find(), MessageFactoryDiscovery::find());
$pluginClient = (new PluginClientFactory())->createClient($myCustomClient, []);

// If we get to this line, no exceptions has been thrown.
$this->assertInstanceOf(PluginClient::class, $pluginClient);
}
}