Geen omschrijving

EsiFragmentRendererTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /**
  24. * @group legacy
  25. * @expectedDeprecation Passing non-scalar values as part of URI attributes to the ESI and SSI rendering strategies is deprecated %s.
  26. */
  27. public function testRenderFallbackWithObjectAttributesIsDeprecated()
  28. {
  29. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true), new UriSigner('foo'));
  30. $request = Request::create('/');
  31. $reference = new ControllerReference('main_controller', array('foo' => array('a' => array(), 'b' => new \stdClass())), array());
  32. $strategy->render($reference, $request);
  33. }
  34. public function testRender()
  35. {
  36. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  37. $request = Request::create('/');
  38. $request->setLocale('fr');
  39. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  40. $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
  41. $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
  42. $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
  43. }
  44. public function testRenderControllerReference()
  45. {
  46. $signer = new UriSigner('foo');
  47. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(), $signer);
  48. $request = Request::create('/');
  49. $request->setLocale('fr');
  50. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  51. $reference = new ControllerReference('main_controller', array(), array());
  52. $altReference = new ControllerReference('alt_controller', array(), array());
  53. $this->assertEquals(
  54. '<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" />',
  55. $strategy->render($reference, $request, array('alt' => $altReference))->getContent()
  56. );
  57. }
  58. /**
  59. * @expectedException \LogicException
  60. */
  61. public function testRenderControllerReferenceWithoutSignerThrowsException()
  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(new ControllerReference('main_controller'), $request);
  68. }
  69. /**
  70. * @expectedException \LogicException
  71. */
  72. public function testRenderAltControllerReferenceWithoutSignerThrowsException()
  73. {
  74. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  75. $request = Request::create('/');
  76. $request->setLocale('fr');
  77. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  78. $strategy->render('/', $request, array('alt' => new ControllerReference('alt_controller')));
  79. }
  80. private function getInlineStrategy($called = false)
  81. {
  82. $inline = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer')->disableOriginalConstructor()->getMock();
  83. if ($called) {
  84. $inline->expects($this->once())->method('render');
  85. }
  86. return $inline;
  87. }
  88. }