EsiFragmentRendererTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\FragmentRenderer;
  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. class EsiFragmentRendererTest extends \PHPUnit_Framework_TestCase
  16. {
  17. protected function setUp()
  18. {
  19. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  20. $this->markTestSkipped('The "HttpFoundation" component is not available');
  21. }
  22. }
  23. public function testRenderFallbackToInlineStrategyIfNoRequest()
  24. {
  25. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true));
  26. $strategy->render('/', Request::create('/'));
  27. }
  28. public function testRenderFallbackToInlineStrategyIfEsiNotSupported()
  29. {
  30. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true));
  31. $strategy->render('/', Request::create('/'));
  32. }
  33. public function testRender()
  34. {
  35. $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
  36. $request = Request::create('/');
  37. $request->setLocale('fr');
  38. $request->headers->set('Surrogate-Capability', 'ESI/1.0');
  39. $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
  40. $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
  41. $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
  42. $this->assertEquals('<esi:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" alt="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller" />', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent());
  43. }
  44. private function getInlineStrategy($called = false)
  45. {
  46. $inline = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer')->disableOriginalConstructor()->getMock();
  47. if ($called) {
  48. $inline->expects($this->once())->method('render');
  49. }
  50. return $inline;
  51. }
  52. }