4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
11 class Twig_Tests_NodeVisitor_OptimizerTest
extends PHPUnit_Framework_TestCase
13 public function testRenderBlockOptimizer()
15 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
17 $stream = $env->parse($env->tokenize('{{ block("foo") }}', 'index'));
19 $node = $stream->getNode('body')->getNode(0);
21 $this->assertEquals('Twig_Node_Expression_BlockReference', get_class($node));
22 $this->assertTrue($node->getAttribute('output'));
25 public function testRenderParentBlockOptimizer()
27 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
29 $stream = $env->parse($env->tokenize('{% extends "foo" %}{% block content %}{{ parent() }}{% endblock %}', 'index'));
31 $node = $stream->getNode('blocks')->getNode('content')->getNode(0)->getNode('body');
33 $this->assertEquals('Twig_Node_Expression_Parent', get_class($node));
34 $this->assertTrue($node->getAttribute('output'));
37 public function testRenderVariableBlockOptimizer()
39 if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
43 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
44 $stream = $env->parse($env->tokenize('{{ block(name|lower) }}', 'index'));
46 $node = $stream->getNode('body')->getNode(0)->getNode(1);
48 $this->assertEquals('Twig_Node_Expression_BlockReference', get_class($node));
49 $this->assertTrue($node->getAttribute('output'));
53 * @dataProvider getTestsForForOptimizer
55 public function testForOptimizer($template, $expected)
57 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false));
59 $stream = $env->parse($env->tokenize($template, 'index'));
61 foreach ($expected as $target => $withLoop) {
62 $this->assertTrue($this->checkForConfiguration($stream, $target, $withLoop), sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
66 public function getTestsForForOptimizer()
69 array('{% for i in foo %}{% endfor %}', array('i' => false)),
71 array('{% for i in foo %}{{ loop.index }}{% endfor %}', array('i' => true)),
73 array('{% for i in foo %}{% for j in foo %}{% endfor %}{% endfor %}', array('i' => false, 'j' => false)),
75 array('{% for i in foo %}{% include "foo" %}{% endfor %}', array('i' => true)),
77 array('{% for i in foo %}{% include "foo" only %}{% endfor %}', array('i' => false)),
79 array('{% for i in foo %}{% include "foo" with { "foo": "bar" } only %}{% endfor %}', array('i' => false)),
81 array('{% for i in foo %}{% include "foo" with { "foo": loop.index } only %}{% endfor %}', array('i' => true)),
83 array('{% for i in foo %}{% for j in foo %}{{ loop.index }}{% endfor %}{% endfor %}', array('i' => false, 'j' => true)),
85 array('{% for i in foo %}{% for j in foo %}{{ loop.parent.loop.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => true)),
87 array('{% for i in foo %}{% set l = loop %}{% for j in foo %}{{ l.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => false)),
89 array('{% for i in foo %}{% for j in foo %}{{ foo.parent.loop.index }}{% endfor %}{% endfor %}', array('i' => false, 'j' => false)),
91 array('{% for i in foo %}{% for j in foo %}{{ loop["parent"].loop.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => true)),
95 public function checkForConfiguration(Twig_NodeInterface
$node = null, $target, $withLoop)
101 foreach ($node as $n) {
102 if ($n instanceof Twig_Node_For
) {
103 if ($target === $n->getNode('value_target')->getAttribute('name')) {
104 return $withLoop == $n->getAttribute('with_loop');
108 $ret = $this->checkForConfiguration($n, $target, $withLoop);