Skip to content

Commit aa75adf

Browse files
committed
bug #20239 [HttpKernel] Fix a regression in the RequestDataCollector (jakzal)
This PR was merged into the 3.1 branch. Discussion ---------- [HttpKernel] Fix a regression in the RequestDataCollector | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19701 | License | MIT | Doc PR | - The regression was introduced by refactoring made as part of #17589 (if/else statements where rearranged). Commits ------- 57008ea [HttpKernel] Fix a regression in the RequestDataCollector 26b90e4 [HttpKernel] Refactor a RequestDataCollector test case to use a data provider
2 parents 57f8d1e + 57008ea commit aa75adf

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ protected function parseController($controller)
374374
);
375375
}
376376

377-
return (string) $controller ?: 'n/a';
377+
return is_string($controller) ? $controller : 'n/a';
378378
}
379379

380380
private function getCookieHeader($name, $value, $expires, $path, $domain, $secure, $httponly)

src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php

+35-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests\DataCollector;
1313

14+
use Symfony\Component\HttpFoundation\RedirectResponse;
1415
use Symfony\Component\HttpFoundation\Session\Session;
1516
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
1617
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
@@ -70,16 +71,28 @@ public function testKernelResponseDoesNotStartSession()
7071
}
7172

7273
/**
73-
* Test various types of controller callables.
74+
* @dataProvider provideControllerCallables
7475
*/
75-
public function testControllerInspection()
76+
public function testControllerInspection($name, $callable, $expected)
77+
{
78+
$c = new RequestDataCollector();
79+
$request = $this->createRequest();
80+
$response = $this->createResponse();
81+
$this->injectController($c, $callable, $request);
82+
$c->collect($request, $response);
83+
84+
$this->assertSame($expected, $c->getController(), sprintf('Testing: %s', $name));
85+
}
86+
87+
public function provideControllerCallables()
7688
{
7789
// make sure we always match the line number
7890
$r1 = new \ReflectionMethod($this, 'testControllerInspection');
7991
$r2 = new \ReflectionMethod($this, 'staticControllerMethod');
8092
$r3 = new \ReflectionClass($this);
93+
8194
// test name, callable, expected
82-
$controllerTests = array(
95+
return array(
8396
array(
8497
'"Regular" callable',
8598
array($this, 'testControllerInspection'),
@@ -168,15 +181,17 @@ function () { return 'foo'; },
168181
),
169182
),
170183
);
184+
}
185+
186+
public function testItIgnoresInvalidCallables()
187+
{
188+
$request = $this->createRequestWithSession();
189+
$response = new RedirectResponse('/');
171190

172191
$c = new RequestDataCollector();
173-
$request = $this->createRequest();
174-
$response = $this->createResponse();
175-
foreach ($controllerTests as $controllerTest) {
176-
$this->injectController($c, $controllerTest[1], $request);
177-
$c->collect($request, $response);
178-
$this->assertSame($controllerTest[2], $c->getController(), sprintf('Testing: %s', $controllerTest[0]));
179-
}
192+
$c->collect($request, $response);
193+
194+
$this->assertSame('n/a', $c->getController());
180195
}
181196

182197
protected function createRequest()
@@ -191,6 +206,16 @@ protected function createRequest()
191206
return $request;
192207
}
193208

209+
private function createRequestWithSession()
210+
{
211+
$request = $this->createRequest();
212+
$request->attributes->set('_controller', 'Foo::bar');
213+
$request->setSession(new Session(new MockArraySessionStorage()));
214+
$request->getSession()->start();
215+
216+
return $request;
217+
}
218+
194219
protected function createResponse()
195220
{
196221
$response = new Response();

0 commit comments

Comments
 (0)