EsiFragmentRendererTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  14. use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
  15. use Symfony\Component\HttpKernel\HttpCache\Esi;
  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. /**
  25. * @group legacy
  26. * @expectedDeprecation Passing non-scalar values as part of URI attributes to the ESI and SSI rendering strategies is deprecated %s.
  27. */
  28. public function testRenderFallbackWithObjectAttributesIsDeprecated()
  29. {
  30. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true), new UriSigner('foo'));
  31. $request = Request::create('/');
  32. $reference = new ControllerReference('main_controller', ['foo' => new \stdClass()], []);
  33. $strategy->render($reference, $request);
  34. }
  35. public function testRenderFallbackWithScalarIsNotDeprecated()
  36. {
  37. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true), new UriSigner('foo'));
  38. $request = Request::create('/');
  39. $reference = new ControllerReference('main_controller', ['foo' => [true]], []);
  40. $strategy->render($reference, $request);
  41. }
  42. public function testRender()
  43. {
  44. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  45. $request = Request::create('/');
  46. $request->setLocale('fr');
  47. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  48. $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
  49. $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, ['comment' => 'This is a comment'])->getContent());
  50. $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, ['alt' => 'foo'])->getContent());
  51. }
  52. public function testRenderControllerReference()
  53. {
  54. $signer = new UriSigner('foo');
  55. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(), $signer);
  56. $request = Request::create('/');
  57. $request->setLocale('fr');
  58. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  59. $reference = new ControllerReference('main_controller', [], []);
  60. $altReference = new ControllerReference('alt_controller', [], []);
  61. $this->assertEquals(
  62. '<esi:include src="/_fragment?_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" alt="/_fragment?_hash=iPJEdRoUpGrM1ztqByiorpfMPtiW%2FOWwdH1DBUXHhEc%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller" />',
  63. $strategy->render($reference, $request, ['alt' => $altReference])->getContent()
  64. );
  65. }
  66. /**
  67. * @expectedException \LogicException
  68. */
  69. public function testRenderControllerReferenceWithoutSignerThrowsException()
  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(new ControllerReference('main_controller'), $request);
  76. }
  77. /**
  78. * @expectedException \LogicException
  79. */
  80. public function testRenderAltControllerReferenceWithoutSignerThrowsException()
  81. {
  82. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  83. $request = Request::create('/');
  84. $request->setLocale('fr');
  85. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  86. $strategy->render('/', $request, ['alt' => new ControllerReference('alt_controller')]);
  87. }
  88. private function getInlineStrategy($called = false)
  89. {
  90. $inline = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer')->disableOriginalConstructor()->getMock();
  91. if ($called) {
  92. $inline->expects($this->once())->method('render');
  93. }
  94. return $inline;
  95. }
  96. }