EsiFragmentRendererTest.php 3.7 KB

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