Skip to content

Commit

Permalink
minor #4130 Add more tests (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Add more tests

Commits
-------

133cbb7 Add more tests
  • Loading branch information
fabpot committed Jul 9, 2024
2 parents 13b9afa + 133cbb7 commit 03fa665
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions tests/NodeVisitor/OptimizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\Node\Expression\BlockReferenceExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\ParentExpression;
use Twig\Node\ForNode;
use Twig\Node\Node;
Expand Down Expand Up @@ -46,21 +47,44 @@ public function testRenderParentBlockOptimizer()
$this->assertTrue($node->getAttribute('output'));
}

public function testForVarOptimizer()
{
$env = new Environment($this->createMock(LoaderInterface::class), ['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 getTestsForForOptimizer
* @dataProvider getTestsForForLoopOptimizer
*/
public function testForOptimizer($template, $expected)
public function testForLoopOptimizer($template, $expected)
{
$env = new Environment($this->createMock(LoaderInterface::class), ['cache' => false]);

$stream = $env->parse($env->tokenize(new Source($template, 'index')));

foreach ($expected as $target => $withLoop) {
$this->assertTrue($this->checkForConfiguration($stream, $target, $withLoop), \sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
$this->assertTrue($this->checkForLoopConfiguration($stream, $target, $withLoop), \sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
}
}

public function getTestsForForOptimizer()
public function getTestsForForLoopOptimizer()
{
return [
['{% for i in foo %}{% endfor %}', ['i' => false]],
Expand Down Expand Up @@ -99,7 +123,7 @@ public function getTestsForForOptimizer()
];
}

public function checkForConfiguration(Node $node, $target, $withLoop)
public function checkForLoopConfiguration(Node $node, $target, $withLoop)
{
foreach ($node as $n) {
if ($n instanceof ForNode) {
Expand All @@ -108,7 +132,7 @@ public function checkForConfiguration(Node $node, $target, $withLoop)
}
}

$ret = $this->checkForConfiguration($n, $target, $withLoop);
$ret = $this->checkForLoopConfiguration($n, $target, $withLoop);
if (null !== $ret) {
return $ret;
}
Expand Down

0 comments on commit 03fa665

Please sign in to comment.