EsiFragmentRendererTest.php 3.7 KB

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