PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pyneff/carsharing
PHP | 216 lines | 169 code | 31 blank | 16 comment | 0 complexity | df3fd28aec0ec1bb4806a65a7cfce250 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, BSD-3-Clause, BSD-2-Clause
  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\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. return $expected;
  46. };
  47. $resolver->expects($this->once())
  48. ->method('getController')
  49. ->with($request)
  50. ->will($this->returnValue($controller));
  51. $resolver->expects($this->once())
  52. ->method('getArguments')
  53. ->with($request, $controller)
  54. ->will($this->returnValue(array()));
  55. $actual = $kernel->handle($request, $type);
  56. $this->assertSame($expected, $actual, '->handle() returns the response');
  57. }
  58. /**
  59. * @dataProvider getProviderTypes
  60. */
  61. public function testHandleRestoresThePreviousRequestOnException($type)
  62. {
  63. $request = new Request();
  64. $expected = new \Exception();
  65. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  66. $container
  67. ->expects($this->once())
  68. ->method('enterScope')
  69. ->with($this->equalTo('request'))
  70. ;
  71. $container
  72. ->expects($this->once())
  73. ->method('leaveScope')
  74. ->with($this->equalTo('request'))
  75. ;
  76. $container
  77. ->expects($this->once())
  78. ->method('set')
  79. ->with($this->equalTo('request'), $this->equalTo($request), $this->equalTo('request'))
  80. ;
  81. $dispatcher = new EventDispatcher();
  82. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  83. $kernel = new HttpKernel($dispatcher, $container, $resolver);
  84. $controller = function() use ($expected) {
  85. throw $expected;
  86. };
  87. $resolver->expects($this->once())
  88. ->method('getController')
  89. ->with($request)
  90. ->will($this->returnValue($controller));
  91. $resolver->expects($this->once())
  92. ->method('getArguments')
  93. ->with($request, $controller)
  94. ->will($this->returnValue(array()));
  95. try {
  96. $kernel->handle($request, $type);
  97. $this->fail('->handle() suppresses the controller exception');
  98. } catch (\Exception $actual) {
  99. $this->assertSame($expected, $actual, '->handle() throws the controller exception');
  100. }
  101. }
  102. public function testGenerateInternalUriHandlesNullValues()
  103. {
  104. $request = new Request();
  105. $router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
  106. $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
  107. $container
  108. ->expects($this->at(0))
  109. ->method('get')
  110. ->with($this->equalTo('router'))
  111. ->will($this->returnValue($router))
  112. ;
  113. $container
  114. ->expects($this->at('1'))
  115. ->method('get')
  116. ->with($this->equalTo('request'))
  117. ->will($this->returnValue($request))
  118. ;
  119. $controller = 'AController';
  120. $attributes = array('anAttribute' => null);
  121. $query = array('aQueryParam' => null);
  122. $expectedPath = 'none';
  123. $routeParameters = array('controller' => $controller, 'path' => $expectedPath, '_format' => 'html');
  124. $router
  125. ->expects($this->once())
  126. ->method('generate')
  127. ->with($this->equalTo('_internal'), $this->equalTo($routeParameters))
  128. ->will($this->returnValue('GENERATED_URI'))
  129. ;
  130. $dispatcher = new EventDispatcher();
  131. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  132. $kernel = new HttpKernel($dispatcher, $container, $resolver);
  133. $uri = $kernel->generateInternalUri($controller, $attributes, $query);
  134. $this->assertEquals('GENERATED_URI', $uri);
  135. }
  136. public function getProviderTypes()
  137. {
  138. return array(
  139. array(HttpKernelInterface::MASTER_REQUEST),
  140. array(HttpKernelInterface::SUB_REQUEST),
  141. );
  142. }
  143. public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
  144. {
  145. $request = new Request();
  146. $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
  147. $container
  148. ->expects($this->at(0))
  149. ->method('getParameter')
  150. ->with($this->equalTo('kernel.debug'))
  151. ->will($this->returnValue(false))
  152. ;
  153. $container
  154. ->expects($this->at(1))
  155. ->method('has')
  156. ->with($this->equalTo('esi'))
  157. ->will($this->returnValue(false))
  158. ;
  159. $container
  160. ->expects($this->at(2))
  161. ->method('get')
  162. ->with($this->equalTo('request'))
  163. ->will($this->returnValue($request))
  164. ;
  165. $dispatcher = new EventDispatcher();
  166. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  167. $resolver->expects($this->once())
  168. ->method('getController')
  169. ->will($this->returnValue(function () {
  170. ob_start();
  171. echo 'bar';
  172. throw new \RuntimeException();
  173. }));
  174. $resolver->expects($this->once())
  175. ->method('getArguments')
  176. ->will($this->returnValue(array()));
  177. $kernel = new HttpKernel($dispatcher, $container, $resolver);
  178. // simulate a main request with output buffering
  179. ob_start();
  180. echo 'Foo';
  181. // simulate a sub-request with output buffering and an exception
  182. $kernel->render('/');
  183. $this->assertEquals('Foo', ob_get_clean());
  184. }
  185. }