Açıklama Yok

InlineFragmentRendererTest.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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\Component\HttpKernel\Tests\Fragment;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
  13. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  14. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  15. use Symfony\Component\HttpKernel\HttpKernel;
  16. use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\EventDispatcher\EventDispatcher;
  21. class InlineFragmentRendererTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function testRender()
  24. {
  25. $strategy = new InlineFragmentRenderer($this->getKernel($this->returnValue(new Response('foo'))));
  26. $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent());
  27. }
  28. public function testRenderWithControllerReference()
  29. {
  30. $strategy = new InlineFragmentRenderer($this->getKernel($this->returnValue(new Response('foo'))));
  31. $this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
  32. }
  33. public function testRenderWithObjectsAsAttributes()
  34. {
  35. $object = new \stdClass();
  36. $subRequest = Request::create('/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller');
  37. $subRequest->attributes->replace(array('object' => $object, '_format' => 'html', '_controller' => 'main_controller', '_locale' => 'en'));
  38. $subRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
  39. $subRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
  40. $strategy = new InlineFragmentRenderer($this->getKernelExpectingRequest($subRequest));
  41. $strategy->render(new ControllerReference('main_controller', array('object' => $object), array()), Request::create('/'));
  42. }
  43. /**
  44. * @group legacy
  45. */
  46. public function testRenderWithObjectsAsAttributesPassedAsObjectsInTheControllerLegacy()
  47. {
  48. $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver')->setMethods(array('getController'))->getMock();
  49. $resolver
  50. ->expects($this->once())
  51. ->method('getController')
  52. ->will($this->returnValue(function (\stdClass $object, Bar $object1) {
  53. return new Response($object1->getBar());
  54. }))
  55. ;
  56. $kernel = new HttpKernel(new EventDispatcher(), $resolver, new RequestStack());
  57. $renderer = new InlineFragmentRenderer($kernel);
  58. $response = $renderer->render(new ControllerReference('main_controller', array('object' => new \stdClass(), 'object1' => new Bar()), array()), Request::create('/'));
  59. $this->assertEquals('bar', $response->getContent());
  60. }
  61. /**
  62. * @group legacy
  63. */
  64. public function testRenderWithObjectsAsAttributesPassedAsObjectsInTheController()
  65. {
  66. $resolver = $this->getMockBuilder(ControllerResolverInterface::class)->getMock();
  67. $resolver
  68. ->expects($this->once())
  69. ->method('getController')
  70. ->will($this->returnValue(function (\stdClass $object, Bar $object1) {
  71. return new Response($object1->getBar());
  72. }))
  73. ;
  74. $kernel = new HttpKernel(new EventDispatcher(), $resolver, new RequestStack(), new ArgumentResolver());
  75. $renderer = new InlineFragmentRenderer($kernel);
  76. $response = $renderer->render(new ControllerReference('main_controller', array('object' => new \stdClass(), 'object1' => new Bar()), array()), Request::create('/'));
  77. $this->assertEquals('bar', $response->getContent());
  78. }
  79. public function testRenderWithTrustedHeaderDisabled()
  80. {
  81. $trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP);
  82. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, '');
  83. $strategy = new InlineFragmentRenderer($this->getKernelExpectingRequest(Request::create('/')));
  84. $strategy->render('/', Request::create('/'));
  85. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, $trustedHeaderName);
  86. }
  87. /**
  88. * @expectedException \RuntimeException
  89. */
  90. public function testRenderExceptionNoIgnoreErrors()
  91. {
  92. $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  93. $dispatcher->expects($this->never())->method('dispatch');
  94. $strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher);
  95. $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent());
  96. }
  97. public function testRenderExceptionIgnoreErrors()
  98. {
  99. $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  100. $dispatcher->expects($this->once())->method('dispatch')->with(KernelEvents::EXCEPTION);
  101. $strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher);
  102. $this->assertEmpty($strategy->render('/', Request::create('/'), array('ignore_errors' => true))->getContent());
  103. }
  104. public function testRenderExceptionIgnoreErrorsWithAlt()
  105. {
  106. $strategy = new InlineFragmentRenderer($this->getKernel($this->onConsecutiveCalls(
  107. $this->throwException(new \RuntimeException('foo')),
  108. $this->returnValue(new Response('bar'))
  109. )));
  110. $this->assertEquals('bar', $strategy->render('/', Request::create('/'), array('ignore_errors' => true, 'alt' => '/foo'))->getContent());
  111. }
  112. private function getKernel($returnValue)
  113. {
  114. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
  115. $kernel
  116. ->expects($this->any())
  117. ->method('handle')
  118. ->will($returnValue)
  119. ;
  120. return $kernel;
  121. }
  122. /**
  123. * Creates a Kernel expecting a request equals to $request
  124. * Allows delta in comparison in case REQUEST_TIME changed by 1 second.
  125. */
  126. private function getKernelExpectingRequest(Request $request)
  127. {
  128. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
  129. $kernel
  130. ->expects($this->any())
  131. ->method('handle')
  132. ->with($this->equalTo($request, 1))
  133. ;
  134. return $kernel;
  135. }
  136. public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
  137. {
  138. $controllerResolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
  139. $controllerResolver
  140. ->expects($this->once())
  141. ->method('getController')
  142. ->will($this->returnValue(function () {
  143. ob_start();
  144. echo 'bar';
  145. throw new \RuntimeException();
  146. }))
  147. ;
  148. $argumentResolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolverInterface')->getMock();
  149. $argumentResolver
  150. ->expects($this->once())
  151. ->method('getArguments')
  152. ->will($this->returnValue(array()))
  153. ;
  154. $kernel = new HttpKernel(new EventDispatcher(), $controllerResolver, new RequestStack(), $argumentResolver);
  155. $renderer = new InlineFragmentRenderer($kernel);
  156. // simulate a main request with output buffering
  157. ob_start();
  158. echo 'Foo';
  159. // simulate a sub-request with output buffering and an exception
  160. $renderer->render('/', Request::create('/'), array('ignore_errors' => true));
  161. $this->assertEquals('Foo', ob_get_clean());
  162. }
  163. public function testESIHeaderIsKeptInSubrequest()
  164. {
  165. $expectedSubRequest = Request::create('/');
  166. $expectedSubRequest->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
  167. if (Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
  168. $expectedSubRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
  169. $expectedSubRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
  170. }
  171. $strategy = new InlineFragmentRenderer($this->getKernelExpectingRequest($expectedSubRequest));
  172. $request = Request::create('/');
  173. $request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
  174. $strategy->render('/', $request);
  175. }
  176. public function testESIHeaderIsKeptInSubrequestWithTrustedHeaderDisabled()
  177. {
  178. $trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP);
  179. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, '');
  180. $this->testESIHeaderIsKeptInSubrequest();
  181. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, $trustedHeaderName);
  182. }
  183. public function testHeadersPossiblyResultingIn304AreNotAssignedToSubrequest()
  184. {
  185. $expectedSubRequest = Request::create('/');
  186. if (Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
  187. $expectedSubRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
  188. $expectedSubRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
  189. }
  190. $strategy = new InlineFragmentRenderer($this->getKernelExpectingRequest($expectedSubRequest));
  191. $request = Request::create('/', 'GET', array(), array(), array(), array('HTTP_IF_MODIFIED_SINCE' => 'Fri, 01 Jan 2016 00:00:00 GMT', 'HTTP_IF_NONE_MATCH' => '*'));
  192. $strategy->render('/', $request);
  193. }
  194. }
  195. class Bar
  196. {
  197. public $bar = 'bar';
  198. public function getBar()
  199. {
  200. return $this->bar;
  201. }
  202. }