From 3349d702b478715d2b85054f44b93683e28e18e3 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 27 Jul 2015 14:10:36 +0100 Subject: [PATCH] Issue #2497693 by marvin_B8, joshi.rohit100, borisson_, Crell: Add PSR-7 to Symfony Response View listener --- core/core.services.yml | 5 + .../EventSubscriber/PsrResponseSubscriber.php | 62 ++++++++++++ .../system/src/Tests/Routing/RouterTest.php | 9 ++ .../router_test.routing.yml | 7 ++ .../src/TestControllers.php | 6 ++ .../PsrResponseSubscriberTest.php | 99 +++++++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php create mode 100644 core/tests/Drupal/Tests/Core/EventSubscriber/PsrResponseSubscriberTest.php diff --git a/core/core.services.yml b/core/core.services.yml index e2939e907e..f330708af8 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -890,6 +890,11 @@ services: class: Drupal\Core\EventSubscriber\RouteMethodSubscriber tags: - { name: event_subscriber } + psr_response_view_subscriber: + class: Drupal\Core\EventSubscriber\PsrResponseSubscriber + arguments: ['@psr7.http_foundation_factory'] + tags: + - { name: event_subscriber } # Main content view subscriber plus the renderers it uses. main_content_view_subscriber: diff --git a/core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php new file mode 100644 index 0000000000..eb0818c873 --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php @@ -0,0 +1,62 @@ +httpFoundationFactory = $http_foundation_factory; + } + + /** + * Converts a PSR-7 response to a Symfony response. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event + * The Event to process. + */ + public function onKernelView(GetResponseForControllerResultEvent $event) { + $controller_result = $event->getControllerResult(); + + if ($controller_result instanceof ResponseInterface) { + $event->setResponse($this->httpFoundationFactory->createResponse($controller_result)); + } + + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[KernelEvents::VIEW][] = ['onKernelView']; + return $events; + } + +} diff --git a/core/modules/system/src/Tests/Routing/RouterTest.php b/core/modules/system/src/Tests/Routing/RouterTest.php index b945676744..207049f2ad 100644 --- a/core/modules/system/src/Tests/Routing/RouterTest.php +++ b/core/modules/system/src/Tests/Routing/RouterTest.php @@ -199,6 +199,15 @@ public function testRouterMatching() { $this->assertText('Route not matched.'); } + /** + * Tests that a PSR-7 response works. + */ + public function testRouterResponsePsr7() { + $this->drupalGet('/router_test/test23'); + $this->assertResponse(200); + $this->assertText('test23'); + } + /** * Tests the user account on the DIC. */ diff --git a/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml b/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml index 5debfc219c..36a70171c3 100644 --- a/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml +++ b/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml @@ -141,6 +141,13 @@ router_test.22: requirements: _role: 'anonymous' +router_test.23: + path: '/router_test/test23' + defaults: + _controller: '\Drupal\router_test\TestControllers::test23' + requirements: + _access: 'TRUE' + router_test.hierarchy_parent: path: '/menu-test/parent' defaults: diff --git a/core/modules/system/tests/modules/router_test_directory/src/TestControllers.php b/core/modules/system/tests/modules/router_test_directory/src/TestControllers.php index 614c9a8504..f3ff517039 100644 --- a/core/modules/system/tests/modules/router_test_directory/src/TestControllers.php +++ b/core/modules/system/tests/modules/router_test_directory/src/TestControllers.php @@ -12,6 +12,8 @@ use Drupal\user\UserInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Response; +use Zend\Diactoros\Response\HtmlResponse; + /** * Controller routines for testing the routing system. @@ -102,6 +104,10 @@ public function test21() { return new CacheableResponse('test21'); } + public function test23() { + return new HtmlResponse('test23'); + } + /** * Throws an exception. * diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/PsrResponseSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/PsrResponseSubscriberTest.php new file mode 100644 index 0000000000..2e3552ef4e --- /dev/null +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/PsrResponseSubscriberTest.php @@ -0,0 +1,99 @@ +getMock('Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface', [], [], '', NULL); + $factory + ->expects($this->any()) + ->method('createResponse') + ->willReturn($this->getMock('Symfony\Component\HttpFoundation\Response')); + + $this->httpFoundationFactoryMock = $factory; + + $this->psrResponseSubscriber = new PsrResponseSubscriber($this->httpFoundationFactoryMock); + } + + /** + * Tests altering and finished event. + * + * @covers ::onKernelView + */ + public function testConvertsControllerResult() { + $event = $this->createEventMock($this->getMock('Psr\Http\Message\ResponseInterface')); + $event + ->expects($this->once()) + ->method('setResponse') + ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Response')); + $this->psrResponseSubscriber->onKernelView($event); + } + + + /** + * Tests altering and finished event. + * + * @covers ::onKernelView + */ + public function testDoesNotConvertControllerResult() { + $event = $this->createEventMock([]); + $event + ->expects($this->never()) + ->method('setResponse'); + $this->psrResponseSubscriber->onKernelView($event); + $event = $this->createEventMock(NULL); + $event + ->expects($this->never()) + ->method('setResponse'); + $this->psrResponseSubscriber->onKernelView($event); + } + + /** + * Sets up an alias event that return $controllerResult. + * + * @param mixed $controller_result + * The return Object. + * + * @return \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent|\PHPUnit_Framework_MockObject_MockObject + * A mock object to test. + */ + protected function createEventMock($controller_result) { + $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent', [], [], '', NULL); + $event + ->expects($this->once()) + ->method('getControllerResult') + ->willReturn($controller_result); + return $event; + } + +} -- GitLab