Skip to content

add profile client #136

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 1 commit into from
Apr 3, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## Unreleased

### Added

- The real request method and target url are now displayed in the profiler.

## 1.4.0 - 2017-02-21

### Changed
Expand Down
83 changes: 83 additions & 0 deletions Collector/ProfileClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Http\HttplugBundle\Collector;

use Http\Client\Common\FlexibleHttpClient;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Psr\Http\Message\RequestInterface;

/**
* The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target
* url and response status code.
*
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
*
* @internal
*/
class ProfileClient implements HttpClient, HttpAsyncClient
{
/**
* @var HttpClient|HttpAsyncClient
*/
private $client;

/**
* @var Collector
*/
private $collector;

/**
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and
Copy link

Choose a reason for hiding this comment

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

How about introducing a new interface extending both interfaces and type hinting against that?

Copy link
Member

Choose a reason for hiding this comment

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

That may be convenient but it does not comply with the Interface Segregation principle.

I know we talked about this before and decided not to, but I cannot find that thread atm.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link

@greg0ire greg0ire Apr 3, 2017

Choose a reason for hiding this comment

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

The ISP states not to make big interfaces, so that people don't need to implement all of the methods inside. In your case, they still can choose to implement either inherited interface, so ISP wouldn't be violated here IMO. And if you do need both methods, it looks like the best bay to express that need.

Copy link

@greg0ire greg0ire Apr 3, 2017

Choose a reason for hiding this comment

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

To be honest, I don't understand what inversion of control has to do with this, but I trust you have your (maybe fairly complicated) reasons. Plus, I wasn't aware there were other capabilities, and if an interface is introduced for every combination, this could get quite messy… the real thing to fix here might be php itself , to add the possibility to type hint against several interfaces :P

Copy link

Choose a reason for hiding this comment

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

More about this here

* HttpAsyncClient interfaces.
* @param Collector $collector
*/
public function __construct($client, Collector $collector)
{
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
throw new \RuntimeException(sprintf(
'%s first argument must implement %s and %s. Consider using %s.',
__METHOD__,
HttpClient::class,
HttpAsyncClient::class,
FlexibleHttpClient::class
));
}
$this->client = $client;
$this->collector = $collector;
}

/**
* {@inheritdoc}
*/
public function sendAsyncRequest(RequestInterface $request)
{
$this->collectRequestInformations($request);

return $this->client->sendAsyncRequest($request);
}

/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request)
{
$this->collectRequestInformations($request);

return $this->client->sendRequest($request);
}

/**
* @param RequestInterface $request
*/
private function collectRequestInformations(RequestInterface $request)
{
if (!$stack = $this->collector->getCurrentStack()) {
return;
}

$stack = $this->collector->getCurrentStack();
$stack->setRequestTarget($request->getRequestTarget());
$stack->setRequestMethod($request->getMethod());
}
}
55 changes: 55 additions & 0 deletions Collector/ProfileClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Http\HttplugBundle\Collector;

use Http\Client\Common\FlexibleHttpClient;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\HttplugBundle\ClientFactory\ClientFactory;

/**
* The ProfileClientFactory decorates any ClientFactory and returns the created client decorated by a ProfileClient.
*
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
*
* @internal
*/
class ProfileClientFactory implements ClientFactory
{
/**
* @var ClientFactory|callable
*/
private $factory;

/**
* @var Collector
*/
private $collector;

/**
* @param ClientFactory|callable $factory
* @param Collector $collector
*/
public function __construct($factory, Collector $collector)
{
if (!$factory instanceof ClientFactory && !is_callable($factory)) {
throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class));
}
$this->factory = $factory;
$this->collector = $collector;
}

/**
* {@inheritdoc}
*/
public function createClient(array $config = [])
{
$client = is_callable($this->factory) ? $this->factory($config) : $this->factory->createClient($config);

if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
$client = new FlexibleHttpClient($client);
}

return new ProfileClient($client, $this->collector);
}
}
42 changes: 42 additions & 0 deletions Collector/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ final class Stack
*/
private $failed = false;

/**
* @var string
*/
private $requestTarget;

/**
* @var string
*/
private $requestMethod;

/**
* @param string $client
* @param string $request
Expand Down Expand Up @@ -109,4 +119,36 @@ public function setFailed($failed)
{
$this->failed = $failed;
}

/**
* @return string
*/
public function getRequestTarget()
{
return $this->requestTarget;
}

/**
* @param string $requestTarget
*/
public function setRequestTarget($requestTarget)
{
$this->requestTarget = $requestTarget;
}

/**
* @return string
*/
public function getRequestMethod()
{
return $this->requestMethod;
}

/**
* @param string $requestMethod
*/
public function setRequestMethod($requestMethod)
{
$this->requestMethod = $requestMethod;
}
}
26 changes: 26 additions & 0 deletions Resources/config/data-collector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,31 @@
<service id="httplug.collector.twig.http_message" class="Http\HttplugBundle\Collector\Twig\HttpMessageMarkupExtension" public="false">
<tag name="twig.extension" />
</service>

