PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Bundle/FrameworkBundle/Tests/HttpKernelTest.php

http://github.com/symfony/symfony
PHP | 222 lines | 174 code | 32 blank | 16 comment | 1 complexity | 0a68c324332272bb531549446489bf3b MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests;
  11. use Symfony\Component\HttpKernel\HttpKernelInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Bundle\FrameworkBundle\HttpKernel;
  15. use Symfony\Component\EventDispatcher\EventDispatcher;
  16. class HttpKernelTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @dataProvider getProviderTypes
  20. */
  21. public function testHandle($type)
  22. {
  23. $request = new Request();
  24. $expected = new Response();
  25. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  26. $container
  27. ->expects($this->once())
  28. ->method('enterScope')
  29. ->with($this->equalTo('request'))
  30. ;
  31. $container
  32. ->expects($this->once())
  33. ->method('leaveScope')
  34. ->with($this->equalTo('request'))
  35. ;
  36. $container
  37. ->expects($this->once())
  38. ->method('set')
  39. ->with($this->equalTo('request'), $this->equalTo($request), $this->equalTo('request'))
  40. ;
  41. $dispatcher = new EventDispatcher();
  42. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  43. $kernel = new HttpKernel($dispatcher, $container, $resolver);
  44. $controller = function() use($expected)
  45. {
  46. return $expected;
  47. };
  48. $resolver->expects($this->once())
  49. ->method('getController')
  50. ->with($request)
  51. ->will($this->returnValue($controller));
  52. $resolver->expects($this->once())
  53. ->method('getArguments')
  54. ->with($request, $controller)
  55. ->will($this->returnValue(array()));
  56. $actual = $kernel->handle($request, $type);
  57. $this->assertSame($expected, $actual, '->handle() returns the response');
  58. }
  59. /**
  60. * @dataProvider getProviderTypes
  61. */
  62. public function testHandleRestoresThePreviousRequestOnException($type)
  63. {
  64. $request = new Request();
  65. $expected = new \Exception();
  66. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  67. $container
  68. ->expects($this->once())
  69. ->method('enterScope')
  70. ->with($this->equalTo('request'))
  71. ;
  72. $container
  73. ->expects($this->once())
  74. ->method('leaveScope')
  75. ->with($this->equalTo('request'))
  76. ;
  77. $container
  78. ->expects($this->once())
  79. ->method('set')
  80. ->with($this->equalTo('request'), $this->equalTo($request), $this->equalTo('request'))
  81. ;
  82. $dispatcher = new EventDispatcher();
  83. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  84. $kernel = new HttpKernel($dispatcher, $container, $resolver);
  85. $controller = function() use ($expected)
  86. {
  87. throw $expected;
  88. };
  89. $resolver->expects($this->once())
  90. ->method('getController')
  91. ->with($request)
  92. ->will($this->returnValue($controller));
  93. $resolver->expects($this->once())
  94. ->method('getArguments')
  95. ->with($request, $controller)
  96. ->will($this->returnValue(array()));
  97. try {
  98. $kernel->handle($request, $type);
  99. $this->fail('->handle() suppresses the controller exception');
  100. } catch (\Exception $actual) {
  101. $this->assertSame($expected, $actual, '->handle() throws the controller exception');
  102. }
  103. }
  104. public function testGenerateInternalUriHandlesNullValues()
  105. {
  106. $request = new Request();
  107. $router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
  108. $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
  109. $container
  110. ->expects($this->at(0))
  111. ->method('get')
  112. ->with($this->equalTo('router'))
  113. ->will($this->returnValue($router))
  114. ;
  115. $container
  116. ->expects($this->at('1'))
  117. ->method('get')
  118. ->with($this->equalTo('request'))
  119. ->will($this->returnValue($request))
  120. ;
  121. $controller = 'AController';
  122. $attributes = array('anAttribute' => null);
  123. $query = array('aQueryParam' => null);
  124. $expectedPath = 'none';
  125. $routeParameters = array('controller' => $controller, 'path' => $expectedPath, '_format' => 'html');
  126. $router
  127. ->expects($this->once())
  128. ->method('generate')
  129. ->with($this->equalTo('_internal'), $this->equalTo($routeParameters))
  130. ->will($this->returnValue('GENERATED_URI'))
  131. ;
  132. $dispatcher = new EventDispatcher();
  133. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  134. $kernel = new HttpKernel($dispatcher, $container, $resolver);
  135. $uri = $kernel->generateInternalUri($controller, $attributes, $query);
  136. $this->assertEquals('GENERATED_URI', $uri);
  137. }
  138. public function getProviderTypes()
  139. {
  140. return array(
  141. array(HttpKernelInterface::MASTER_REQUEST),
  142. array(HttpKernelInterface::SUB_REQUEST),
  143. );
  144. }
  145. public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
  146. {
  147. if (version_compare(phpversion(), "5.3.2", "<=")) {
  148. $this->markTestSkipped('Test fails with PHP5.3.2 due to https://bugs.php.net/bug.php?id=50563');
  149. }
  150. $request = new Request();
  151. $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
  152. $container
  153. ->expects($this->at(0))
  154. ->method('getParameter')
  155. ->with($this->equalTo('kernel.debug'))
  156. ->will($this->returnValue(false))
  157. ;
  158. $container
  159. ->expects($this->at(1))
  160. ->method('has')
  161. ->with($this->equalTo('esi'))
  162. ->will($this->returnValue(false))
  163. ;
  164. $container
  165. ->expects($this->at(2))
  166. ->method('get')
  167. ->with($this->equalTo('request'))
  168. ->will($this->returnValue($request))
  169. ;
  170. $dispatcher = new EventDispatcher();
  171. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  172. $resolver->expects($this->once())
  173. ->method('getController')
  174. ->will($this->returnValue(function () {
  175. ob_start();
  176. echo 'bar';
  177. throw new \RuntimeException();
  178. }));
  179. $resolver->expects($this->once())
  180. ->method('getArguments')
  181. ->will($this->returnValue(array()));
  182. $kernel = new HttpKernel($dispatcher, $container, $resolver);
  183. // simulate a main request with output buffering
  184. ob_start();
  185. echo 'Foo';
  186. // simulate a sub-request with output buffering and an exception
  187. $kernel->render('/');
  188. $this->assertEquals('Foo', ob_get_clean());
  189. }
  190. }