HttpKernelTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\EventDispatcher\EventDispatcher;
  16. class HttpKernelTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @expectedException RuntimeException
  20. */
  21. public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue()
  22. {
  23. $kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
  24. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
  25. }
  26. /**
  27. * @expectedException RuntimeException
  28. */
  29. public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalseAndNoListenerIsRegistered()
  30. {
  31. $kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
  32. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
  33. }
  34. public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse()
  35. {
  36. $dispatcher = new EventDispatcher();
  37. $dispatcher->connect('core.exception', function ($event)
  38. {
  39. $event->setProcessed();
  40. return new Response($event->get('exception')->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->connect('core.request', function ($event)
  49. {
  50. $event->setProcessed();
  51. return new Response('hello');
  52. });
  53. $kernel = new HttpKernel($dispatcher, $this->getResolver());
  54. $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
  55. }
  56. /**
  57. * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  58. */
  59. public function testHandleWhenNoControllerIsFound()
  60. {
  61. $dispatcher = new EventDispatcher();
  62. $kernel = new HttpKernel($dispatcher, $this->getResolver(false));
  63. $kernel->handle(new Request());
  64. }
  65. /**
  66. * @expectedException LogicException
  67. */
  68. public function testHandleWhenNoControllerIsNotACallable()
  69. {
  70. $dispatcher = new EventDispatcher();
  71. $kernel = new HttpKernel($dispatcher, $this->getResolver('foobar'));
  72. $kernel->handle(new Request());
  73. }
  74. /**
  75. * @expectedException RuntimeException
  76. */
  77. public function testHandleWhenControllerDoesNotReturnAResponse()
  78. {
  79. $dispatcher = new EventDispatcher();
  80. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  81. $kernel->handle(new Request());
  82. }
  83. public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegistered()
  84. {
  85. $dispatcher = new EventDispatcher();
  86. $dispatcher->connect('core.view', function ($event, $retval)
  87. {
  88. return new Response($retval);
  89. });
  90. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  91. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  92. }
  93. /**
  94. * @expectedException RuntimeException
  95. */
  96. public function testHandleWhenAViewDoesNotReturnAResponse()
  97. {
  98. $dispatcher = new EventDispatcher();
  99. $dispatcher->connect('core.view', function ($event, $retval)
  100. {
  101. return $retval;
  102. });
  103. $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
  104. $kernel->handle(new Request());
  105. }
  106. /**
  107. * @expectedException RuntimeException
  108. */
  109. public function testHandleWhenAResponseListenerDoesNotReturnAResponse()
  110. {
  111. $dispatcher = new EventDispatcher();
  112. $dispatcher->connect('core.response', function ($event, $response)
  113. {
  114. return 'foo';
  115. });
  116. $kernel = new HttpKernel($dispatcher, $this->getResolver());
  117. $kernel->handle(new Request());
  118. }
  119. public function testHandleWithAResponseListener()
  120. {
  121. $dispatcher = new EventDispatcher();
  122. $dispatcher->connect('core.response', function ($event, $response)
  123. {
  124. return 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. }