PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/eryx/php-framework-benchmark
PHP | 220 lines | 172 code | 32 blank | 16 comment | 1 complexity | 4dd017d433ae18bc7cfeb4bb1f4418cb MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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. 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. if (version_compare(phpversion(), '5.3.3', '<')) {
  146. $this->markTestSkipped('Test fails with PHP 5.3.2 due to https://bugs.php.net/bug.php?id=50563');
  147. }
  148. $request = new Request();
  149. $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
  150. $container
  151. ->expects($this->at(0))
  152. ->method('getParameter')
  153. ->with($this->equalTo('kernel.debug'))
  154. ->will($this->returnValue(false))
  155. ;
  156. $container
  157. ->expects($this->at(1))
  158. ->method('has')
  159. ->with($this->equalTo('esi'))
  160. ->will($this->returnValue(false))
  161. ;
  162. $container
  163. ->expects($this->at(2))
  164. ->method('get')
  165. ->with($this->equalTo('request'))
  166. ->will($this->returnValue($request))
  167. ;
  168. $dispatcher = new EventDispatcher();
  169. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  170. $resolver->expects($this->once())
  171. ->method('getController')
  172. ->will($this->returnValue(function () {
  173. ob_start();
  174. echo 'bar';
  175. throw new \RuntimeException();
  176. }));
  177. $resolver->expects($this->once())
  178. ->method('getArguments')
  179. ->will($this->returnValue(array()));
  180. $kernel = new HttpKernel($dispatcher, $container, $resolver);
  181. // simulate a main request with output buffering
  182. ob_start();
  183. echo 'Foo';
  184. // simulate a sub-request with output buffering and an exception
  185. $kernel->render('/');
  186. $this->assertEquals('Foo', ob_get_clean());
  187. }
  188. }