From 556bfcb804b11f2027522d108007e4f7dff86076 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 2 Nov 2010 18:38:08 +0100 Subject: [PATCH] [HttpKernel] added some more unit tests --- .../Component/HttpKernel/BaseHttpKernel.php | 12 +- .../HttpKernel/BaseHttpKernelTest.php | 192 ++++++++++++++++++ .../Controller/ControllerResolverTest.php | 9 + .../Component/HttpKernel/HttpKernelTest.php | 69 +++++++ .../HttpKernel/ResponseListenerTest.php | 66 ++++++ 5 files changed, 344 insertions(+), 4 deletions(-) create mode 100644 tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php create mode 100644 tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php create mode 100644 tests/Symfony/Tests/Component/HttpKernel/ResponseListenerTest.php diff --git a/src/Symfony/Component/HttpKernel/BaseHttpKernel.php b/src/Symfony/Component/HttpKernel/BaseHttpKernel.php index 8be74c0582c0..9180a4f11ff1 100644 --- a/src/Symfony/Component/HttpKernel/BaseHttpKernel.php +++ b/src/Symfony/Component/HttpKernel/BaseHttpKernel.php @@ -84,7 +84,8 @@ public function handle(Request $request = null, $type = HttpKernelInterface::MAS } // exception - $event = $this->dispatcher->notifyUntil(new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e))); + $event = new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e)); + $this->dispatcher->notifyUntil($event); if ($event->isProcessed()) { return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type); } @@ -109,7 +110,8 @@ public function handle(Request $request = null, $type = HttpKernelInterface::MAS protected function handleRaw(Request $request, $type = self::MASTER_REQUEST) { // request - $event = $this->dispatcher->notifyUntil(new Event($this, 'core.request', array('request_type' => $type, 'request' => $request))); + $event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request)); + $this->dispatcher->notifyUntil($event); if ($event->isProcessed()) { return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type); } @@ -119,7 +121,8 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST) throw new NotFoundHttpException('Unable to find the controller.'); } - $event = $this->dispatcher->filter(new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request)), $controller); + $event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request)); + $this->dispatcher->filter($event, $controller); $controller = $event->getReturnValue(); // controller must be a callable @@ -134,7 +137,8 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST) $retval = call_user_func_array($controller, $arguments); // view - $event = $this->dispatcher->filter(new Event($this, 'core.view', array('request_type' => $type, 'request' => $request)), $retval); + $event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request)); + $this->dispatcher->filter($event, $retval); return $this->filterResponse($event->getReturnValue(), $request, sprintf('The controller must return a response (instead of %s).', is_object($event->getReturnValue()) ? 'an object of class '.get_class($event->getReturnValue()) : is_array($event->getReturnValue()) ? 'an array' : str_replace("\n", '', var_export($event->getReturnValue(), true))), $type); } diff --git a/tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php b/tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php new file mode 100644 index 000000000000..ee9341e93d3b --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php @@ -0,0 +1,192 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpKernel; + +use Symfony\Component\HttpKernel\BaseHttpKernel; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\EventDispatcher\EventDispatcher; + +class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase +{ + public function testHandleChangingMasterRequest() + { + $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver()); + + $kernel->handle(); + $this->assertInstanceof('Symfony\Component\HttpFoundation\Request', $kernel->getRequest()); + + $request = Request::create('/'); + $kernel->handle($request); + $this->assertSame($request, $kernel->getRequest()); + + $subRequest = Request::create('/'); + $kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST); + $this->assertSame($request, $kernel->getRequest()); + } + + /** + * @expectedException RuntimeException + */ + public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue() + { + $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); })); + + $kernel->handle(null, HttpKernelInterface::MASTER_REQUEST, true); + } + + /** + * @expectedException RuntimeException + */ + public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalseAndNoListenerIsRegistered() + { + $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); })); + + $kernel->handle(null, HttpKernelInterface::MASTER_REQUEST, false); + } + + public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse() + { + $dispatcher = new EventDispatcher(); + $dispatcher->connect('core.exception', function ($event) + { + $event->setReturnValue(new Response($event->getParameter('exception')->getMessage())); + + return true; + }); + + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); })); + + $this->assertEquals('foo', $kernel->handle()->getContent()); + } + + public function testHandleWhenAListenerReturnsAResponse() + { + $dispatcher = new EventDispatcher(); + $dispatcher->connect('core.request', function ($event) + { + $event->setReturnValue(new Response('hello')); + + return true; + }); + + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver()); + + $this->assertEquals('hello', $kernel->handle()->getContent()); + } + + /** + * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException + */ + public function testHandleWhenNoControllerIsFound() + { + $dispatcher = new EventDispatcher(); + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(false)); + + $kernel->handle(); + } + + /** + * @expectedException LogicException + */ + public function testHandleWhenNoControllerIsNotACallable() + { + $dispatcher = new EventDispatcher(); + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver('foobar')); + + $kernel->handle(); + } + + /** + * @expectedException RuntimeException + */ + public function testHandleWhenControllerDoesNotReturnAResponse() + { + $dispatcher = new EventDispatcher(); + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; })); + + $kernel->handle(); + } + + public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegistered() + { + $dispatcher = new EventDispatcher(); + $dispatcher->connect('core.view', function ($event, $retval) + { + return new Response($retval); + }); + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; })); + + $this->assertEquals('foo', $kernel->handle()->getContent()); + } + + /** + * @expectedException RuntimeException + */ + public function testHandleWhenAViewDoesNotReturnAResponse() + { + $dispatcher = new EventDispatcher(); + $dispatcher->connect('core.view', function ($event, $retval) + { + return $retval; + }); + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; })); + + $kernel->handle(); + } + + /** + * @expectedException RuntimeException + */ + public function testHandleWhenAResponseListenerDoesNotReturnAResponse() + { + $dispatcher = new EventDispatcher(); + $dispatcher->connect('core.response', function ($event, $response) + { + return 'foo'; + }); + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver()); + + $kernel->handle(); + } + + public function testHandleWithAResponseListener() + { + $dispatcher = new EventDispatcher(); + $dispatcher->connect('core.response', function ($event, $response) + { + return new Response('foo'); + }); + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver()); + + $this->assertEquals('foo', $kernel->handle()->getContent()); + } + + protected function getResolver($controller = null) + { + if (null === $controller) { + $controller = function () { return new Response('Hello'); }; + } + $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'); + $resolver->expects($this->any()) + ->method('getController') + ->will($this->returnValue($controller)) + ; + $resolver->expects($this->any()) + ->method('getArguments') + ->will($this->returnValue(array())) + ; + + return $resolver; + } +} diff --git a/tests/Symfony/Tests/Component/HttpKernel/Controller/ControllerResolverTest.php b/tests/Symfony/Tests/Component/HttpKernel/Controller/ControllerResolverTest.php index 60690ca6bd64..6825bc6d3347 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/Controller/ControllerResolverTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/Controller/ControllerResolverTest.php @@ -32,6 +32,10 @@ public function testGetController() $this->assertInstanceOf('Symfony\Tests\Component\HttpKernel\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable'); $this->assertEquals(array('Using controller "Symfony\Tests\Component\HttpKernel\ControllerResolverTest::testGetController"'), $logger->getLogs('info')); + $request->attributes->set('_controller', $lambda = function () {}); + $controller = $resolver->getController($request); + $this->assertSame($lambda, $controller); + $request->attributes->set('_controller', 'foo'); try { $resolver->getController($request); @@ -78,6 +82,11 @@ public function testGetArguments() $request->attributes->set('bar', 'bar'); $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes'); + $request = Request::create('/'); + $request->attributes->set('foo', 'foo'); + $controller = function ($foo) {}; + $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller)); + $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $request->attributes->set('foobar', 'foobar'); diff --git a/tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php b/tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php new file mode 100644 index 000000000000..c9d2a7f28a3b --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpKernel; + +use Symfony\Component\HttpKernel\HttpKernel; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\EventDispatcher\EventDispatcher; + +class HttpKernelTest extends \PHPUnit_Framework_TestCase +{ + public function testHandleGetsTheRequestFromTheContainer() + { + $request = Request::create('/'); + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->any()) + ->method('get') + ->will($this->returnValue($request)) + ; + + $kernel = new HttpKernel($container, new EventDispatcher(), $this->getResolver()); + + $kernel->handle(); + + $this->assertEquals($request, $kernel->getRequest()); + } + + public function testHandleSetsTheRequestIfPassed() + { + $request = Request::create('/'); + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->exactly(2)) + ->method('set') + ->with('request', $request) + ; + + $kernel = new HttpKernel($container, new EventDispatcher(), $this->getResolver()); + + $kernel->handle($request); + } + + protected function getResolver($controller = null) + { + if (null === $controller) { + $controller = function () { return new Response('Hello'); }; + } + $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'); + $resolver->expects($this->any()) + ->method('getController') + ->will($this->returnValue($controller)) + ; + $resolver->expects($this->any()) + ->method('getArguments') + ->will($this->returnValue(array())) + ; + + return $resolver; + } +} diff --git a/tests/Symfony/Tests/Component/HttpKernel/ResponseListenerTest.php b/tests/Symfony/Tests/Component/HttpKernel/ResponseListenerTest.php new file mode 100644 index 000000000000..d019f339d7e5 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/ResponseListenerTest.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpKernel; + +use Symfony\Component\HttpKernel\ResponseListener; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\HttpKernelInterface; + +class ResponseListenerTest extends \PHPUnit_Framework_TestCase +{ + public function testFilterDoesNothingForSubRequests() + { + $event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST)); + $response = new Response('foo'); + + $this->assertEquals(array(), $this->getResponseListener()->filter($event, $response)->headers->all()); + } + + public function testFilterDoesNothingIfContentTypeIsSet() + { + $event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST)); + $response = new Response('foo'); + $response->headers->set('Content-Type', 'text/plain'); + + $this->assertEquals(array('content-type' => array('text/plain')), $this->getResponseListener()->filter($event, $response)->headers->all()); + } + + public function testFilterDoesNothingIfRequestFormatIsNotDefined() + { + $event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => Request::create('/'))); + $response = new Response('foo'); + + $this->assertEquals(array(), $this->getResponseListener()->filter($event, $response)->headers->all()); + } + + public function testFilterSetContentType() + { + $request = Request::create('/'); + $request->setRequestFormat('json'); + $event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => $request)); + $response = new Response('foo'); + + $this->assertEquals(array('content-type' => array('application/json')), $this->getResponseListener()->filter($event, $response)->headers->all()); + } + + protected function getResponseListener() + { + $dispatcher = new EventDispatcher(); + $listener = new ResponseListener(); + $listener->register($dispatcher); + + return $listener; + } +}