EsiFragmentRendererTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  13. use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
  14. use Symfony\Component\HttpKernel\HttpCache\Esi;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\UriSigner;
  17. class EsiFragmentRendererTest extends TestCase
  18. {
  19. public function testRenderFallbackToInlineStrategyIfEsiNotSupported()
  20. {
  21. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true));
  22. $strategy->render('/', Request::create('/'));
  23. }
  24. public function testRenderFallbackWithScalar()
  25. {
  26. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true), new UriSigner('foo'));
  27. $request = Request::create('/');
  28. $reference = new ControllerReference('main_controller', array('foo' => array(true)), array());
  29. $strategy->render($reference, $request);
  30. }
  31. public function testRender()
  32. {
  33. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  34. $request = Request::create('/');
  35. $request->setLocale('fr');
  36. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  37. $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
  38. $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
  39. $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
  40. }
  41. public function testRenderControllerReference()
  42. {
  43. $signer = new UriSigner('foo');
  44. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(), $signer);
  45. $request = Request::create('/');
  46. $request->setLocale('fr');
  47. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  48. $reference = new ControllerReference('main_controller', array(), array());
  49. $altReference = new ControllerReference('alt_controller', array(), array());
  50. $this->assertEquals(
  51. '<esi:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller&_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D" alt="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller&_hash=iPJEdRoUpGrM1ztqByiorpfMPtiW%2FOWwdH1DBUXHhEc%3D" />',
  52. $strategy->render($reference, $request, array('alt' => $altReference))->getContent()
  53. );
  54. }
  55. /**
  56. * @expectedException \LogicException
  57. */
  58. public function testRenderControllerReferenceWithoutSignerThrowsException()
  59. {
  60. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  61. $request = Request::create('/');
  62. $request->setLocale('fr');
  63. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  64. $strategy->render(new ControllerReference('main_controller'), $request);
  65. }
  66. /**
  67. * @expectedException \LogicException
  68. */
  69. public function testRenderAltControllerReferenceWithoutSignerThrowsException()
  70. {
  71. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  72. $request = Request::create('/');
  73. $request->setLocale('fr');
  74. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  75. $strategy->render('/', $request, array('alt' => new ControllerReference('alt_controller')));
  76. }
  77. private function getInlineStrategy($called = false)
  78. {
  79. $inline = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer')->disableOriginalConstructor()->getMock();
  80. if ($called) {
  81. $inline->expects($this->once())->method('render');
  82. }
  83. return $inline;
  84. }
  85. }