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 */
11
12use Twig\Node\Expression\AssignNameExpression;
13use Twig\Node\Expression\ConstantExpression;
14use Twig\Node\Expression\NameExpression;
15use Twig\Node\Node;
16use Twig\Node\PrintNode;
17use Twig\Node\SetNode;
18use Twig\Node\TextNode;
19use Twig\Test\NodeTestCase;
20
21class Twig_Tests_Node_SetTest extends NodeTestCase
22{
23    public function testConstructor()
24    {
25        $names = new Node([new AssignNameExpression('foo', 1)], [], 1);
26        $values = new Node([new ConstantExpression('foo', 1)], [], 1);
27        $node = new SetNode(false, $names, $values, 1);
28
29        $this->assertEquals($names, $node->getNode('names'));
30        $this->assertEquals($values, $node->getNode('values'));
31        $this->assertFalse($node->getAttribute('capture'));
32    }
33
34    public function getTests()
35    {
36        $tests = [];
37
38        $names = new Node([new AssignNameExpression('foo', 1)], [], 1);
39        $values = new Node([new ConstantExpression('foo', 1)], [], 1);
40        $node = new SetNode(false, $names, $values, 1);
41        $tests[] = [$node, <<<EOF
42// line 1
43\$context["foo"] = "foo";
44EOF
45        ];
46
47        $names = new Node([new AssignNameExpression('foo', 1)], [], 1);
48        $values = new Node([new PrintNode(new ConstantExpression('foo', 1), 1)], [], 1);
49        $node = new SetNode(true, $names, $values, 1);
50        $tests[] = [$node, <<<EOF
51// line 1
52ob_start();
53echo "foo";
54\$context["foo"] = ('' === \$tmp = ob_get_clean()) ? '' : new Markup(\$tmp, \$this->env->getCharset());
55EOF
56        ];
57
58        $names = new Node([new AssignNameExpression('foo', 1)], [], 1);
59        $values = new TextNode('foo', 1);
60        $node = new SetNode(true, $names, $values, 1);
61        $tests[] = [$node, <<<EOF
62// line 1
63\$context["foo"] = ('' === \$tmp = "foo") ? '' : new Markup(\$tmp, \$this->env->getCharset());
64EOF
65        ];
66
67        $names = new Node([new AssignNameExpression('foo', 1), new AssignNameExpression('bar', 1)], [], 1);
68        $values = new Node([new ConstantExpression('foo', 1), new NameExpression('bar', 1)], [], 1);
69        $node = new SetNode(false, $names, $values, 1);
70        $tests[] = [$node, <<<EOF
71// line 1
72list(\$context["foo"], \$context["bar"]) = ["foo", {$this->getVariableGetter('bar')}];
73EOF
74        ];
75
76        return $tests;
77    }
78}
79