HIncludeFragmentRendererTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\HIncludeFragmentRenderer;
  14. use Symfony\Component\HttpKernel\UriSigner;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class HIncludeFragmentRendererTest extends TestCase
  17. {
  18. /**
  19. * @expectedException \LogicException
  20. */
  21. public function testRenderExceptionWhenControllerAndNoSigner()
  22. {
  23. $strategy = new HIncludeFragmentRenderer();
  24. $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'));
  25. }
  26. public function testRenderWithControllerAndSigner()
  27. {
  28. $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
  29. $this->assertEquals('<hx:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&amp;_hash=BP%2BOzCD5MRUI%2BHJpgPDOmoju00FnzLhP3TGcSHbbBLs%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
  30. }
  31. public function testRenderWithUri()
  32. {
  33. $strategy = new HIncludeFragmentRenderer();
  34. $this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo', Request::create('/'))->getContent());
  35. $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
  36. $this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo', Request::create('/'))->getContent());
  37. }
  38. public function testRenderWithDefault()
  39. {
  40. // only default
  41. $strategy = new HIncludeFragmentRenderer();
  42. $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
  43. // only global default
  44. $strategy = new HIncludeFragmentRenderer(null, null, 'global_default');
  45. $this->assertEquals('<hx:include src="/foo">global_default</hx:include>', $strategy->render('/foo', Request::create('/'), array())->getContent());
  46. // global default and default
  47. $strategy = new HIncludeFragmentRenderer(null, null, 'global_default');
  48. $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
  49. }
  50. public function testRenderWithAttributesOptions()
  51. {
  52. // with id
  53. $strategy = new HIncludeFragmentRenderer();
  54. $this->assertEquals('<hx:include src="/foo" id="bar">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default', 'id' => 'bar'))->getContent());
  55. // with attributes
  56. $strategy = new HIncludeFragmentRenderer();
  57. $this->assertEquals('<hx:include src="/foo" p1="v1" p2="v2">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default', 'attributes' => array('p1' => 'v1', 'p2' => 'v2')))->getContent());
  58. // with id & attributes
  59. $strategy = new HIncludeFragmentRenderer();
  60. $this->assertEquals('<hx:include src="/foo" p1="v1" p2="v2" id="bar">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default', 'id' => 'bar', 'attributes' => array('p1' => 'v1', 'p2' => 'v2')))->getContent());
  61. }
  62. public function testRenderWithDefaultText()
  63. {
  64. $engine = $this->getMockBuilder('Symfony\\Component\\Templating\\EngineInterface')->getMock();
  65. $engine->expects($this->once())
  66. ->method('exists')
  67. ->with('default')
  68. ->will($this->throwException(new \InvalidArgumentException()));
  69. // only default
  70. $strategy = new HIncludeFragmentRenderer($engine);
  71. $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
  72. }
  73. }