暫無描述

SsiFragmentRendererTest.php 3.5KB

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