No Description

HIncludeFragmentRendererTest.php 4.2KB

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