Skip to content

Don't use dependency injection decoration for plugin profiling #193

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
Jul 17, 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
9 changes: 5 additions & 4 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@ private function configureClient(ContainerBuilder $container, $clientName, array
$pluginClientOptions = [];
if ($profiling) {
//Decorate each plugin with a ProfilePlugin instance.
foreach ($plugins as $pluginServiceId) {
$plugins = array_map(function ($pluginServiceId) use ($container) {
$this->decoratePluginWithProfilePlugin($container, $pluginServiceId);
}

return $pluginServiceId.'.debug';
}, $plugins);

// To profile the requests, add a StackPlugin as first plugin in the chain.
$stackPluginId = $this->configureStackPlugin($container, $clientName, $serviceId);
Expand Down Expand Up @@ -427,9 +429,8 @@ private function configurePlugin(ContainerBuilder $container, $serviceId, $plugi
private function decoratePluginWithProfilePlugin(ContainerBuilder $container, $pluginServiceId)
{
$container->register($pluginServiceId.'.debug', ProfilePlugin::class)
->setDecoratedService($pluginServiceId)
->setArguments([
new Reference($pluginServiceId.'.debug.inner'),
new Reference($pluginServiceId),
new Reference('httplug.collector.collector'),
new Reference('httplug.collector.formatter'),
])
Expand Down
33 changes: 33 additions & 0 deletions Tests/Functional/ServiceInstantiationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Http\HttplugBundle\Tests\Functional;

use Http\Client\Common\Plugin\RedirectPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\HttplugBundle\Collector\Collector;
use Http\HttplugBundle\Collector\ProfileClient;
use Http\HttplugBundle\Collector\ProfilePlugin;
use Http\HttplugBundle\Collector\StackPlugin;
use Nyholm\NSA;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\Profiler\Profiler;

Expand Down Expand Up @@ -38,4 +44,31 @@ public function testDebugToolbar()
$collector = $profiler->get('httplug');
$this->assertInstanceOf(Collector::class, $collector);
}

public function testProfilingShouldNotChangeServiceReference()
{
static::bootKernel(['debug' => true]);
$container = static::$kernel->getContainer();

$this->assertInstanceof(RedirectPlugin::class, $container->get('app.http.plugin.custom'));
}

public function testProfilingDecoration()
{
static::bootKernel(['debug' => true]);
$container = static::$kernel->getContainer();

$client = $container->get('httplug.client.acme');

$this->assertInstanceOf(PluginClient::class, $client);
$this->assertInstanceOf(ProfileClient::class, NSA::getProperty($client, 'client'));

$plugins = NSA::getProperty($client, 'plugins');

$this->assertInstanceOf(StackPlugin::class, $plugins[0]);
$this->assertInstanceOf(ProfilePlugin::class, $plugins[1]);
$this->assertInstanceOf(ProfilePlugin::class, $plugins[2]);
$this->assertInstanceOf(ProfilePlugin::class, $plugins[3]);
$this->assertInstanceOf(ProfilePlugin::class, $plugins[4]);
}
}
6 changes: 5 additions & 1 deletion Tests/Resources/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ httplug:
-
decoder:
use_content_encoding: false
- httplug.plugin.redirect
- app.http.plugin.custom
-
add_host:
host: "http://localhost:8000"
Expand All @@ -22,3 +22,7 @@ httplug:
type: basic
username: foo
password: bar

services:
app.http.plugin.custom:
class: Http\Client\Common\Plugin\RedirectPlugin
16 changes: 8 additions & 8 deletions Tests/Unit/DependencyInjection/HttplugExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ public function testClientPlugins()

$plugins = [
'httplug.client.acme.plugin.stack',
'httplug.client.acme.plugin.decoder',
'httplug.plugin.redirect',
'httplug.client.acme.plugin.add_host',
'httplug.client.acme.plugin.header_append',
'httplug.client.acme.plugin.header_defaults',
'httplug.client.acme.plugin.header_set',
'httplug.client.acme.plugin.header_remove',
'httplug.client.acme.authentication.my_basic',
'httplug.client.acme.plugin.decoder.debug',
'httplug.plugin.redirect.debug',
'httplug.client.acme.plugin.add_host.debug',
'httplug.client.acme.plugin.header_append.debug',
'httplug.client.acme.plugin.header_defaults.debug',
'httplug.client.acme.plugin.header_set.debug',
'httplug.client.acme.plugin.header_remove.debug',
'httplug.client.acme.authentication.my_basic.debug',
];
$pluginReferences = array_map(function ($id) {
return new Reference($id);
Expand Down