diff --git a/Collector/Profile.php b/Collector/Profile.php index b9ff0e5d..5543e216 100644 --- a/Collector/Profile.php +++ b/Collector/Profile.php @@ -33,12 +33,10 @@ final class Profile /** * @param string $plugin - * @param string $request */ - public function __construct($plugin, $request) + public function __construct($plugin) { $this->plugin = $plugin; - $this->request = $request; } /** @@ -57,6 +55,14 @@ public function getRequest() return $this->request; } + /** + * @param string $request + */ + public function setRequest($request) + { + $this->request = $request; + } + /** * @return string */ diff --git a/Collector/ProfilePlugin.php b/Collector/ProfilePlugin.php index a7223756..96d5b817 100644 --- a/Collector/ProfilePlugin.php +++ b/Collector/ProfilePlugin.php @@ -56,14 +56,28 @@ public function __construct(Plugin $plugin, Collector $collector, Formatter $for */ public function handleRequest(RequestInterface $request, callable $next, callable $first) { - $profile = new Profile($this->pluginName, $this->formatter->formatRequest($request)); + $profile = new Profile($this->pluginName); $stack = $this->collector->getCurrentStack(); if (null !== $stack) { $stack->addProfile($profile); } - return $this->plugin->handleRequest($request, $next, $first)->then(function (ResponseInterface $response) use ($profile) { + // wrap the next callback to profile the plugin request changes + $wrappedNext = function (RequestInterface $request) use ($next, $profile) { + $profile->setRequest($this->formatter->formatRequest($request)); + + return $next($request); + }; + + // wrap the first callback to profile the plugin request changes + $wrappedFirst = function (RequestInterface $request) use ($first, $profile) { + $profile->setRequest($this->formatter->formatRequest($request)); + + return $first($request); + }; + + return $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst)->then(function (ResponseInterface $response) use ($profile) { $profile->setResponse($this->formatter->formatResponse($response)); return $response; diff --git a/Tests/Unit/Collector/ProfilePluginTest.php b/Tests/Unit/Collector/ProfilePluginTest.php index 12912a03..e14daf48 100644 --- a/Tests/Unit/Collector/ProfilePluginTest.php +++ b/Tests/Unit/Collector/ProfilePluginTest.php @@ -77,7 +77,11 @@ public function setUp() $this->plugin ->method('handleRequest') - ->willReturn($this->promise) + ->willReturnCallback(function ($request, $next, $first) { + $next($request); + + return $this->promise; + }) ; $this->formatter @@ -128,6 +132,15 @@ public function testProfileIsInitialized() $this->assertCount(1, $this->currentStack->getProfiles()); $profile = $this->currentStack->getProfiles()[0]; $this->assertEquals('http.plugin.mock', $profile->getPlugin()); + } + + public function testCollectRequestInformations() + { + $this->subject->handleRequest($this->request, function () { + }, function () { + }); + + $profile = $this->currentStack->getProfiles()[0]; $this->assertEquals('FormattedRequest', $profile->getRequest()); }