SsiFragmentRendererTest.php 3.4 KB

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