4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\Fragment;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Controller\ControllerReference;
17 use Symfony\Component\HttpKernel\Fragment\HIncludeFragmentRenderer;
18 use Symfony\Component\HttpKernel\UriSigner;
20 class HIncludeFragmentRendererTest extends TestCase
23 * @expectedException \LogicException
25 public function testRenderExceptionWhenControllerAndNoSigner()
27 $strategy = new HIncludeFragmentRenderer();
28 $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'));
31 public function testRenderWithControllerAndSigner()
33 $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
35 $this->assertEquals('<hx:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&_hash=BP%2BOzCD5MRUI%2BHJpgPDOmoju00FnzLhP3TGcSHbbBLs%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
38 public function testRenderWithUri()
40 $strategy = new HIncludeFragmentRenderer();
41 $this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo', Request::create('/'))->getContent());
43 $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
44 $this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo', Request::create('/'))->getContent());
47 public function testRenderWithDefault()
50 $strategy = new HIncludeFragmentRenderer();
51 $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
53 // only global default
54 $strategy = new HIncludeFragmentRenderer(null, null, 'global_default');
55 $this->assertEquals('<hx:include src="/foo">global_default</hx:include>', $strategy->render('/foo', Request::create('/'), array())->getContent());
57 // global default and default
58 $strategy = new HIncludeFragmentRenderer(null, null, 'global_default');
59 $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
62 public function testRenderWithAttributesOptions()
65 $strategy = new HIncludeFragmentRenderer();
66 $this->assertEquals('<hx:include src="/foo" id="bar">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default', 'id' => 'bar'))->getContent());
69 $strategy = new HIncludeFragmentRenderer();
70 $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());
72 // with id & attributes
73 $strategy = new HIncludeFragmentRenderer();
74 $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());
77 public function testRenderWithDefaultText()
79 $engine = $this->getMockBuilder('Symfony\\Component\\Templating\\EngineInterface')->getMock();
80 $engine->expects($this->once())
83 ->will($this->throwException(new \InvalidArgumentException()));
86 $strategy = new HIncludeFragmentRenderer($engine);
87 $this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent());
90 public function testRenderWithEngineAndDefaultText()
92 $engine = $this->getMockBuilder('Symfony\\Component\\Templating\\EngineInterface')->getMock();
93 $engine->expects($this->once())
96 ->will($this->throwException(new \RuntimeException()));
99 $strategy = new HIncludeFragmentRenderer($engine);
100 $this->assertEquals('<hx:include src="/foo">loading...</hx:include>', $strategy->render('/foo', Request::create('/'), array('default' => 'loading...'))->getContent());