HttpKernelTest.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\HttpKernel;
  11. use Symfony\Component\HttpKernel\HttpKernel;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\EventDispatcher\EventDispatcher;
  17. class HttpKernelTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @expectedException RuntimeException
  21. */
  22. public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue()
  23. {
  24. $kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
  25. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
  26. }
  27. /**
  28. * @expectedException RuntimeException
  29. */
  30. public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalseAndNoListenerIsRegistered()
  31. {
  32. $kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
  33. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
  34. }
  35. public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse()
  36. {
  37. $dispatcher = new EventDispatcher();
  38. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  39. $event->setResponse(new Response($event->getException()->getMessage()));
  40. });
  41. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); }));
  42. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  43. }
  44. public function testHandleWhenAListenerReturnsAResponse()
  45. {
  46. $dispatcher = new EventDispatcher();
  47. $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
  48. $event->setResponse(new Response('hello'));
  49. });
  50. $kernel = new HttpKernel($dispatcher, $this->getResolver());
  51. $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
  52. }
  53. /**
  54. * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  55. */
  56. public function testHandleWhenNoControllerIsFound()
  57. {
  58. $dispatcher = new EventDispatcher();
  59. $kernel = new HttpKernel($dispatcher, $this->getResolver(false));
  60. $kernel->handle(new Request());
  61. }
  62. /**
  63. * @expectedException LogicException
  64. */
  65. public function testHandleWhenTheControllerIsNotACallable()
  66. {
  67. $dispatcher = new EventDispatcher();
  68. $kernel = new HttpKernel($dispatcher, $this->getResolver('foobar'));
  69. $kernel->handle(new Request());
  70. }
  71. public function testHandleWhenTheControllerIsAClosure()
  72. {
  73. $response = new Response('foo');
  74. $dispatcher = new EventDispatcher();
  75. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () use ($response) { return $response; }));
  76. $this->assertSame($response, $kernel->handle(new Request()));
  77. }
  78. public function testHandleWhenTheControllerIsAnObjectWithInvoke()
  79. {
  80. $dispatcher = new EventDispatcher();
  81. $kernel = new HttpKernel($dispatcher, $this->getResolver(new Controller()));
  82. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  83. }
  84. public function testHandleWhenTheControllerIsAFunction()
  85. {
  86. $dispatcher = new EventDispatcher();
  87. $kernel = new HttpKernel($dispatcher, $this->getResolver('Symfony\Tests\Component\HttpKernel\controller_func'));
  88. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  89. }
  90. public function testHandleWhenTheControllerIsAnArray()
  91. {
  92. $dispatcher = new EventDispatcher();
  93. $kernel = new HttpKernel($dispatcher, $this->getResolver(array(new Controller(), 'controller')));
  94. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  95. }
  96. public function testHandleWhenTheControllerIsAStaticArray()
  97. {
  98. $dispatcher = new EventDispatcher();
  99. $kernel = new HttpKernel($dispatcher, $this->getResolver(array('Symfony\Tests\Component\HttpKernel\Controller', 'staticcontroller')));
  100. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  101. }
  102. /**
  103. * @expectedException LogicException
  104. */
  105. public function testHandleWhenTheControllerDoesNotReturnAResponse()
  106. {
  107. $dispatcher = new EventDispatcher();
  108. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  109. $kernel->handle(new Request());
  110. }
  111. public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
  112. {
  113. $dispatcher = new EventDispatcher();
  114. $dispatcher->addListener(KernelEvents::VIEW, function ($event) {
  115. $event->setResponse(new Response($event->getControllerResult()));
  116. });
  117. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  118. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  119. }
  120. public function testHandleWithAResponseListener()
  121. {
  122. $dispatcher = new EventDispatcher();
  123. $dispatcher->addListener(KernelEvents::RESPONSE, function ($event) {
  124. $event->setResponse(new Response('foo'));
  125. });
  126. $kernel = new HttpKernel($dispatcher, $this->getResolver());
  127. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  128. }
  129. protected function getResolver($controller = null)
  130. {
  131. if (null === $controller) {
  132. $controller = function() { return new Response('Hello'); };
  133. }
  134. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  135. $resolver->expects($this->any())
  136. ->method('getController')
  137. ->will($this->returnValue($controller));
  138. $resolver->expects($this->any())
  139. ->method('getArguments')
  140. ->will($this->returnValue(array()));
  141. return $resolver;
  142. }
  143. protected function assertResponseEquals(Response $expected, Response $actual)
  144. {
  145. $expected->setDate($actual->getDate());
  146. $this->assertEquals($expected, $actual);
  147. }
  148. }
  149. class Controller
  150. {
  151. public function __invoke()
  152. {
  153. return new Response('foo');
  154. }
  155. public function controller()
  156. {
  157. return new Response('foo');
  158. }
  159. public static function staticController()
  160. {
  161. return new Response('foo');
  162. }
  163. }
  164. function controller_func()
  165. {
  166. return new Response('foo');
  167. }