4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16 use Symfony\Component\HttpFoundation\RequestStack;
17 use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
18 use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
19 use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
20 use Symfony\Component\HttpKernel\HttpKernel;
21 use Symfony\Component\HttpKernel\HttpKernelInterface;
22 use Symfony\Component\HttpKernel\KernelEvents;
23 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
24 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
25 use Symfony\Component\HttpFoundation\Request;
26 use Symfony\Component\HttpFoundation\Response;
27 use Symfony\Component\HttpFoundation\RedirectResponse;
28 use Symfony\Component\EventDispatcher\EventDispatcher;
30 class HttpKernelTest extends TestCase
33 * @expectedException \RuntimeException
35 public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
37 $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
39 $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
43 * @expectedException \RuntimeException
45 public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered()
47 $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
49 $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
52 public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithAHandlingListener()
54 $dispatcher = new EventDispatcher();
55 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
56 $event->setResponse(new Response($event->getException()->getMessage()));
59 $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException('foo'); });
60 $response = $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
62 $this->assertEquals('500', $response->getStatusCode());
63 $this->assertEquals('foo', $response->getContent());
66 public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithANonHandlingListener()
68 $exception = new \RuntimeException();
70 $dispatcher = new EventDispatcher();
71 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
72 // should set a response, but does not
75 $kernel = $this->getHttpKernel($dispatcher, function () use ($exception) { throw $exception; });
78 $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
79 $this->fail('LogicException expected');
80 } catch (\RuntimeException $e) {
81 $this->assertSame($exception, $e);
85 public function testHandleExceptionWithARedirectionResponse()
87 $dispatcher = new EventDispatcher();
88 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
89 $event->setResponse(new RedirectResponse('/login', 301));
92 $kernel = $this->getHttpKernel($dispatcher, function () { throw new AccessDeniedHttpException(); });
93 $response = $kernel->handle(new Request());
95 $this->assertEquals('301', $response->getStatusCode());
96 $this->assertEquals('/login', $response->headers->get('Location'));
99 public function testHandleHttpException()
101 $dispatcher = new EventDispatcher();
102 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
103 $event->setResponse(new Response($event->getException()->getMessage()));
106 $kernel = $this->getHttpKernel($dispatcher, function () { throw new MethodNotAllowedHttpException(array('POST')); });
107 $response = $kernel->handle(new Request());
109 $this->assertEquals('405', $response->getStatusCode());
110 $this->assertEquals('POST', $response->headers->get('Allow'));
114 * @dataProvider getStatusCodes
116 public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
118 $dispatcher = new EventDispatcher();
119 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) use ($responseStatusCode, $expectedStatusCode) {
120 $event->setResponse(new Response('', $responseStatusCode, array('X-Status-Code' => $expectedStatusCode)));
123 $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
124 $response = $kernel->handle(new Request());
126 $this->assertEquals($expectedStatusCode, $response->getStatusCode());
127 $this->assertFalse($response->headers->has('X-Status-Code'));
130 public function getStatusCodes()
140 public function testHandleWhenAListenerReturnsAResponse()
142 $dispatcher = new EventDispatcher();
143 $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
144 $event->setResponse(new Response('hello'));
147 $kernel = $this->getHttpKernel($dispatcher);
149 $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
153 * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
155 public function testHandleWhenNoControllerIsFound()
157 $dispatcher = new EventDispatcher();
158 $kernel = $this->getHttpKernel($dispatcher, false);
160 $kernel->handle(new Request());
163 public function testHandleWhenTheControllerIsAClosure()
165 $response = new Response('foo');
166 $dispatcher = new EventDispatcher();
167 $kernel = $this->getHttpKernel($dispatcher, function () use ($response) { return $response; });
169 $this->assertSame($response, $kernel->handle(new Request()));
172 public function testHandleWhenTheControllerIsAnObjectWithInvoke()
174 $dispatcher = new EventDispatcher();
175 $kernel = $this->getHttpKernel($dispatcher, new Controller());
177 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
180 public function testHandleWhenTheControllerIsAFunction()
182 $dispatcher = new EventDispatcher();
183 $kernel = $this->getHttpKernel($dispatcher, 'Symfony\Component\HttpKernel\Tests\controller_func');
185 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
188 public function testHandleWhenTheControllerIsAnArray()
190 $dispatcher = new EventDispatcher();
191 $kernel = $this->getHttpKernel($dispatcher, array(new Controller(), 'controller'));
193 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
196 public function testHandleWhenTheControllerIsAStaticArray()
198 $dispatcher = new EventDispatcher();
199 $kernel = $this->getHttpKernel($dispatcher, array('Symfony\Component\HttpKernel\Tests\Controller', 'staticcontroller'));
201 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
205 * @expectedException \LogicException
207 public function testHandleWhenTheControllerDoesNotReturnAResponse()
209 $dispatcher = new EventDispatcher();
210 $kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
212 $kernel->handle(new Request());
215 public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
217 $dispatcher = new EventDispatcher();
218 $dispatcher->addListener(KernelEvents::VIEW, function ($event) {
219 $event->setResponse(new Response($event->getControllerResult()));
222 $kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
224 $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
227 public function testHandleWithAResponseListener()
229 $dispatcher = new EventDispatcher();
230 $dispatcher->addListener(KernelEvents::RESPONSE, function ($event) {
231 $event->setResponse(new Response('foo'));
233 $kernel = $this->getHttpKernel($dispatcher);
235 $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
238 public function testHandleAllowChangingControllerArguments()
240 $dispatcher = new EventDispatcher();
241 $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function (FilterControllerArgumentsEvent $event) {
242 $event->setArguments(array('foo'));
245 $kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); });
247 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
250 public function testHandleAllowChangingControllerAndArguments()
252 $dispatcher = new EventDispatcher();
253 $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function (FilterControllerArgumentsEvent $event) {
254 $oldController = $event->getController();
255 $oldArguments = $event->getArguments();
257 $newController = function ($id) use ($oldController, $oldArguments) {
258 $response = call_user_func_array($oldController, $oldArguments);
260 $response->headers->set('X-Id', $id);
265 $event->setController($newController);
266 $event->setArguments(array('bar'));
269 $kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); }, null, array('foo'));
271 $this->assertResponseEquals(new Response('foo', 200, array('X-Id' => 'bar')), $kernel->handle(new Request()));
274 public function testTerminate()
276 $dispatcher = new EventDispatcher();
277 $kernel = $this->getHttpKernel($dispatcher);
278 $dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use (&$called, &$capturedKernel, &$capturedRequest, &$capturedResponse) {
280 $capturedKernel = $event->getKernel();
281 $capturedRequest = $event->getRequest();
282 $capturedResponse = $event->getResponse();
285 $kernel->terminate($request = Request::create('/'), $response = new Response());
286 $this->assertTrue($called);
287 $this->assertEquals($kernel, $capturedKernel);
288 $this->assertEquals($request, $capturedRequest);
289 $this->assertEquals($response, $capturedResponse);
292 public function testVerifyRequestStackPushPopDuringHandle()
294 $request = new Request();
296 $stack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->setMethods(array('push', 'pop'))->getMock();
297 $stack->expects($this->at(0))->method('push')->with($this->equalTo($request));
298 $stack->expects($this->at(1))->method('pop');
300 $dispatcher = new EventDispatcher();
301 $kernel = $this->getHttpKernel($dispatcher, null, $stack);
303 $kernel->handle($request, HttpKernelInterface::MASTER_REQUEST);
307 * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
309 public function testInconsistentClientIpsOnMasterRequests()
311 $request = new Request();
312 $request->setTrustedProxies(array('1.1.1.1'), -1);
313 $request->server->set('REMOTE_ADDR', '1.1.1.1');
314 $request->headers->set('FORWARDED', 'for=2.2.2.2');
315 $request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
317 $dispatcher = new EventDispatcher();
318 $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
319 $event->getRequest()->getClientIp();
322 $kernel = $this->getHttpKernel($dispatcher);
323 $kernel->handle($request, $kernel::MASTER_REQUEST, false);
326 private function getHttpKernel(EventDispatcherInterface $eventDispatcher, $controller = null, RequestStack $requestStack = null, array $arguments = array())
328 if (null === $controller) {
329 $controller = function () { return new Response('Hello'); };
332 $controllerResolver = $this->getMockBuilder(ControllerResolverInterface::class)->getMock();
334 ->expects($this->any())
335 ->method('getController')
336 ->will($this->returnValue($controller));
338 $argumentResolver = $this->getMockBuilder(ArgumentResolverInterface::class)->getMock();
340 ->expects($this->any())
341 ->method('getArguments')
342 ->will($this->returnValue($arguments));
344 return new HttpKernel($eventDispatcher, $controllerResolver, $requestStack, $argumentResolver);
347 private function assertResponseEquals(Response $expected, Response $actual)
349 $expected->setDate($actual->getDate());
350 $this->assertEquals($expected, $actual);
356 public function __invoke()
358 return new Response('foo');
361 public function controller()
363 return new Response('foo');
366 public static function staticController()
368 return new Response('foo');
372 function controller_func()
374 return new Response('foo');