SsiFragmentRendererTest.php 3.5 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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  14. use Symfony\Component\HttpKernel\Fragment\SsiFragmentRenderer;
  15. use Symfony\Component\HttpKernel\HttpCache\Ssi;
  16. use Symfony\Component\HttpKernel\UriSigner;
  17. class SsiFragmentRendererTest extends TestCase
  18. {
  19. public function testRenderFallbackToInlineStrategyIfSsiNotSupported()
  20. {
  21. $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy(true));
  22. $strategy->render('/', Request::create('/'));
  23. }
  24. public function testRender()
  25. {
  26. $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy());
  27. $request = Request::create('/');
  28. $request->setLocale('fr');
  29. $request->headers->set('Surrogate-Capability', 'SSI/1.0');
  30. $this->assertEquals('<!--#include virtual="/" -->', $strategy->render('/', $request)->getContent());
  31. $this->assertEquals('<!--#include virtual="/" -->', $strategy->render('/', $request, ['comment' => 'This is a comment'])->getContent(), 'Strategy options should not impact the ssi include tag');
  32. }
  33. public function testRenderControllerReference()
  34. {
  35. $signer = new UriSigner('foo');
  36. $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy(), $signer);
  37. $request = Request::create('/');
  38. $request->setLocale('fr');
  39. $request->headers->set('Surrogate-Capability', 'SSI/1.0');
  40. $reference = new ControllerReference('main_controller', [], []);
  41. $altReference = new ControllerReference('alt_controller', [], []);
  42. $this->assertEquals(
  43. '<!--#include virtual="/_fragment?_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" -->',
  44. $strategy->render($reference, $request, ['alt' => $altReference])->getContent()
  45. );
  46. }
  47. /**
  48. * @expectedException \LogicException
  49. */
  50. public function testRenderControllerReferenceWithoutSignerThrowsException()
  51. {
  52. $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy());
  53. $request = Request::create('/');
  54. $request->setLocale('fr');
  55. $request->headers->set('Surrogate-Capability', 'SSI/1.0');
  56. $strategy->render(new ControllerReference('main_controller'), $request);
  57. }
  58. /**
  59. * @expectedException \LogicException
  60. */
  61. public function testRenderAltControllerReferenceWithoutSignerThrowsException()
  62. {
  63. $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy());
  64. $request = Request::create('/');
  65. $request->setLocale('fr');
  66. $request->headers->set('Surrogate-Capability', 'SSI/1.0');
  67. $strategy->render('/', $request, ['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. }