HIncludeFragmentRendererTest.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Fragment\Tests\FragmentRenderer;
  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. protected function setUp()
  18. {
  19. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  20. $this->markTestSkipped('The "HttpFoundation" component is not available');
  21. }
  22. }
  23. /**
  24. * @expectedException \LogicException
  25. */
  26. public function testRenderExceptionWhenControllerAndNoSigner()
  27. {
  28. $strategy = new HIncludeFragmentRenderer();
  29. $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'));
  30. }
  31. public function testRenderWithControllerAndSigner()
  32. {
  33. $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
  34. $this->assertEquals('<hx:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&amp;_hash=5RZ1IkwF487EaXt6buHka73CCtQ%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
  35. }
  36. public function testRenderWithUri()
  37. {
  38. $strategy = new HIncludeFragmentRenderer();
  39. $this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo', Request::create('/'))->getContent());
  40. $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
  41. $this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo', Request::create('/'))->getContent());
  42. }
  43. public function testRenderWithDefault()
  44. {
  45. // only default
  46. $strategy = new HIncludeFragmentRenderer();
  47. $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
  48. // only global default
  49. $strategy = new HIncludeFragmentRenderer(null, null, 'global_default');
  50. $this->assertEquals('<hx:include src="/foo">global_default</hx:include>', $strategy->render('/foo', Request::create('/'), array())->getContent());
  51. // global default and default
  52. $strategy = new HIncludeFragmentRenderer(null, null, 'global_default');
  53. $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
  54. }
  55. public function testRenderWithAttributesOptions()
  56. {
  57. // with id
  58. $strategy = new HIncludeFragmentRenderer();
  59. $this->assertEquals('<hx:include src="/foo" id="bar">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default', 'id' => 'bar'))->getContent());
  60. // with attributes
  61. $strategy = new HIncludeFragmentRenderer();
  62. $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());
  63. // with id & attributes
  64. $strategy = new HIncludeFragmentRenderer();
  65. $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());
  66. }
  67. public function testRenderWithDefaultText()
  68. {
  69. $engine = $this->getMock('Symfony\\Component\\Templating\\EngineInterface');
  70. $engine->expects($this->once())
  71. ->method('exists')
  72. ->with('default')
  73. ->will($this->throwException(new \InvalidArgumentException()));
  74. // only default
  75. $strategy = new HIncludeFragmentRenderer($engine);
  76. $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
  77. }
  78. }