InlineFragmentRendererTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\Fragment\Tests\FragmentRenderer;
  11. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  12. use Symfony\Component\HttpKernel\HttpKernel;
  13. use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\EventDispatcher\EventDispatcher;
  18. class InlineFragmentRendererTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected function setUp()
  21. {
  22. if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
  23. $this->markTestSkipped('The "EventDispatcher" component is not available');
  24. }
  25. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  26. $this->markTestSkipped('The "HttpFoundation" component is not available');
  27. }
  28. }
  29. public function testRender()
  30. {
  31. $strategy = new InlineFragmentRenderer($this->getKernel($this->returnValue(new Response('foo'))));
  32. $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent());
  33. }
  34. public function testRenderWithControllerReference()
  35. {
  36. $strategy = new InlineFragmentRenderer($this->getKernel($this->returnValue(new Response('foo'))));
  37. $this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
  38. }
  39. public function testRenderWithObjectsAsAttributes()
  40. {
  41. $object = new \stdClass();
  42. $subRequest = Request::create('/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller');
  43. $subRequest->attributes->replace(array('object' => $object, '_format' => 'html', '_controller' => 'main_controller', '_locale' => 'en'));
  44. $subRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
  45. $subRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
  46. $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
  47. $kernel
  48. ->expects($this->any())
  49. ->method('handle')
  50. ->with($subRequest)
  51. ;
  52. $strategy = new InlineFragmentRenderer($kernel);
  53. $strategy->render(new ControllerReference('main_controller', array('object' => $object), array()), Request::create('/'));
  54. }
  55. public function testRenderWithTrustedHeaderDisabled()
  56. {
  57. $trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP);
  58. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, '');
  59. $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
  60. $kernel
  61. ->expects($this->any())
  62. ->method('handle')
  63. ->with(Request::create('/'))
  64. ;
  65. $strategy = new InlineFragmentRenderer($kernel);
  66. $strategy->render('/', Request::create('/'));
  67. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, $trustedHeaderName);
  68. }
  69. /**
  70. * @expectedException \RuntimeException
  71. */
  72. public function testRenderExceptionNoIgnoreErrors()
  73. {
  74. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  75. $dispatcher->expects($this->never())->method('dispatch');
  76. $strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher);
  77. $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent());
  78. }
  79. public function testRenderExceptionIgnoreErrors()
  80. {
  81. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  82. $dispatcher->expects($this->once())->method('dispatch')->with(KernelEvents::EXCEPTION);
  83. $strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher);
  84. $this->assertEmpty($strategy->render('/', Request::create('/'), array('ignore_errors' => true))->getContent());
  85. }
  86. public function testRenderExceptionIgnoreErrorsWithAlt()
  87. {
  88. $strategy = new InlineFragmentRenderer($this->getKernel($this->onConsecutiveCalls(
  89. $this->throwException(new \RuntimeException('foo')),
  90. $this->returnValue(new Response('bar'))
  91. )));
  92. $this->assertEquals('bar', $strategy->render('/', Request::create('/'), array('ignore_errors' => true, 'alt' => '/foo'))->getContent());
  93. }
  94. private function getKernel($returnValue)
  95. {
  96. $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
  97. $kernel
  98. ->expects($this->any())
  99. ->method('handle')
  100. ->will($returnValue)
  101. ;
  102. return $kernel;
  103. }
  104. public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
  105. {
  106. $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
  107. $resolver
  108. ->expects($this->once())
  109. ->method('getController')
  110. ->will($this->returnValue(function () {
  111. ob_start();
  112. echo 'bar';
  113. throw new \RuntimeException();
  114. }))
  115. ;
  116. $resolver
  117. ->expects($this->once())
  118. ->method('getArguments')
  119. ->will($this->returnValue(array()))
  120. ;
  121. $kernel = new HttpKernel(new EventDispatcher(), $resolver);
  122. $renderer = new InlineFragmentRenderer($kernel);
  123. // simulate a main request with output buffering
  124. ob_start();
  125. echo 'Foo';
  126. // simulate a sub-request with output buffering and an exception
  127. $renderer->render('/', Request::create('/'), array('ignore_errors' => true));
  128. $this->assertEquals('Foo', ob_get_clean());
  129. }
  130. public function testESIHeaderIsKeptInSubrequest()
  131. {
  132. $expectedSubRequest = Request::create('/');
  133. $expectedSubRequest->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
  134. if (Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
  135. $expectedSubRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
  136. $expectedSubRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
  137. }
  138. $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
  139. $kernel
  140. ->expects($this->any())
  141. ->method('handle')
  142. ->with($expectedSubRequest)
  143. ;
  144. $strategy = new InlineFragmentRenderer($kernel);
  145. $request = Request::create('/');
  146. $request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
  147. $strategy->render('/', $request);
  148. }
  149. public function testESIHeaderIsKeptInSubrequestWithTrustedHeaderDisabled()
  150. {
  151. $trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP);
  152. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, '');
  153. $this->testESIHeaderIsKeptInSubrequest();
  154. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, $trustedHeaderName);
  155. }
  156. }