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\Event\GetResponseForExceptionEvent;
21 use Symfony\Component\HttpKernel\HttpKernel;
22 use Symfony\Component\HttpKernel\HttpKernelInterface;
23 use Symfony\Component\HttpKernel\KernelEvents;
24 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
25 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
26 use Symfony\Component\HttpFoundation\Request;
27 use Symfony\Component\HttpFoundation\Response;
28 use Symfony\Component\HttpFoundation\RedirectResponse;
29 use Symfony\Component\EventDispatcher\EventDispatcher;
31 class HttpKernelTest extends TestCase
34 * @expectedException \RuntimeException
36 public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
38 $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
40 $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
44 * @expectedException \RuntimeException
46 public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered()
48 $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
50 $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
53 public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithAHandlingListener()
55 $dispatcher = new EventDispatcher();
56 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
57 $event->setResponse(new Response($event->getException()->getMessage()));
60 $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException('foo'); });
61 $response = $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
63 $this->assertEquals('500', $response->getStatusCode());
64 $this->assertEquals('foo', $response->getContent());
67 public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithANonHandlingListener()
69 $exception = new \RuntimeException();
71 $dispatcher = new EventDispatcher();
72 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
73 // should set a response, but does not
76 $kernel = $this->getHttpKernel($dispatcher, function () use ($exception) { throw $exception; });
79 $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
80 $this->fail('LogicException expected');
81 } catch (\RuntimeException $e) {
82 $this->assertSame($exception, $e);
86 public function testHandleExceptionWithARedirectionResponse()
88 $dispatcher = new EventDispatcher();
89 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
90 $event->setResponse(new RedirectResponse('/login', 301));
93 $kernel = $this->getHttpKernel($dispatcher, function () { throw new AccessDeniedHttpException(); });
94 $response = $kernel->handle(new Request());
96 $this->assertEquals('301', $response->getStatusCode());
97 $this->assertEquals('/login', $response->headers->get('Location'));
100 public function testHandleHttpException()
102 $dispatcher = new EventDispatcher();
103 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
104 $event->setResponse(new Response($event->getException()->getMessage()));
107 $kernel = $this->getHttpKernel($dispatcher, function () { throw new MethodNotAllowedHttpException(array('POST')); });
108 $response = $kernel->handle(new Request());
110 $this->assertEquals('405', $response->getStatusCode());
111 $this->assertEquals('POST', $response->headers->get('Allow'));
116 * @dataProvider getStatusCodes
118 public function testLegacyHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
120 $dispatcher = new EventDispatcher();
121 $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) use ($responseStatusCode, $expectedStatusCode) {
122 $event->setResponse(new Response('', $responseStatusCode, array('X-Status-Code' => $expectedStatusCode)));
125 $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
126 $response = $kernel->handle(new Request());
128 $this->assertEquals($expectedStatusCode, $response->getStatusCode());
129 $this->assertFalse($response->headers->has('X-Status-Code'));
132 public function getStatusCodes()
143 * @dataProvider getSpecificStatusCodes
145 public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($expectedStatusCode)
147 $dispatcher = new EventDispatcher();
148 $dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) use ($expectedStatusCode) {
149 $event->allowCustomResponseCode();
150 $event->setResponse(new Response('', $expectedStatusCode));
153 $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
154 $response = $kernel->handle(new Request());
156 $this->assertEquals($expectedStatusCode, $response->getStatusCode());
159 public function getSpecificStatusCodes()
168 public function testHandleWhenAListenerReturnsAResponse()
170 $dispatcher = new EventDispatcher();
171 $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
172 $event->setResponse(new Response('hello'));
175 $kernel = $this->getHttpKernel($dispatcher);
177 $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
181 * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
183 public function testHandleWhenNoControllerIsFound()
185 $dispatcher = new EventDispatcher();
186 $kernel = $this->getHttpKernel($dispatcher, false);
188 $kernel->handle(new Request());
191 public function testHandleWhenTheControllerIsAClosure()
193 $response = new Response('foo');
194 $dispatcher = new EventDispatcher();
195 $kernel = $this->getHttpKernel($dispatcher, function () use ($response) { return $response; });
197 $this->assertSame($response, $kernel->handle(new Request()));
200 public function testHandleWhenTheControllerIsAnObjectWithInvoke()
202 $dispatcher = new EventDispatcher();
203 $kernel = $this->getHttpKernel($dispatcher, new Controller());
205 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
208 public function testHandleWhenTheControllerIsAFunction()
210 $dispatcher = new EventDispatcher();
211 $kernel = $this->getHttpKernel($dispatcher, 'Symfony\Component\HttpKernel\Tests\controller_func');
213 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
216 public function testHandleWhenTheControllerIsAnArray()
218 $dispatcher = new EventDispatcher();
219 $kernel = $this->getHttpKernel($dispatcher, array(new Controller(), 'controller'));
221 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
224 public function testHandleWhenTheControllerIsAStaticArray()
226 $dispatcher = new EventDispatcher();
227 $kernel = $this->getHttpKernel($dispatcher, array('Symfony\Component\HttpKernel\Tests\Controller', 'staticcontroller'));
229 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
233 * @expectedException \LogicException
235 public function testHandleWhenTheControllerDoesNotReturnAResponse()
237 $dispatcher = new EventDispatcher();
238 $kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
240 $kernel->handle(new Request());
243 public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
245 $dispatcher = new EventDispatcher();
246 $dispatcher->addListener(KernelEvents::VIEW, function ($event) {
247 $event->setResponse(new Response($event->getControllerResult()));
250 $kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
252 $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
255 public function testHandleWithAResponseListener()
257 $dispatcher = new EventDispatcher();
258 $dispatcher->addListener(KernelEvents::RESPONSE, function ($event) {
259 $event->setResponse(new Response('foo'));
261 $kernel = $this->getHttpKernel($dispatcher);
263 $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
266 public function testHandleAllowChangingControllerArguments()
268 $dispatcher = new EventDispatcher();
269 $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function (FilterControllerArgumentsEvent $event) {
270 $event->setArguments(array('foo'));
273 $kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); });
275 $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
278 public function testHandleAllowChangingControllerAndArguments()
280 $dispatcher = new EventDispatcher();
281 $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function (FilterControllerArgumentsEvent $event) {
282 $oldController = $event->getController();
283 $oldArguments = $event->getArguments();
285 $newController = function ($id) use ($oldController, $oldArguments) {
286 $response = call_user_func_array($oldController, $oldArguments);
288 $response->headers->set('X-Id', $id);
293 $event->setController($newController);
294 $event->setArguments(array('bar'));
297 $kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); }, null, array('foo'));
299 $this->assertResponseEquals(new Response('foo', 200, array('X-Id' => 'bar')), $kernel->handle(new Request()));
302 public function testTerminate()
304 $dispatcher = new EventDispatcher();
305 $kernel = $this->getHttpKernel($dispatcher);
306 $dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use (&$called, &$capturedKernel, &$capturedRequest, &$capturedResponse) {
308 $capturedKernel = $event->getKernel();
309 $capturedRequest = $event->getRequest();
310 $capturedResponse = $event->getResponse();
313 $kernel->terminate($request = Request::create('/'), $response = new Response());
314 $this->assertTrue($called);
315 $this->assertEquals($kernel, $capturedKernel);
316 $this->assertEquals($request, $capturedRequest);
317 $this->assertEquals($response, $capturedResponse);
320 public function testVerifyRequestStackPushPopDuringHandle()
322 $request = new Request();
324 $stack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->setMethods(array('push', 'pop'))->getMock();
325 $stack->expects($this->at(0))->method('push')->with($this->equalTo($request));
326 $stack->expects($this->at(1))->method('pop');
328 $dispatcher = new EventDispatcher();
329 $kernel = $this->getHttpKernel($dispatcher, null, $stack);
331 $kernel->handle($request, HttpKernelInterface::MASTER_REQUEST);
335 * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
337 public function testInconsistentClientIpsOnMasterRequests()
339 $request = new Request();
340 $request->setTrustedProxies(array('1.1.1.1'), Request::HEADER_X_FORWARDED_FOR | Request::HEADER_FORWARDED);
341 $request->server->set('REMOTE_ADDR', '1.1.1.1');
342 $request->headers->set('FORWARDED', 'for=2.2.2.2');
343 $request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
345 $dispatcher = new EventDispatcher();
346 $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
347 $event->getRequest()->getClientIp();
350 $kernel = $this->getHttpKernel($dispatcher);
351 $kernel->handle($request, $kernel::MASTER_REQUEST, false);
354 private function getHttpKernel(EventDispatcherInterface $eventDispatcher, $controller = null, RequestStack $requestStack = null, array $arguments = array())
356 if (null === $controller) {
357 $controller = function () { return new Response('Hello'); };
360 $controllerResolver = $this->getMockBuilder(ControllerResolverInterface::class)->getMock();
362 ->expects($this->any())
363 ->method('getController')
364 ->will($this->returnValue($controller));
366 $argumentResolver = $this->getMockBuilder(ArgumentResolverInterface::class)->getMock();
368 ->expects($this->any())
369 ->method('getArguments')
370 ->will($this->returnValue($arguments));
372 return new HttpKernel($eventDispatcher, $controllerResolver, $requestStack, $argumentResolver);
375 private function assertResponseEquals(Response $expected, Response $actual)
377 $expected->setDate($actual->getDate());
378 $this->assertEquals($expected, $actual);
384 public function __invoke()
386 return new Response('foo');
389 public function controller()
391 return new Response('foo');
394 public static function staticController()
396 return new Response('foo');
400 function controller_func()
402 return new Response('foo');