<!-- ClientFactories -->
<service id="httplug.collector.factory.buzz" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.buzz" public="false">
<argument type="service" id="httplug.collector.factory.buzz.inner"/>
<argument type="service" id="httplug.collector.collector"/>
</service>
<service id="httplug.collector.factory.curl" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.curl" public="false">
<argument type="service" id="httplug.collector.factory.curl.inner"/>
<argument type="service" id="httplug.collector.collector"/>
</service>
<service id="httplug.collector.factory.guzzle5" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.guzzle5" public="false">
<argument type="service" id="httplug.collector.factory.guzzle5.inner"/>
<argument type="service" id="httplug.collector.collector"/>
</service>
<service id="httplug.collector.factory.guzzle6" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.guzzle6" public="false">
<argument type="service" id="httplug.collector.factory.guzzle6.inner"/>
<argument type="service" id="httplug.collector.collector"/>
</service>
<service id="httplug.collector.factory.react" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.react" public="false">
<argument type="service" id="httplug.collector.factory.react.inner"/>
<argument type="service" id="httplug.collector.collector"/>
</service>
<service id="httplug.collector.factory.socket" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.socket" public="false">
<argument type="service" id="httplug.collector.factory.socket.inner"/>
<argument type="service" id="httplug.collector.collector"/>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion Resources/views/webprofiler.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

{% for stack in collector.clientStacks(client) %}
<h3>
Request #{{ loop.index }}
Request #{{ loop.index }} - {{ stack.requestMethod }} {{ stack.requestTarget }}
{% if stack.failed %}
- <span class="httplug-error">Errored</span>
{% endif %}
Expand Down
108 changes: 108 additions & 0 deletions Tests/Unit/Collector/ProfileClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Http\HttplugBundle\Tests\Unit\Collector;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\HttplugBundle\Collector\Collector;
use Http\HttplugBundle\Collector\ProfileClient;
use Http\HttplugBundle\Collector\Stack;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ProfileClientTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Collector
*/
private $collector;

/**
* @var Stack
*/
private $currentStack;

/**
* @var HttpClient
*/
private $client;

/**
* @var RequestInterface
*/
private $request;

/**
* @var ProfileClient
*/
private $subject;

/**
* @var ResponseInterface
*/
private $response;

/**
* @var Promise
*/
private $promise;

public function setUp()
{
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
$this->currentStack = new Stack('default', 'FormattedRequest');
$this->client = $this->getMockBuilder(ClientInterface::class)->getMock();
$this->request = $this->getMockBuilder(RequestInterface::class)->getMock();
$this->subject = new ProfileClient($this->client, $this->collector);
$this->response = $this->getMockBuilder(ResponseInterface::class)->getMock();
$this->promise = $this->getMockBuilder(Promise::class)->getMock();

$this->client->method('sendRequest')->willReturn($this->response);
$this->client->method('sendAsyncRequest')->willReturn($this->promise);

$this->request->method('getMethod')->willReturn('GET');
$this->request->method('getRequestTarget')->willReturn('/target');

$this->collector->method('getCurrentStack')->willReturn($this->currentStack);
}

public function testCallDecoratedClient()
{
$this->client
->expects($this->once())
->method('sendRequest')
->with($this->identicalTo($this->request))
;

$this->client
->expects($this->once())
->method('sendAsyncRequest')
->with($this->identicalTo($this->request))
;

$this->assertEquals($this->response, $this->subject->sendRequest($this->request));

$this->assertEquals($this->promise, $this->subject->sendAsyncRequest($this->request));
}

public function testCollectRequestInformations()
{
$this->subject->sendRequest($this->request);

$this->assertEquals('GET', $this->currentStack->getRequestMethod());
$this->assertEquals('/target', $this->currentStack->getRequestTarget());
}

public function testCollectAsyncRequestInformations()
{
$this->subject->sendAsyncRequest($this->request);

$this->assertEquals('GET', $this->currentStack->getRequestMethod());
$this->assertEquals('/target', $this->currentStack->getRequestTarget());
}
}

interface ClientInterface extends HttpClient, HttpAsyncClient
Copy link
Collaborator

Choose a reason for hiding this comment

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

or just mock flexible client? not sure whats better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FlexibleHttpClient is final!

Copy link
Collaborator

Choose a reason for hiding this comment

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

does that prevent from doing $this->createMock(FlexiblehttpClient::class) ?

Copy link
Member

Choose a reason for hiding this comment

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

yes

Copy link
Collaborator

Choose a reason for hiding this comment

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

argh, thats quite stupid. ok then this is fine.

Copy link
Contributor Author

@fbourigault fbourigault Apr 3, 2017

Choose a reason for hiding this comment

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

That's the drawback of the final keyword. But this case is special as we have to Mock both HttpClient and HttpAsyncClient interface.

Should this interface moved in an other package (in the tests section)?

EDIT: having this interface here makes it not autoloadable which is good!

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah i think its right to have it here. its not something that anybody should rely on.

{
}