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\ConstantExpression;
13use Twig\Node\Expression\NameExpression;
14use Twig\Node\MacroNode;
15use Twig\Node\Node;
16use Twig\Node\TextNode;
17use Twig\Test\NodeTestCase;
18
19class Twig_Tests_Node_MacroTest extends NodeTestCase
20{
21    public function testConstructor()
22    {
23        $body = new TextNode('foo', 1);
24        $arguments = new Node([new NameExpression('foo', 1)], [], 1);
25        $node = new MacroNode('foo', $body, $arguments, 1);
26
27        $this->assertEquals($body, $node->getNode('body'));
28        $this->assertEquals($arguments, $node->getNode('arguments'));
29        $this->assertEquals('foo', $node->getAttribute('name'));
30    }
31
32    public function getTests()
33    {
34        $body = new TextNode('foo', 1);
35        $arguments = new Node([
36            'foo' => new ConstantExpression(null, 1),
37            'bar' => new ConstantExpression('Foo', 1),
38        ], [], 1);
39        $node = new MacroNode('foo', $body, $arguments, 1);
40
41        if (PHP_VERSION_ID >= 50600) {
42            $declaration = ', ...$__varargs__';
43            $varargs = '$__varargs__';
44        } else {
45            $declaration = '';
46            $varargs = 'func_num_args() > 2 ? array_slice(func_get_args(), 2) : []';
47        }
48
49        return [
50            [$node, <<<EOF
51// line 1
52public function getfoo(\$__foo__ = null, \$__bar__ = "Foo"$declaration)
53{
54    \$context = \$this->env->mergeGlobals([
55        "foo" => \$__foo__,
56        "bar" => \$__bar__,
57        "varargs" => $varargs,
58    ]);
59
60    \$blocks = [];
61
62    ob_start();
63    try {
64        echo "foo";
65    } catch (\Exception \$e) {
66        ob_end_clean();
67
68        throw \$e;
69    } catch (\Throwable \$e) {
70        ob_end_clean();
71
72        throw \$e;
73    }
74
75    return ('' === \$tmp = ob_get_clean()) ? '' : new Markup(\$tmp, \$this->env->getCharset());
76}
77EOF
78            ],
79        ];
80    }
81}
82