EsiFragmentRendererTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Fragment\InlineFragmentRenderer;
  16. use Symfony\Component\HttpKernel\HttpCache\Esi;
  17. use Symfony\Component\HttpKernel\UriSigner;
  18. class EsiFragmentRendererTest extends TestCase
  19. {
  20. public function testRenderFallbackToInlineStrategyIfEsiNotSupported()
  21. {
  22. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true));
  23. $strategy->render('/', Request::create('/'));
  24. }
  25. public function testRenderFallbackWithScalar()
  26. {
  27. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true), new UriSigner('foo'));
  28. $request = Request::create('/');
  29. $reference = new ControllerReference('main_controller', ['foo' => [true]], []);
  30. $strategy->render($reference, $request);
  31. }
  32. public function testRender()
  33. {
  34. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  35. $request = Request::create('/');
  36. $request->setLocale('fr');
  37. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  38. $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
  39. $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, ['comment' => 'This is a comment'])->getContent());
  40. $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, ['alt' => 'foo'])->getContent());
  41. }
  42. public function testRenderControllerReference()
  43. {
  44. $signer = new UriSigner('foo');
  45. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(), $signer);
  46. $request = Request::create('/');
  47. $request->setLocale('fr');
  48. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  49. $reference = new ControllerReference('main_controller', [], []);
  50. $altReference = new ControllerReference('alt_controller', [], []);
  51. $this->assertEquals(
  52. '<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" />',
  53. $strategy->render($reference, $request, ['alt' => $altReference])->getContent()
  54. );
  55. }
  56. public function testRenderControllerReferenceWithoutSignerThrowsException()
  57. {
  58. $this->expectException(\LogicException::class);
  59. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  60. $request = Request::create('/');
  61. $request->setLocale('fr');
  62. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  63. $strategy->render(new ControllerReference('main_controller'), $request);
  64. }
  65. public function testRenderAltControllerReferenceWithoutSignerThrowsException()
  66. {
  67. $this->expectException(\LogicException::class);
  68. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  69. $request = Request::create('/');
  70. $request->setLocale('fr');
  71. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  72. $strategy->render('/', $request, ['alt' => new ControllerReference('alt_controller')]);
  73. }
  74. private function getInlineStrategy($called = false)
  75. {
  76. $inline = $this->createMock(InlineFragmentRenderer::class);
  77. if ($called) {
  78. $inline->expects($this->once())->method('render');
  79. }
  80. return $inline;
  81. }
  82. }