Skip to content

Commit 2619782

Browse files
committed
don't use dependency injection decoration for plugin profiling
1 parent 227eee2 commit 2619782

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

DependencyInjection/HttplugExtension.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,11 @@ private function configureClient(ContainerBuilder $container, $clientName, array
273273
$pluginClientOptions = [];
274274
if ($profiling) {
275275
//Decorate each plugin with a ProfilePlugin instance.
276-
foreach ($plugins as $pluginServiceId) {
276+
$plugins = array_map(function ($pluginServiceId) use ($container) {
277277
$this->decoratePluginWithProfilePlugin($container, $pluginServiceId);
278-
}
278+
279+
return $pluginServiceId.'.debug';
280+
}, $plugins);
279281

280282
// To profile the requests, add a StackPlugin as first plugin in the chain.
281283
$stackPluginId = $this->configureStackPlugin($container, $clientName, $serviceId);
@@ -427,9 +429,8 @@ private function configurePlugin(ContainerBuilder $container, $serviceId, $plugi
427429
private function decoratePluginWithProfilePlugin(ContainerBuilder $container, $pluginServiceId)
428430
{
429431
$container->register($pluginServiceId.'.debug', ProfilePlugin::class)
430-
->setDecoratedService($pluginServiceId)
431432
->setArguments([
432-
new Reference($pluginServiceId.'.debug.inner'),
433+
new Reference($pluginServiceId),
433434
new Reference('httplug.collector.collector'),
434435
new Reference('httplug.collector.formatter'),
435436
])

Tests/Functional/ServiceInstantiationTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace Http\HttplugBundle\Tests\Functional;
44

5+
use Http\Client\Common\Plugin\RedirectPlugin;
6+
use Http\Client\Common\PluginClient;
57
use Http\Client\HttpClient;
68
use Http\HttplugBundle\Collector\Collector;
9+
use Http\HttplugBundle\Collector\ProfileClient;
10+
use Http\HttplugBundle\Collector\ProfilePlugin;
11+
use Http\HttplugBundle\Collector\StackPlugin;
12+
use Nyholm\NSA;
713
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
814
use Symfony\Component\HttpKernel\Profiler\Profiler;
915

@@ -38,4 +44,31 @@ public function testDebugToolbar()
3844
$collector = $profiler->get('httplug');
3945
$this->assertInstanceOf(Collector::class, $collector);
4046
}
47+
48+
public function testProfilingShouldNotChangeServiceReference()
49+
{
50+
static::bootKernel(['debug' => true]);
51+
$container = static::$kernel->getContainer();
52+
53+
$this->assertInstanceof(RedirectPlugin::class, $container->get('app.http.plugin.custom'));
54+
}
55+
56+
public function testProfilingDecoration()
57+
{
58+
static::bootKernel(['debug' => true]);
59+
$container = static::$kernel->getContainer();
60+
61+
$client = $container->get('httplug.client.acme');
62+
63+
$this->assertInstanceOf(PluginClient::class, $client);
64+
$this->assertInstanceOf(ProfileClient::class, NSA::getProperty($client, 'client'));
65+
66+
$plugins = NSA::getProperty($client, 'plugins');
67+
68+
$this->assertInstanceOf(StackPlugin::class, $plugins[0]);
69+
$this->assertInstanceOf(ProfilePlugin::class, $plugins[1]);
70+
$this->assertInstanceOf(ProfilePlugin::class, $plugins[2]);
71+
$this->assertInstanceOf(ProfilePlugin::class, $plugins[3]);
72+
$this->assertInstanceOf(ProfilePlugin::class, $plugins[4]);
73+
}
4174
}

Tests/Resources/app/config/config_test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ httplug:
1212
-
1313
decoder:
1414
use_content_encoding: false
15-
- httplug.plugin.redirect
15+
- app.http.plugin.custom
1616
-
1717
add_host:
1818
host: "http://localhost:8000"
@@ -22,3 +22,7 @@ httplug:
2222
type: basic
2323
username: foo
2424
password: bar
25+
26+
services:
27+
app.http.plugin.custom:
28+
class: Http\Client\Common\Plugin\RedirectPlugin

Tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ public function testClientPlugins()
117117

118118
$plugins = [
119119
'httplug.client.acme.plugin.stack',
120-
'httplug.client.acme.plugin.decoder',
121-
'httplug.plugin.redirect',
122-
'httplug.client.acme.plugin.add_host',
123-
'httplug.client.acme.plugin.header_append',
124-
'httplug.client.acme.plugin.header_defaults',
125-
'httplug.client.acme.plugin.header_set',
126-
'httplug.client.acme.plugin.header_remove',
127-
'httplug.client.acme.authentication.my_basic',
120+
'httplug.client.acme.plugin.decoder.debug',
121+
'httplug.plugin.redirect.debug',
122+
'httplug.client.acme.plugin.add_host.debug',
123+
'httplug.client.acme.plugin.header_append.debug',
124+
'httplug.client.acme.plugin.header_defaults.debug',
125+
'httplug.client.acme.plugin.header_set.debug',
126+
'httplug.client.acme.plugin.header_remove.debug',
127+
'httplug.client.acme.authentication.my_basic.debug',
128128
];
129129
$pluginReferences = array_map(function ($id) {
130130
return new Reference($id);

0 commit comments

Comments
 (0)