HttpKernelTest.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. {
  40. $event->setResponse(new Response($event->getException()->getMessage()));
  41. });
  42. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); }));
  43. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  44. }
  45. public function testHandleWhenAListenerReturnsAResponse()
  46. {
  47. $dispatcher = new EventDispatcher();
  48. $dispatcher->addListener(KernelEvents::REQUEST, function ($event)
  49. {
  50. $event->setResponse(new Response('hello'));
  51. });
  52. $kernel = new HttpKernel($dispatcher, $this->getResolver());
  53. $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
  54. }
  55. /**
  56. * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  57. */
  58. public function testHandleWhenNoControllerIsFound()
  59. {
  60. $dispatcher = new EventDispatcher();
  61. $kernel = new HttpKernel($dispatcher, $this->getResolver(false));
  62. $kernel->handle(new Request());
  63. }
  64. /**
  65. * @expectedException LogicException
  66. */
  67. public function testHandleWhenTheControllerIsNotACallable()
  68. {
  69. $dispatcher = new EventDispatcher();
  70. $kernel = new HttpKernel($dispatcher, $this->getResolver('foobar'));
  71. $kernel->handle(new Request());
  72. }
  73. public function testHandleWhenTheControllerIsAClosure()
  74. {
  75. $response = new Response('foo');
  76. $dispatcher = new EventDispatcher();
  77. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () use ($response) { return $response; }));
  78. $this->assertSame($response, $kernel->handle(new Request()));
  79. }
  80. public function testHandleWhenTheControllerIsAnObjectWithInvoke()
  81. {
  82. $dispatcher = new EventDispatcher();
  83. $kernel = new HttpKernel($dispatcher, $this->getResolver(new Controller()));
  84. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  85. }
  86. public function testHandleWhenTheControllerIsAFunction()
  87. {
  88. $dispatcher = new EventDispatcher();
  89. $kernel = new HttpKernel($dispatcher, $this->getResolver('Symfony\Tests\Component\HttpKernel\controller_func'));
  90. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  91. }
  92. public function testHandleWhenTheControllerIsAnArray()
  93. {
  94. $dispatcher = new EventDispatcher();
  95. $kernel = new HttpKernel($dispatcher, $this->getResolver(array(new Controller(), 'controller')));
  96. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  97. }
  98. public function testHandleWhenTheControllerIsAStaticArray()
  99. {
  100. $dispatcher = new EventDispatcher();
  101. $kernel = new HttpKernel($dispatcher, $this->getResolver(array('Symfony\Tests\Component\HttpKernel\Controller', 'staticcontroller')));
  102. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  103. }
  104. /**
  105. * @expectedException LogicException
  106. */
  107. public function testHandleWhenTheControllerDoesNotReturnAResponse()
  108. {
  109. $dispatcher = new EventDispatcher();
  110. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  111. $kernel->handle(new Request());
  112. }
  113. public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
  114. {
  115. $dispatcher = new EventDispatcher();
  116. $dispatcher->addListener(KernelEvents::VIEW, function ($event)
  117. {
  118. $event->setResponse(new Response($event->getControllerResult()));
  119. });
  120. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  121. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  122. }
  123. public function testHandleWithAResponseListener()
  124. {
  125. $dispatcher = new EventDispatcher();
  126. $dispatcher->addListener(KernelEvents::RESPONSE, function ($event)
  127. {
  128. $event->setResponse(new Response('foo'));
  129. });
  130. $kernel = new HttpKernel($dispatcher, $this->getResolver());
  131. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  132. }
  133. protected function getResolver($controller = null)
  134. {
  135. if (null === $controller) {
  136. $controller = function() { return new Response('Hello'); };
  137. }
  138. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  139. $resolver->expects($this->any())
  140. ->method('getController')
  141. ->will($this->returnValue($controller));
  142. $resolver->expects($this->any())
  143. ->method('getArguments')
  144. ->will($this->returnValue(array()));
  145. return $resolver;
  146. }
  147. protected function assertResponseEquals(Response $expected, Response $actual)
  148. {
  149. $expected->setDate($actual->getDate());
  150. $this->assertEquals($expected, $actual);
  151. }
  152. }
  153. class Controller
  154. {
  155. public function __invoke()
  156. {
  157. return new Response('foo');
  158. }
  159. public function controller()
  160. {
  161. return new Response('foo');
  162. }
  163. static public function staticController()
  164. {
  165. return new Response('foo');
  166. }
  167. }
  168. function controller_func()
  169. {
  170. return new Response('foo');
  171. }