From 26b90e47c0b69ba4a910f390758c21baa9000db5 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Tue, 18 Oct 2016 06:32:56 +0100 Subject: [PATCH 1/2] [HttpKernel] Refactor a RequestDataCollector test case to use a data provider --- .../RequestDataCollectorTest.php | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php b/src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php index eef00d4a024dd..6336fa4453e19 100644 --- a/src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php @@ -70,16 +70,28 @@ public function testKernelResponseDoesNotStartSession() } /** - * Test various types of controller callables. + * @dataProvider provideControllerCallables */ - public function testControllerInspection() + public function testControllerInspection($name, $callable, $expected) + { + $c = new RequestDataCollector(); + $request = $this->createRequest(); + $response = $this->createResponse(); + $this->injectController($c, $callable, $request); + $c->collect($request, $response); + + $this->assertSame($expected, $c->getController(), sprintf('Testing: %s', $name)); + } + + public function provideControllerCallables() { // make sure we always match the line number $r1 = new \ReflectionMethod($this, 'testControllerInspection'); $r2 = new \ReflectionMethod($this, 'staticControllerMethod'); $r3 = new \ReflectionClass($this); + // test name, callable, expected - $controllerTests = array( + return array( array( '"Regular" callable', array($this, 'testControllerInspection'), @@ -168,15 +180,6 @@ function () { return 'foo'; }, ), ), ); - - $c = new RequestDataCollector(); - $request = $this->createRequest(); - $response = $this->createResponse(); - foreach ($controllerTests as $controllerTest) { - $this->injectController($c, $controllerTest[1], $request); - $c->collect($request, $response); - $this->assertSame($controllerTest[2], $c->getController(), sprintf('Testing: %s', $controllerTest[0])); - } } protected function createRequest() From 57008ea42bd55f6d1378014961886b278468661e Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Tue, 18 Oct 2016 06:32:56 +0100 Subject: [PATCH 2/2] [HttpKernel] Fix a regression in the RequestDataCollector --- .../DataCollector/RequestDataCollector.php | 2 +- .../RequestDataCollectorTest.php | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php index fd66b38cd2592..f274ac79c2c71 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php @@ -374,7 +374,7 @@ protected function parseController($controller) ); } - return (string) $controller ?: 'n/a'; + return is_string($controller) ? $controller : 'n/a'; } private function getCookieHeader($name, $value, $expires, $path, $domain, $secure, $httponly) diff --git a/src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php b/src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php index 6336fa4453e19..df5d974fd8c30 100644 --- a/src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\HttpKernel\Tests\DataCollector; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; @@ -182,6 +183,17 @@ function () { return 'foo'; }, ); } + public function testItIgnoresInvalidCallables() + { + $request = $this->createRequestWithSession(); + $response = new RedirectResponse('/'); + + $c = new RequestDataCollector(); + $c->collect($request, $response); + + $this->assertSame('n/a', $c->getController()); + } + protected function createRequest() { $request = Request::create('http://test.com/foo?bar=baz'); @@ -194,6 +206,16 @@ protected function createRequest() return $request; } + private function createRequestWithSession() + { + $request = $this->createRequest(); + $request->attributes->set('_controller', 'Foo::bar'); + $request->setSession(new Session(new MockArraySessionStorage())); + $request->getSession()->start(); + + return $request; + } + protected function createResponse() { $response = new Response();