tests/TemplateWrapperTest.php
d7062f35
 <?php
 
2119e60c
 namespace Twig\Tests;
 
d7062f35
 /*
  * This file is part of Twig.
  *
  * (c) Fabien Potencier
  *
  * For the full copyright and license information, please view the LICENSE
  * file that was distributed with this source code.
  */
1ddb8253
 
34bdab4d
 use PHPUnit\Framework\TestCase;
1ddb8253
 use Twig\Environment;
 use Twig\Loader\ArrayLoader;
e9919c21
 
34bdab4d
 class TemplateWrapperTest extends TestCase
d7062f35
 {
     public function testHasGetBlocks()
     {
1ddb8253
         $twig = new Environment(new ArrayLoader([
d7062f35
             'index' => '{% block foo %}{% endblock %}',
             'index_with_use' => '{% use "imported" %}{% block foo %}{% endblock %}',
             'index_with_extends' => '{% extends "extended" %}{% block foo %}{% endblock %}',
             'imported' => '{% block imported %}{% endblock %}',
             'extended' => '{% block extended %}{% endblock %}',
5c55243d
         ]));
d7062f35
 
e2103c87
         $wrapper = $twig->load('index');
d7062f35
         $this->assertTrue($wrapper->hasBlock('foo'));
         $this->assertFalse($wrapper->hasBlock('bar'));
5c55243d
         $this->assertEquals(['foo'], $wrapper->getBlockNames());
d7062f35
 
e2103c87
         $wrapper = $twig->load('index_with_use');
d7062f35
         $this->assertTrue($wrapper->hasBlock('foo'));
         $this->assertTrue($wrapper->hasBlock('imported'));
5c55243d
         $this->assertEquals(['imported', 'foo'], $wrapper->getBlockNames());
d7062f35
 
e2103c87
         $wrapper = $twig->load('index_with_extends');
d7062f35
         $this->assertTrue($wrapper->hasBlock('foo'));
         $this->assertTrue($wrapper->hasBlock('extended'));
5c55243d
         $this->assertEquals(['foo', 'extended'], $wrapper->getBlockNames());
d7062f35
     }
159bfec4
 
     public function testRenderBlock()
     {
1ddb8253
         $twig = new Environment(new ArrayLoader([
159bfec4
             'index' => '{% block foo %}{{ foo }}{{ bar }}{% endblock %}',
5c55243d
         ]));
159bfec4
         $twig->addGlobal('bar', 'BAR');
 
e2103c87
         $wrapper = $twig->load('index');
5c55243d
         $this->assertEquals('FOOBAR', $wrapper->renderBlock('foo', ['foo' => 'FOO']));
159bfec4
     }
 
     public function testDisplayBlock()
     {
1ddb8253
         $twig = new Environment(new ArrayLoader([
159bfec4
             'index' => '{% block foo %}{{ foo }}{{ bar }}{% endblock %}',
5c55243d
         ]));
159bfec4
         $twig->addGlobal('bar', 'BAR');
 
e2103c87
         $wrapper = $twig->load('index');
159bfec4
 
         ob_start();
5c55243d
         $wrapper->displayBlock('foo', ['foo' => 'FOO']);
159bfec4
 
         $this->assertEquals('FOOBAR', ob_get_clean());
     }
d7062f35
 }