forked from twigphp/Twig
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSetTest.php
More file actions
113 lines (98 loc) · 3.51 KB
/
SetTest.php
File metadata and controls
113 lines (98 loc) · 3.51 KB
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
<?php
namespace Twig\Tests\Node;
/*
* 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 Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Node\Expression\AssignNameExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\Node\SetNode;
use Twig\Node\TextNode;
use Twig\Test\NodeTestCase;
class SetTest extends NodeTestCase
{
public function testConstructor()
{
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new Node([new ConstantExpression('foo', 1)], [], 1);
$node = new SetNode(false, $names, $values, 1);
$this->assertEquals($names, $node->getNode('names'));
$this->assertEquals($values, $node->getNode('values'));
$this->assertFalse($node->getAttribute('capture'));
}
public static function provideTests(): iterable
{
$tests = [];
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new Node([new ConstantExpression('foo', 1)], [], 1);
$node = new SetNode(false, $names, $values, 1);
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = "foo";
EOF
];
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new Node([new PrintNode(new ConstantExpression('foo', 1), 1)], [], 1);
$node = new SetNode(true, $names, $values, 1);
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
yield from [];
})(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
EOF
, new Environment(new ArrayLoader(), ['use_yield' => true]),
];
$tests[] = [$node, <<<'EOF'
// line 1
$context["foo"] = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros, $blocks) {
yield "foo";
yield from [];
})())) ? '' : new Markup($tmp, $this->env->getCharset());
EOF
, new Environment(new ArrayLoader(), ['use_yield' => false]),
];
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new TextNode('foo', 1);
$node = new SetNode(true, $names, $values, 1);
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = new Markup("foo", \$this->env->getCharset());
EOF
];
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new TextNode('', 1);
$node = new SetNode(true, $names, $values, 1);
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = "";
EOF
];
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new PrintNode(new ConstantExpression('foo', 1), 1);
$node = new SetNode(true, $names, $values, 1);
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = new Markup("foo", \$this->env->getCharset());
EOF
];
$names = new Node([new AssignNameExpression('foo', 1), new AssignNameExpression('bar', 1)], [], 1);
$values = new Node([new ConstantExpression('foo', 1), new NameExpression('bar', 1)], [], 1);
$node = new SetNode(false, $names, $values, 1);
$tests[] = [$node, <<<'EOF'
// line 1
[$context["foo"], $context["bar"]] = ["foo", ($context["bar"] ?? null)];
EOF
];
return $tests;
}
}