SsiFragmentRendererTest.php 3.5 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\HttpKernel\Controller\ControllerReference;
  13. use Symfony\Component\HttpKernel\Fragment\SsiFragmentRenderer;
  14. use Symfony\Component\HttpKernel\HttpCache\Ssi;
  15. use Symfony\Component\HttpFoundation\Request;
  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, array('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', array(), array());
  41. $altReference = new ControllerReference('alt_controller', array(), array());
  42. $this->assertEquals(
  43. '<!--#include virtual="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller&_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%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 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, 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. }