-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathOptimizerTest.php
149 lines (110 loc) · 5.43 KB
/
OptimizerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Twig\Tests\NodeVisitor;
/*
* 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.
*/
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Node\Expression\BlockReferenceExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\ParentExpression;
use Twig\Node\ForNode;
use Twig\Node\Node;
use Twig\NodeVisitor\OptimizerNodeVisitor;
use Twig\Source;
class OptimizerTest extends TestCase
{
public function testConstructor()
{
$this->expectNotToPerformAssertions();
new OptimizerNodeVisitor(OptimizerNodeVisitor::OPTIMIZE_FOR);
}
public function testRenderBlockOptimizer()
{
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$stream = $env->parse($env->tokenize(new Source('{{ block("foo") }}', 'index')));
$node = $stream->getNode('body')->getNode('0');
$this->assertInstanceOf(BlockReferenceExpression::class, $node);
$this->assertTrue($node->getAttribute('output'));
}
public function testRenderParentBlockOptimizer()
{
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$stream = $env->parse($env->tokenize(new Source('{% extends "foo" %}{% block content %}{{ parent() }}{% endblock %}', 'index')));
$node = $stream->getNode('blocks')->getNode('content')->getNode('0')->getNode('body');
$this->assertInstanceOf(ParentExpression::class, $node);
$this->assertTrue($node->getAttribute('output'));
}
public function testForVarOptimizer()
{
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$template = '{% for i, j in foo %}{{ loop.index }}{{ i }}{{ j }}{% endfor %}';
$stream = $env->parse($env->tokenize(new Source($template, 'index')));
foreach (['loop', 'i', 'j'] as $target) {
$this->checkForVarConfiguration($stream, $target);
}
}
public function checkForVarConfiguration(Node $node, $target)
{
foreach ($node as $n) {
if (NameExpression::class === get_class($n) && $target === $n->getAttribute('name')) {
$this->assertTrue($n->getAttribute('always_defined'));
} else {
$this->checkForVarConfiguration($n, $target);
}
}
}
/**
* @dataProvider getTestsForForLoopOptimizer
*/
public function testForLoopOptimizer($template, $expected)
{
$env = new Environment(new ArrayLoader(), ['cache' => false]);
$stream = $env->parse($env->tokenize(new Source($template, 'index')));
foreach ($expected as $target => $withLoop) {
$this->assertTrue($this->checkForLoopConfiguration($stream, $target, $withLoop), \sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
}
}
public static function getTestsForForLoopOptimizer()
{
return [
['{% for i in foo %}{% endfor %}', ['i' => false]],
['{% for i in foo %}{{ loop.index }}{% endfor %}', ['i' => true]],
['{% for i in foo %}{% for j in foo %}{% endfor %}{% endfor %}', ['i' => false, 'j' => false]],
['{% for i in foo %}{% include "foo" %}{% endfor %}', ['i' => true]],
['{% for i in foo %}{% include "foo" only %}{% endfor %}', ['i' => false]],
['{% for i in foo %}{% include "foo" with { "foo": "bar" } only %}{% endfor %}', ['i' => false]],
['{% for i in foo %}{% include "foo" with { "foo": loop.index } only %}{% endfor %}', ['i' => true]],
['{% for i in foo %}{% for j in foo %}{{ loop.index }}{% endfor %}{% endfor %}', ['i' => false, 'j' => true]],
['{% for i in foo %}{% for j in foo %}{{ loop.parent.loop.index }}{% endfor %}{% endfor %}', ['i' => true, 'j' => true]],
['{% for i in foo %}{% set l = loop %}{% for j in foo %}{{ l.index }}{% endfor %}{% endfor %}', ['i' => true, 'j' => false]],
['{% for i in foo %}{% for j in foo %}{{ foo.parent.loop.index }}{% endfor %}{% endfor %}', ['i' => false, 'j' => false]],
['{% for i in foo %}{% for j in foo %}{{ loop["parent"].loop.index }}{% endfor %}{% endfor %}', ['i' => true, 'j' => true]],
['{% for i in foo %}{{ include("foo") }}{% endfor %}', ['i' => true]],
['{% for i in foo %}{{ include("foo", with_context = false) }}{% endfor %}', ['i' => false]],
['{% for i in foo %}{{ include("foo", with_context = true) }}{% endfor %}', ['i' => true]],
['{% for i in foo %}{{ include("foo", { "foo": "bar" }, with_context = false) }}{% endfor %}', ['i' => false]],
['{% for i in foo %}{{ include("foo", { "foo": loop.index }, with_context = false) }}{% endfor %}', ['i' => true]],
];
}
public function checkForLoopConfiguration(Node $node, $target, $withLoop)
{
foreach ($node as $n) {
if ($n instanceof ForNode) {
if ($target === $n->getNode('value_target')->getAttribute('name')) {
return $withLoop == $n->getAttribute('with_loop');
}
}
$ret = $this->checkForLoopConfiguration($n, $target, $withLoop);
if (null !== $ret) {
return $ret;
}
}
}
}