ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
OptimizerTest.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11class Twig_Tests_NodeVisitor_OptimizerTest extends \PHPUnit\Framework\TestCase
12{
13 public function testRenderBlockOptimizer()
14 {
15 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
16
17 $stream = $env->parse($env->tokenize(new Twig_Source('{{ block("foo") }}', 'index')));
18
19 $node = $stream->getNode('body')->getNode(0);
20
21 $this->assertEquals('Twig_Node_Expression_BlockReference', get_class($node));
22 $this->assertTrue($node->getAttribute('output'));
23 }
24
26 {
27 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
28
29 $stream = $env->parse($env->tokenize(new Twig_Source('{% extends "foo" %}{% block content %}{{ parent() }}{% endblock %}', 'index')));
30
31 $node = $stream->getNode('blocks')->getNode('content')->getNode(0)->getNode('body');
32
33 $this->assertEquals('Twig_Node_Expression_Parent', get_class($node));
34 $this->assertTrue($node->getAttribute('output'));
35 }
36
38 {
39 if (PHP_VERSION_ID >= 50400) {
40 $this->markTestSkipped('not needed on PHP >= 5.4');
41 }
42
43 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
44 $stream = $env->parse($env->tokenize(new Twig_Source('{{ block(name|lower) }}', 'index')));
45
46 $node = $stream->getNode('body')->getNode(0)->getNode(1);
47
48 $this->assertEquals('Twig_Node_Expression_BlockReference', get_class($node));
49 $this->assertTrue($node->getAttribute('output'));
50 }
51
55 public function testForOptimizer($template, $expected)
56 {
57 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false));
58
59 $stream = $env->parse($env->tokenize(new Twig_Source($template, 'index')));
60
61 foreach ($expected as $target => $withLoop) {
62 $this->assertTrue($this->checkForConfiguration($stream, $target, $withLoop), sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
63 }
64 }
65
66 public function getTestsForForOptimizer()
67 {
68 return array(
69 array('{% for i in foo %}{% endfor %}', array('i' => false)),
70
71 array('{% for i in foo %}{{ loop.index }}{% endfor %}', array('i' => true)),
72
73 array('{% for i in foo %}{% for j in foo %}{% endfor %}{% endfor %}', array('i' => false, 'j' => false)),
74
75 array('{% for i in foo %}{% include "foo" %}{% endfor %}', array('i' => true)),
76
77 array('{% for i in foo %}{% include "foo" only %}{% endfor %}', array('i' => false)),
78
79 array('{% for i in foo %}{% include "foo" with { "foo": "bar" } only %}{% endfor %}', array('i' => false)),
80
81 array('{% for i in foo %}{% include "foo" with { "foo": loop.index } only %}{% endfor %}', array('i' => true)),
82
83 array('{% for i in foo %}{% for j in foo %}{{ loop.index }}{% endfor %}{% endfor %}', array('i' => false, 'j' => true)),
84
85 array('{% for i in foo %}{% for j in foo %}{{ loop.parent.loop.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => true)),
86
87 array('{% for i in foo %}{% set l = loop %}{% for j in foo %}{{ l.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => false)),
88
89 array('{% for i in foo %}{% for j in foo %}{{ foo.parent.loop.index }}{% endfor %}{% endfor %}', array('i' => false, 'j' => false)),
90
91 array('{% for i in foo %}{% for j in foo %}{{ loop["parent"].loop.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => true)),
92
93 array('{% for i in foo %}{{ include("foo") }}{% endfor %}', array('i' => true)),
94
95 array('{% for i in foo %}{{ include("foo", with_context = false) }}{% endfor %}', array('i' => false)),
96
97 array('{% for i in foo %}{{ include("foo", with_context = true) }}{% endfor %}', array('i' => true)),
98
99 array('{% for i in foo %}{{ include("foo", { "foo": "bar" }, with_context = false) }}{% endfor %}', array('i' => false)),
100
101 array('{% for i in foo %}{{ include("foo", { "foo": loop.index }, with_context = false) }}{% endfor %}', array('i' => true)),
102 );
103 }
104
105 public function checkForConfiguration(Twig_NodeInterface $node = null, $target, $withLoop)
106 {
107 if (null === $node) {
108 return;
109 }
110
111 foreach ($node as $n) {
112 if ($n instanceof Twig_Node_For) {
113 if ($target === $n->getNode('value_target')->getAttribute('name')) {
114 return $withLoop == $n->getAttribute('with_loop');
115 }
116 }
117
118 $ret = $this->checkForConfiguration($n, $target, $withLoop);
119 if (null !== $ret) {
120 return $ret;
121 }
122 }
123 }
124}
$env
$n
Definition: RandomTest.php:85
An exception for terminatinating execution or to throw for unit testing.
Stores the Twig configuration.
Definition: Environment.php:18
Represents a for node.
Definition: For.php:19
Holds information about a non-compiled Twig template.
Definition: Source.php:20
testForOptimizer($template, $expected)
@dataProvider getTestsForForOptimizer
checkForConfiguration(Twig_NodeInterface $node=null, $target, $withLoop)
$template
Represents a node in the AST.
$target
Definition: test.php:19
$stream
PHP stream implementation.
$ret
Definition: parser.php:6