EsiFragmentRendererTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /**
  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', array('foo' => array('a' => array(), 'b' => new \stdClass())), array());
  33. $strategy->render($reference, $request);
  34. }
  35. public function testRender()
  36. {
  37. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  38. $request = Request::create('/');
  39. $request->setLocale('fr');
  40. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  41. $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
  42. $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
  43. $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
  44. }
  45. public function testRenderControllerReference()
  46. {
  47. $signer = new UriSigner('foo');
  48. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(), $signer);
  49. $request = Request::create('/');
  50. $request->setLocale('fr');
  51. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  52. $reference = new ControllerReference('main_controller', array(), array());
  53. $altReference = new ControllerReference('alt_controller', array(), array());
  54. $this->assertEquals(
  55. '<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" />',
  56. $strategy->render($reference, $request, array('alt' => $altReference))->getContent()
  57. );
  58. }
  59. /**
  60. * @expectedException \LogicException
  61. */
  62. public function testRenderControllerReferenceWithoutSignerThrowsException()
  63. {
  64. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  65. $request = Request::create('/');
  66. $request->setLocale('fr');
  67. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  68. $strategy->render(new ControllerReference('main_controller'), $request);
  69. }
  70. /**
  71. * @expectedException \LogicException
  72. */
  73. public function testRenderAltControllerReferenceWithoutSignerThrowsException()
  74. {
  75. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  76. $request = Request::create('/');
  77. $request->setLocale('fr');
  78. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  79. $strategy->render('/', $request, array('alt' => new ControllerReference('alt_controller')));
  80. }
  81. private function getInlineStrategy($called = false)
  82. {
  83. $inline = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer')->disableOriginalConstructor()->getMock();
  84. if ($called) {
  85. $inline->expects($this->once())->method('render');
  86. }
  87. return $inline;
  88. }
  89. }