HIncludeFragmentRendererTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\HIncludeFragmentRenderer;
  15. use Symfony\Component\HttpKernel\UriSigner;
  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', [], []), Request::create('/'));
  25. }
  26. public function testRenderWithControllerAndSigner()
  27. {
  28. $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
  29. $this->assertEquals('<hx:include src="/_fragment?_hash=BP%2BOzCD5MRUI%2BHJpgPDOmoju00FnzLhP3TGcSHbbBLs%3D&amp;_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller"></hx:include>', $strategy->render(new ControllerReference('main_controller', [], []), 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('/'), ['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('/'), [])->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('/'), ['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('/'), ['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('/'), ['default' => 'default', 'attributes' => ['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('/'), ['default' => 'default', 'id' => 'bar', 'attributes' => ['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. ->willThrowException(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('/'), ['default' => 'default'])->getContent());
  72. }
  73. public function testRenderWithEngineAndDefaultText()
  74. {
  75. $engine = $this->getMockBuilder('Symfony\\Component\\Templating\\EngineInterface')->getMock();
  76. $engine->expects($this->once())
  77. ->method('exists')
  78. ->with('loading...')
  79. ->willThrowException(new \RuntimeException());
  80. // only default
  81. $strategy = new HIncludeFragmentRenderer($engine);
  82. $this->assertEquals('<hx:include src="/foo">loading...</hx:include>', $strategy->render('/foo', Request::create('/'), ['default' => 'loading...'])->getContent());
  83. }
  84. }