HttpKernelTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Events;
  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(Events::onCoreException, 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(Events::onCoreRequest, 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 testHandleWhenNoControllerIsNotACallable()
  68. {
  69. $dispatcher = new EventDispatcher();
  70. $kernel = new HttpKernel($dispatcher, $this->getResolver('foobar'));
  71. $kernel->handle(new Request());
  72. }
  73. /**
  74. * @expectedException LogicException
  75. */
  76. public function testHandleWhenControllerDoesNotReturnAResponse()
  77. {
  78. $dispatcher = new EventDispatcher();
  79. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  80. $kernel->handle(new Request());
  81. }
  82. public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegistered()
  83. {
  84. $dispatcher = new EventDispatcher();
  85. $dispatcher->addListener(Events::onCoreView, function ($event)
  86. {
  87. $event->setResponse(new Response($event->getControllerResult()));
  88. });
  89. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  90. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  91. }
  92. public function testHandleWithAResponseListener()
  93. {
  94. $dispatcher = new EventDispatcher();
  95. $dispatcher->addListener(Events::filterCoreResponse, function ($event)
  96. {
  97. $event->setResponse(new Response('foo'));
  98. });
  99. $kernel = new HttpKernel($dispatcher, $this->getResolver());
  100. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  101. }
  102. protected function getResolver($controller = null)
  103. {
  104. if (null === $controller) {
  105. $controller = function() { return new Response('Hello'); };
  106. }
  107. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  108. $resolver->expects($this->any())
  109. ->method('getController')
  110. ->will($this->returnValue($controller));
  111. $resolver->expects($this->any())
  112. ->method('getArguments')
  113. ->will($this->returnValue(array()));
  114. return $resolver;
  115. }
  116. }